#!/bin/sh
# KYOCERA_SIGNATURE="/Library/Printers/Kyocera"
KYOCERA_SIGNATURE="Kyocera"
CUPS_PPD_DIR="/etc/cups/ppd/"
PRINTER_PPD_DIR="/Library/Printers/PPDs/Contents/Resources/"
FILETYPE=".ppd"
LPADMIN=/usr/sbin/lpadmin

echo Delete All Kyocera and TA Mac printers PPDs in Library folder
if [ -x ${LPADMIN} ]; then
    XPPDS=(`grep -l -i "$KYOCERA_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 All Kyocera and TA Mac printers in printer queue
if [ -x ${LPADMIN} ]; then
    XPPDS=(`grep -l -i "$KYOCERA_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
	
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M2035dn\(J\).PPD
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M2535dn\(J\).PPD
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M6530cdn\(J\).PPD
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M6535cidn\(J\).PPD
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P6130cdn\(J\).PPD	
	rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P7040cdn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P3045dn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P3060dn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M5526cdw\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P5026cdw\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M3645idn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ M6635cidn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P6230cdn\(J\).PPD
    rm -rf /Library/Printers/PPDs/Contents/Resources/Kyocera\ ECOSYS\ P7240cdn\(J\).PPD

fi
echo DONE

echo Delete Filters, PDE, Framework, Print Option Pane
rm -rf /usr/libexec/cups/filter/kyo*
rm -rf "~/Library/PreferencePanes/PrintOptionPane.prefPane"
rm -rf "/Library/PreferencePanes/PrintOptionPane.prefPane"
rm -rf /Library/Printers/Kyocera/
rm -rf ~/Library/Application\ Support/Kyocera\ Print\ Panel*
rm -rf ~/Library/Application\ Support/TA_UTAX\ Print\ Panel*
rm -rf ~/Library/Application\ Support/OLIVETTI\ Print\ Panel*
rm -rf /Applications/Kyocera\ Print\ Panel*
rm -rf /Applications/TA_UTAX\ Print\ Panel*
rm -rf /Applications/OLIVETTI\ Print\ Panel*

exit 0
