#!/bin/sh
PHASE3_3_SIGNATURE="/Library/Printers/Kyocera/PDEs/KeAbout.plugin"
PHASE4_0_SIGNATURE="/Library/Printers/Kyocera/PDEs/KfSummary.plugin"
PHASE4_1_SIGNATURE="/Library/Printers/Kyocera/PDEs/KgSummary.plugin"
CUPS_PPD_DIR="/etc/cups/ppd/"
PRINTER_PPD_DIR="/Library/Printers/PPDs/Contents/Resources/"
FILETYPE=".ppd"
LPADMIN=/usr/sbin/lpadmin

echo Delete Kyocera Mac Phase 3.3 printers PPDs in Library folder
if [ -x ${LPADMIN} ]; then
    XPPDS=(`grep -l "$PHASE3_3_SIGNATURE" ${PRINTER_PPD_DIR}*`)
    entry=
    for xppd in ${XPPDS[@]}; do
	if [[ $xppd == *ppd ]]
	then 
		if [[ ! -z $entry ]] ; then		
			entry=${entry}\\\ ${xppd}
			cmd=rm\ -f\ ${entry}
			eval $cmd
			rm -f “${entryFinal}”
			entry=
		else
			rm -f $xppd
			entry=
		fi
	elif [[ $xppd == *PPD ]]
	then 
		if [[ ! -z $entry ]] ; then		
			entry=${entry}\\\ ${xppd}
			cmd=rm\ -f\ ${entry}
			eval $cmd
			rm -f “${entryFinal}”
			entry=
		else
			rm -f $xppd
			entry=
		fi
	else
		if [[ ! -z $entry ]] ; then	
			entry=${entry}\\\ ${xppd}	
		else
			entry=${xppd}
		fi
	fi
    done
fi
echo DONE

echo Delete Kyocera Mac Phase 4.0 printers PPDs in Library folder
if [ -x ${LPADMIN} ]; then
XPPDS=(`grep -l "$PHASE4_0_SIGNATURE" ${PRINTER_PPD_DIR}*`)
entry=
for xppd in ${XPPDS[@]}; do
if [[ $xppd == *ppd ]]
then
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
cmd=rm\ -f\ ${entry}
eval $cmd
rm -f “${entryFinal}”
entry=
else
rm -f $xppd
entry=
fi
elif [[ $xppd == *PPD ]]
then
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
cmd=rm\ -f\ ${entry}
eval $cmd
rm -f “${entryFinal}”
entry=
else
rm -f $xppd
entry=
fi
else
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
else
entry=${xppd}
fi
fi
done
fi
echo DONE

echo Delete Kyocera Mac Phase 4.1 printers PPDs in Library folder
if [ -x ${LPADMIN} ]; then
XPPDS=(`grep -l "$PHASE4_1_SIGNATURE" ${PRINTER_PPD_DIR}*`)
entry=
for xppd in ${XPPDS[@]}; do
if [[ $xppd == *ppd ]]
then
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
cmd=rm\ -f\ ${entry}
eval $cmd
rm -f “${entryFinal}”
entry=
else
rm -f $xppd
entry=
fi
elif [[ $xppd == *PPD ]]
then
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
cmd=rm\ -f\ ${entry}
eval $cmd
rm -f “${entryFinal}”
entry=
else
rm -f $xppd
entry=
fi
else
if [[ ! -z $entry ]] ; then
entry=${entry}\\\ ${xppd}
else
entry=${xppd}
fi
fi
done
fi
echo DONE

echo Delete Kyocera Mac Phase 3.3 printers in printer queue
if [ -x ${LPADMIN} ]; then
    XPPDS=(`grep -l "$PHASE3_3_SIGNATURE" ${CUPS_PPD_DIR}*`)
    for xppd in ${XPPDS[@]}; do
	#remove directory name
	ppdfile=${xppd#${CUPS_PPD_DIR}}
	printer=${ppdfile%${FILETYPE}}
	echo ${printer}
	${LPADMIN} -x ${printer}
	if [ $? -eq 0 ]; then
	    echo Succeed to delete ${printer}
	else
	    echo Fail to delete ${printer}
	fi
    done
fi
echo DONE

echo Delete Kyocera Mac Phase 4.0 printers in printer queue
if [ -x ${LPADMIN} ]; then
XPPDS=(`grep -l "$PHASE4_0_SIGNATURE" ${CUPS_PPD_DIR}*`)
for xppd in ${XPPDS[@]}; do
#remove directory name
ppdfile=${xppd#${CUPS_PPD_DIR}}
printer=${ppdfile%${FILETYPE}}
echo ${printer}
${LPADMIN} -x ${printer}
if [ $? -eq 0 ]; then
echo Succeed to delete ${printer}
else
echo Fail to delete ${printer}
fi
done
fi
echo DONE

echo Delete Kyocera Mac Phase 4.1 printers in printer queue
if [ -x ${LPADMIN} ]; then
XPPDS=(`grep -l "$PHASE4_1_SIGNATURE" ${CUPS_PPD_DIR}*`)
for xppd in ${XPPDS[@]}; do
#remove directory name
ppdfile=${xppd#${CUPS_PPD_DIR}}
printer=${ppdfile%${FILETYPE}}
echo ${printer}
${LPADMIN} -x ${printer}
if [ $? -eq 0 ]; then
echo Succeed to delete ${printer}
else
echo Fail to delete ${printer}
fi
done
fi
echo DONE

echo Delete Filters, PDE, Framework, Print Option Pane
rm -rf "/usr/libexec/cups/filter/kyoprefilter_E"
rm -rf "/usr/libexec/cups/filter/kyofilter_E"
rm -rf "/usr/libexec/cups/filter/kyoPDFfilter"
rm -rf "/Library/Printers/Kyocera/pcl/rastertopcl_b.app"
rm -rf "/Library/Printers/Kyocera/Frameworks/KePDECommon.framework"
rm -rf /Library/Printers/Kyocera/PDEs/Ke*.plugin
rm -rf "/usr/libexec/cups/filter/kyoprefilter_F"
rm -rf "/usr/libexec/cups/filter/kyofilter_F"
rm -rf "/usr/libexec/cups/filter/kyoPDFfilter_F"
rm -rf "/Library/Printers/Kyocera/pcl/rastertopcl_F.app"
rm -rf "/usr/libexec/cups/filter/kyoprefilter_*"
rm -rf "/usr/libexec/cups/filter/kyofilter_*"
rm -rf "/usr/libexec/cups/filter/kyoPDFfilter_*"
rm -rf "/Library/Printers/Kyocera/pcl/rastertopcl_*"

echo Delete Print Panel app
rm -rf "/Applications/Kyocera Print Panel*"

# rm -rf "~/Library/PreferencePanes/PrintOptionPane.prefPane"
# rm -rf "/Library/PreferencePanes/PrintOptionPane.prefPane"
exit 0
