#!/bin/bash

DESTDIR="$1"
pixmascan_installed=no
backend_installed=no

read_yesno()
{
    msg=$2
    if [ $1 = y ]; then
	msg="$msg [YES/no] "
    else
	msg="$msg [yes/NO] "
    fi
    ans=none
    while [ $ans = none ]; do
	echo -n "$msg"
	read temp
	case x"$temp" in
	    xYes|xyes|xYES|xY|xy) ans=y ;;
	    xNo|xno|xNO|xN|xn)    ans=n ;;
	    x)                    ans=$1 ;;
        esac
    done
}

if [ ! -x scan -o ! -r libsane-pixma.so ]; then
    echo "It seems that the software wasn't correctly compiled."
    echo "Run 'make' again and check the error message."
fi

######################################################
## Detect the scanner
######################################################
cont=no
echo
echo "Please connect and turn on your scanner! I will try to detect it."
echo -n "Please wait until your scanner is ready, then press return..."
read
./scan -L && cont=yes

if [ $cont = no ]; then
    echo
    echo "Sorry, I couldn't detect your scanner."
    echo "Please check if it is connected and turned on."
    echo "The following models are currently supported."
    ./scan -LL
    exit 1
fi

######################################################
## Test scan
######################################################
testpnm=/tmp/pixmascan.pnm
testlog=/tmp/pixmascan.log
testopt="-x 10 -y 15 -w 51 -h 25 -1 -d 10 -W"

echo
echo "I'm about to do a test scan. Please put a magazine or photo in the scanner."
echo "The image will be saved in $testpnm, log file in $testlog."
read_yesno y "Proceed?"
if [ $ans = n ]; then
    exit 1
fi
echo "Execute: ./scan $testopt $testpnm 2> $testlog"
./scan $testopt $testpnm 2> $testlog || cont=no

if test $cont = no && grep EXPERIMENT $testlog > /dev/null; then
    read_yesno n "Really proceed?"
    if [ $ans = n ]; then
        exit 1
    fi
    echo "OK, you have been warned!"
    ./scan $testopt -F experiment $testpnm 2> $testlog && cont=yes
fi

if [ $cont = no ]; then
    echo "Sorry, the driver didn't work correctly with your scanner."
    echo "You may want to check $testpnm and $testlog."
    exit 1
fi

echo "Ok, the driver seems to work with your scanner."
echo "Check $testpnm"


######################################################
## Install scan
######################################################
echo
dst="$DESTDIR/usr/local/bin"
read_yesno y "Install the stand-alone program in '$dst'?"
if [ $ans = y ]; then
    if install -D scan "$dst/pixmascan"; then
	echo "The stand-alone programm was successfully installed at"
	echo "$dst/pixmascan"
	pixmascan_installed=yes
    fi
else
    echo "Skip installing the stand-alone program."
fi

######################################################
## Install backend
######################################################
libdirs="/usr/lib/sane /usr/local/lib/sane"
backenddir=
for d in $libdirs; do
    if [ -e $d/libsane-dll.so.1 ]; then
        backenddir=$d
        break
    fi
done

confdirs="/etc/sane.d /usr/local/etc/sane.d"
dllconf=
for d in $confdirs; do
    if [ -e $d/dll.conf ]; then
        dllconf=$d/dll.conf
        break
    fi
done

echo
echo -n "SANE backend directory: "
if [ "$backenddir" ]; then
    echo "$backenddir"
else
    echo "NOT FOUND!"
fi
echo -n "SANE dll.conf: "
if [ "$dllconf" ]; then
    echo "$dllconf"
else
    echo "NOT FOUND!"
fi
if [ ! "$backenddir" -o ! "$dllconf" ]; then
    echo "Unable to find SANE backend directory and dll.conf!"
    echo "Have you already installed the SANE package?"
else
    cont=yes
    echo "Installing the SANE backend..."
    if [ -e "$backenddir/libsane-pixma.so.1" ]; then
	echo "libsane-pixma.so.1 already exists in $backenddir."
	read_yesno n "Overwrite?"
	[ $ans = n ] && cont=no
    fi
    if [ $cont = yes ]; then
	rm -f "$backenddir/libsane-pixma.so.1"
	install libsane-pixma.so "$backenddir/libsane-pixma.so.1" || cont=no
    fi
    if [ $cont = yes ]; then
	temp=`grep pixma "$dllconf"`
	if [ "$temp" != pixma ]; then
	    echo "Inserting the backend name 'pixma' into $dllconf"
	    echo pixma >> "$dllconf"
	fi
	backend_installed=yes
    fi
fi

if [ $pixmascan_installed = yes -o $backend_installed = yes ]; then
    echo
    echo "Basic installation completed."
    echo "NOTE: You probably need to set the permission of the device node in"
    echo "      /dev/bus/usb or /proc/bus/usb."
fi


######################################################
## Test backend
######################################################
if [ $backend_installed = yes ]; then
    testpnm=/tmp/scanimage.pnm
    testlog=/tmp/scanimage.log
    echo
    read_yesno y "Now, I will test the SANE backend. Continue?"
    echo "Execute: scanimage > $testpnm 2> $testlog"
    PIXMA_EXPERIMENT=1 SANE_DEBUG_PIXMA=10 scanimage > $testpnm 2> $testlog
    echo "Check $testpnm and $testlog"
fi

exit 0

