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

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

BACKEND=canondr_backend2510
MODELCONF=DR-2510C.conf

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

case "$1" in
    configure)

	mkdir -p $INSTALLDIR/etc/sane.d/canondr
	mkdir -p $INSTALLDIR/lib/canondr
	mkdir -p $INSTALLDIR/lib/sane

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

	cp -f $TMP_INSTALLDIR/lib/canondr/$BACKEND $INSTALLDIR/lib/canondr/$BACKEND

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

	if [ -e $INSTALLDIR/etc/sane.d/canondr.conf ]
	then
		(if grep -q "DR-2510C" $INSTALLDIR/etc/sane.d/canondr.conf;then :;  \
		else cat $TMP_INSTALLDIR/etc/sane.d/canondr.conf >> $INSTALLDIR/etc/sane.d/canondr.conf; fi)
	else
		cp -f $TMP_INSTALLDIR/etc/sane.d/canondr.conf $INSTALLDIR/etc/sane.d/canondr.conf
		ln -sf $INSTALLDIR/etc/sane.d/canondr.conf $SANECONFDIR/
		
		CONFILE=$SANECONFDIR/dll.conf
		if [ -n "`grep '#[[:space:]]*canondr' $CONFILE`" ];then
   		sed -i 's,#[[:space:]]*\(canondr\),\1,' $CONFILE
		elif [ -z "`grep canondr $CONFILE`" ];then
   		echo canondr >> $CONFILE
		fi
		
	fi

#*************install binary files************#
	if [ -e $INSTALLDIR/lib/canondr/canondr_com_usb ]
	then
		rm -f $INSTALLDIR/lib/canondr/canondr_com_usb
	fi
	cp -f $TMP_INSTALLDIR/lib/canondr/canondr_com_usb $INSTALLDIR/lib/canondr/canondr_com_usb
	chmod u+s $INSTALLDIR/lib/canondr/canondr_com_usb

	if [ -e $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 ]
	then
		rm -f $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0
		/usr/bin/install -c -m 755 $TMP_INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0
	else
		/usr/bin/install -c -m 755 $TMP_INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0
		ln -sf $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 $SANELIBDIR/
		ln -sf $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 $SANELIBDIR/libsane-canondr.so.1
		ln -sf $INSTALLDIR/lib/sane/libsane-canondr.so.1.0.0 $SANELIBDIR/libsane-canondr.so
	fi

#*************install documents******************#
	rm -rf $DOCDIR/$NAME*
	cp -rf $TMP_INSTALLDIR/share/doc/* $DOCDIR

#*************delete tmp directory******************#
	rm -rf $TMP_INSTALLDIR
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst 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


