#/bin/bash -x

function NO_PKGS {
    echo The distribution that you appear to be running is does not currently
    echo have packages available for it. If you would be willing to assist the
    echo development team in making packages for your distribution. Please 
    echo email ben@zork.net
    exit 3
}


if [ `whoami` != root ]; then
    echo This script must be run as root.
    exit 1
fi

if [ -z `arch | grep i.86` ]; then
    echo Packages are currently only available for intel based distributions
    echo If you would be willing to make packages for your architecture
    echo please e-mail ben@zork.net
    exit 2
fi


if [ -f /etc/debian_version ]; then
	#debian's easy peasy
    if grep 'lpr.sourceforge.net' /etc/apt/sources.list ; then
    	echo "you appear to already have us in your sources.list"
	apt-get update && apt-get install task-gnulpr && exit 0
    else
    	echo '# gnulpr lines (added by http://lpr.sourceforge.net/install-binaries.sh)' >> /etc/apt/sources.list
    	echo 'deb http://lpr.sourceforge.net/debian potato main' >> /etc/apt/sources.list
    	echo 'deb-src http://lpr.sourceforge.net/debian potato main' >> /etc/apt/sources.list
	apt-get update && apt-get install task-gnulpr && exit 0
    fi
 # mandrake 7 and redhat 6 now use the keen installer of doom.
elif [ -f /etc/mandrake-release ];then
    if  grep '7\.[0-2]' /etc/mandrake-release ;then
	DIST="mandrake-70-i386"
	if rpm -q cups 2>/dev/null; then
	    echo "*** NOTICE ***"
	    echo " If you have currently working printers set up using CUPS,"
	    echo "they will cease to function shortly. The CUPS packages are"
	    echo "about to be removed to make way for gnulpr."
	    echo
	    echo "[Press Enter to continue, or ^C to abort]"
	    
	    read i < /dev/tty
	    # the nodeps is neccessary to work around the fact that
	    # mandrake makes samba dependant cups.
	fi
	if rpm -q samba 2>/dev/null; then
	    echo "*** NOTICE ***"
	    echo " The samba configuration will need to be changed to work"
	    echo "with LPR rather than CUPS. A copy of your current"
	    echo "configuration will be saved in /etc/smb.conf-"
	    echo
	    echo "[Press Enter to continue]"
	    
	    read i < /dev/tty
	    mv /etc/smb.conf /etc/smb.conf-
	    sed -e 's/printing.*=.*cups/printing = bsd/' \
                -e '/^[ ]*print command/d' -e '/lpq command/d' \
		-e '/lprm command/d' /etc/smb.conf- >/etc/smb.conf
	fi

# This should really be a function, but anyway...
	lynx -source http://lpr.sourceforge.net/installers/installer-${DIST}.gz > /tmp/gnulpr-installer.gz
	#gunzip /tmp/gnulpr-installer.gz
	chmod +x /tmp/gnulpr-installer.gz
	exec /tmp/gnulpr-installer.gz

    else
      echo Packages are currently only available for mandrake 7.x 
      NO_PKGS
    fi
elif [ -f /etc/redhat-release ];then
    if  grep '6\.[0-2]' /etc/redhat-release ;then
    	if grep '6\.[0-1]' /etc/redhat-release ; then
		echo "************************** NOTICE ****************************"
		echo "*** You are installing this on a Red Hat 6.0 or 6.1 system ***"
		echo "*** It has only been tested to work with Red Hat 6.2       ***"
		echo "*** It is unlikely that you should experience any problems ***"
		echo "*** but we recommend that you upgrade to 6.2 or higher     ***"
		echo "*** before proceeding.                                     ***"
		echo "***   Hit ^C to cancel, or enter to continue               ***"
		echo "**************************************************************"
		read i < /dev/tty
	fi
	DIST="redhat-62-i386"
	if rpm -q rhs-printfilters 2>/dev/null; then
	    if grep -v '^$\|^\#' /etc/printcap; then
		echo "*** NOTICE ***"
		echo "	If you have currently working printers set up using"
		echo "rhs-printfilters, they will cease to function shortly."
		echo "The package rhs-printfilters will be removed to "
		echo "make way for the gnulpr printfilters."
		echo
		echo "[Press Enter to continue, or ^C to abort]"

		read i  < /dev/tty
	    fi
	fi

	lynx -source http://lpr.sourceforge.net/installers/installer-${DIST}.gz > /tmp/gnulpr-installer.gz
	chmod +x /tmp/gnulpr-installer.gz
	exec /tmp/gnulpr-installer.gz

    fi
fi

if test "x$DIALOG" = "x"; then
	#RETVAL=1
	#if test "x$DISPLAY" != "x"; then
		#DIALOG=`which gdialog`
		#RETVAL=$?
	#fi

	#if test $RETVAL -ne 0; then
		DIALOG=`which whiptail`
		if test $? -ne 0; then
			DIALOG=`which dialog`
			if test $? -ne 0; then
				echo "Could not find gdialog, dialog, or whiptail." 
				exit 1
			fi
		fi
	#fi
	export DIALOG
fi
TEMPFILE=`mktemp -q /tmp/downloader.XXXXXX`


$DIALOG --title "Protocol"  --menu 'Which protocol do you plan on using?' 9 65 2 "ftp://" "File Transfer Protocol" "http://" "HyperText Transfer Protocol (unofficial repository only)" 2> $TEMPFILE < /dev/tty

RETVAL=$?

case $RETVAL in
  0)
    PROTOCOL=`cat $TEMPFILE`;;
  1)
    exit 1;;
  255)
    exit 1;;
  *)
    exit 1;;
esac

case $PROTOCOL in
  ftp://)
	DEFAULTLOC="lpr.sourceforge.net/pub/lpr/hp-release-1_0/"
	export DEFAULTLOC;;
  *)
  	DEFAULTLOC= "";;
esac

$DIALOG --title "Location"  --inputbox "Where is the repository?" 9 65 "$DEFAULTLOC" 2> $TEMPFILE < /dev/tty

RETVAL=$?

case $RETVAL in
  0)
    TOP_LEVEL=`cat $TEMPFILE`;;
  1)
    exit 1;;
  255)
    exit 1;;
  *)
    exit 1;;
esac

rm -f $TEMPFILE


if [ -f /etc/redhat-release ];then
    if  grep '7\.0' /etc/redhat-release ;then
	DIST="redhat-7.0"
	LPR_RPM=""
	if rpm -q rhs-printfilters 2>/dev/null; then
	    if grep -v '^$\|^\#' /etc/printcap; then
		echo "*** NOTICE ***"
		echo "	If you have currently working printers set up using"
		echo "rhs-printfilters, they will cease to function shortly."
		echo "The package rhs-printfilters is about to be removed to "
		echo "make way for the gnulpr printfilters."
		echo
		echo "[Press Enter to continue, or ^C to abort]"

		read i  < /dev/tty
		echo -n "Removing rhs-printfilters..."
	    fi
	    rpm -q printtool 2>/dev/null && rpm -e printtool
	    rpm -e rhs-printfilters
	    echo " Done."
	fi
    else
      echo Packages are currently only available for Redhat 6.x and 7.0
      NO_PKGS
    fi
elif [ -f /etc/SuSE-release ];then
    rpm --nodeps -e lprold
    if  grep '6\.4' /etc/SuSE-release ;then
	DIST="suse-6.4"
	# how backward can you get?
	#rpm -Uvh ${PROTOCOL}lpr.sourceforge.net/extras/suse-6.4/cvs.rpm
    elif  grep '7\.0' /etc/SuSE-release ;then
	DIST="suse-7.0"
    else
      echo Packages are currently only available for SuSE 6.4 and 7.0
      NO_PKGS
    fi
elif [ -f /etc/turbolinux-release ];then
    if  grep '6\.1' /etc/turbolinux-release ;then
	DIST="turbo-6.1"

	RPMOPTS="--oldpackage $RPMOPTS"
	export RPMOPTS
	if rpm -q vine-printfilters 2>/dev/null; then
	    if grep -v '^$\|^\#' /etc/printcap; then
		echo "*** NOTICE ***"
		echo "	If you have currently working printers set up using"
		echo "vine-printfilters, they will cease to function shortly."
		echo "The package rhs-printfilters is about to be removed to "
		echo "make way for the gnulpr printfilters."
		echo
		echo "[Press Enter to continue, or ^C to abort]"

		read i  < /dev/tty
		echo -n "Removing rhs-printfilters..."
	    fi
	    rpm -q printconfig 2>/dev/null && rpm -e printconfig
	    rpm -e vine-printfilters
	    echo " Done."
	fi
	# GAR
	# rpm in turbo seems to balk at http loads.  sux0r
	#lynx -dump http://lpr.sourceforge.net/pub/lpr/extras/${DIST}/wget.rpm  > /tmp/wget.rpm && rpm -Uvh /tmp/wget.rpm
	#lynx -dump http://lpr.sourceforge.net/pub/lpr/extras/turbo-6.1/cvs.rpm > /tmp/cvs.rpm && rpm -Uvh /tmp/cvs.rpm
	if grep parport_lowlevel /etc/modules.conf ; then
		echo "...parallel port pre-configured."
	else
		echo "*** NOTICE ***"
		echo "Your system does not appear to have the appropriate alias"
		echo "for the low-level parallel port drivers.  In order to rectify"
		echo "this, we will now add a line to /etc/modules.conf which reads"
		echo "'alias parport_lowlevel parport_pc'.  If you do not wish for"
		echo "this to occur, please hit ^C now.  Otherwise, hit Enter or"
		echo "Return to continue."

		read i < /dev/tty
		echo 'alias parport_lowlevel parport_pc' >> /etc/modules.conf
		depmod -a
	fi
    else
      echo Packages are currenty only available for TurboLinux 6.1
      NO_PKGS
    fi
else
    NO_PKGS
fi

which wget &> /dev/null
if test $? -ne 0; then
	echo "Installing wget..."
	#lynx -dump http://lpr.sourceforge.net/extras/${DIST}/wget.rpm > wget-${DIST}.rpm
	lynx -source ${PROTOCOL}lpr.sourceforge.net/pub/lpr/extras/${DIST}/wget.rpm  > /tmp/wget.rpm && rpm -Uvh /tmp/wget.rpm
	#rpm -Uvh ${PROTOCOL}lpr.sourceforge.net/extras/${DIST}/wget.rpm 
fi

#wget -c --passive-ftp -r ${PROTOCOL}${TOP_LEVEL}/${DIST}/RPMS/

#let's try -m for mirroring.
wget --no-parent -c --passive-ftp -m ${PROTOCOL}${TOP_LEVEL}/${DIST}/RPMS/
#wget -m ${PROTOCOL}${TOP_LEVEL}/${DIST}/RPMS/

TOP_LEVEL=`echo $TOP_LEVEL | sed 's/~/%7E/g'`
find $TOP_LEVEL/$DIST/RPMS/ -name '*.rpm' -print | xargs rpm -Uvh $RPMOPTS
if test $? -ne 0; then
	echo "*** ERROR INSTALLING RPMS ***"
	exit 1
else
	echo "installed."
fi

# yeah, yeah, ugly hack.
test -x /etc/rc.d/init.d/lpd && /etc/rc.d/init.d/lpd restart && exit 0
test -x /etc/init.d/lpd && /etc/init.d/lpd restart && exit 0



# If the distribution is Mandrake, then all the RPMS are *.i686.rpm
#if test "x${DIST}" = "xmandrake-7.2"; then
  #FILE_LIST=`echo "$FILE_LIST" | sed -e 's/i386/i686/g'`
#fi

# let RPM sort out the order of the RPM's dependancies hence the xargs
#for i in $FILE_LIST $LPR_RPM; do 
    #echo $TOP_LEVEL/$DIST/$i
#done | xargs rpm -Uvh 

