#!/bin/sh
# postrm script for cndrvsane-dr2510c
#
# see: dh_installdeb(1)

INSTALLDIR=/opt/Canon
SANECONFDIR=/etc/sane.d
SANELIBDIR=/usr/lib/sane
VERSION=1.00
DOCDIR=/usr/share/doc/cndrvsane-dr2510c

BACKEND=canondr_backend2510
MODELCONF=DR-2510C.conf

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)

#*****************uninstall BACKEND*****************#
	if [ -e $INSTALLDIR/lib/canondr/$BACKEND ]
	then
		rm -f $INSTALLDIR/lib/canondr/$BACKEND
	fi

#******************uninstall config files************#
	if [ -e $INSTALLDIR/etc/sane.d/canondr/$MODELCONF ]
	then
		rm -f $INSTALLDIR/etc/sane.d/canondr/$MODELCONF
	fi

	if [ -e $INSTALLDIR/etc/sane.d/canondr.conf ]
	then
		cat $INSTALLDIR/etc/sane.d/canondr.conf|awk '$3=="DR-2510C"{print NR}'>tmp.txt
		read  START < tmp.txt
		if [ $? -eq 0 ]
		then		
			cat $INSTALLDIR/etc/sane.d/canondr.conf|sed "$START, +3 d" > out.txt
			cp -f out.txt $INSTALLDIR/etc/sane.d/canondr.conf
			rm -f tmp.txt out.txt
		fi
	fi

	LINE=`cat $INSTALLDIR/etc/sane.d/canondr.conf|wc -l`
	if [ $LINE -lt 4 ]
	then
		rm -f $INSTALLDIR/etc/sane.d/canondr.conf
		rm -f $SANECONFDIR/canondr.conf
		
		CONFFILE=$SANECONFDIR/dll.conf
   	if [ -n "`grep ^canondr $CONFFILE`" ];then
			sed -i 's/^canondr/#canondr/' $CONFFILE
   	fi
   
	fi

#******************uninstall binary files************#
	if [ ! -e $INSTALLDIR/etc/sane.d/canondr.conf ]
	then
		if [ -e $INSTALLDIR/lib/canondr/canondr_com_usb ]
		then
			rm -f $INSTALLDIR/lib/canondr/canondr_com_usb
		fi

		if [ -e $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 ]
		then
			rm -f $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0
			rm -f $SANELIBDIR/libsane-canondr.so
			rm -f $SANELIBDIR/libsane-canondr.so.1
			rm -f $SANELIBDIR/libsane-canondr.so.1.0.0
		fi
	fi

#*****************remove documents*********************#
	rm -rf $DOCDIR

#******************romove install directory************#
	if [ ! -e $INSTALLDIR/etc/sane.d/canondr.conf ]
	then
		rm -rf $INSTALLDIR
	fi

    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


