#!/bin/sh
#############################################
##                                           
## Ricoh UNIX Printing Solution           
## Installation Program                    
##
## Version 11.2  12/14/2004
## Copyright 1999-2004
## Ricoh Engineering
##
#############################################
#############################################
##
## For debugging, uncomment below: set -x
##
#############################################
#set -x

#############################################
##
## Select Brand Here
## Supported brands are: Ricoh, Lanier, Savin
##         Infotec, Gestetner, Rex-Rotary and Nashuatec
##
#############################################

BRAND="Ricoh"
SUPPORT_CONTACT="For support, please contact $BRAND Technical Support"

cat << Trademark

#############################################
##
## $BRAND UNIX Printing Solution
## Installation Program
##
## $SUPPORT_CONTACT
##
#############################################

Trademark

#############################################
##
## Version History
##
##
#############################################

#############################################
##
## Create myecho command based on UNIX version
## For BSD UNIX myecho="echo -e",
## For SYS V UNIX myecho="echo"
##
#############################################

myecho="echo"
test X`echo -e` = X && myecho="echo -e"

#############################################
##
## Check if user is logged in as root
##
#############################################

umask 022

if test "`id|sed 's/(.*$//'`" != "uid=0"
then
     $myecho "You must have root privileges to run this script. Exiting ...."
     exit 1
fi

#############################################
##
## End User License Agreement
##
#############################################
more EULA

while true; do
$myecho "Do you agree ? [y/n] \c"

read EULA_CONFIRM

case "$EULA_CONFIRM" in
	Y|y)
		break;;
	N|n)
		$myecho "Sorry."
		exit 1;;
esac
done

#############################################
##
## Message to user
##
#############################################

$myecho "\nIf you want to terminate execution of this program,\
	\npress CTRL+C (or Delete key for SCO OpenServer) at any time."


##
## Initialize variables
##
#############################################

PORTNAME="text"
PRODUCT_NAME="Aficio CL3000e/CL3100N/CL2000"
DOS_AVAILABLE=Y
PRINTER_SCRIPT="sol_3000"
FILTER_TMP="filter_tmp"


Product=
Version=
OS_VERSION=
OS_NAME=
OS_TYPE=

#############################################
##
## Detect / select OS Version
##
#############################################

OS_NAME=`uname -s`

case $OS_NAME in
SunOS)
	OS_VERSION=`uname -r`
	OS_TYPE="SYSV"

	if [ "$OS_VERSION" = 5.6 -o "$OS_VERSION" = 5.7 -o "$OS_VERSION" = 5.8 -o "$OS_VERSION" = 5.9 ]
	then
		OS_VERSION="Solaris2.6"
		Version="Sun Solaris Version `uname -r`"
	else
		OS_VERSION="Solaris2.5"
		Version="Sun Solaris Version `uname -r`"
	fi
	;;
HP-UX)
	OS_VERSION="HPUX"
	OS_TYPE="SYSV"
	Version="HP-UX `uname -r`"
	;;
AIX)
	OS_VERSION="AIX43"
	OS_TYPE="SYSV"
	Version="IBM AIX `uname -v`.`uname -r`"
	;;
Linux)
	OS_VERSION="LINUX"
	OS_TYPE="BSD"
	if grep 'Red Hat' /etc/issue > /dev/null
	then
		Version="Red Hat Linux"
	elif grep SuSE /etc/issue > /dev/null
	then
		Version="SuSE Linux"
	elif grep Mandrake /etc/issue > /dev/null
	then
		Version="Mandrake Linux"
	else
		Version="LINUX"
	fi
	;;
SCO_SV)
	OS_VERSION="SCO"
	OS_TYPE="BSD"
	Version="SCO OpenServer `uname -v`"
##################################
#
# Make a filter for SCO. 08/08/03
#
##################################

        cat sco_head $PRINTER_SCRIPT sco_tail > $FILTER_TMP
	;;
FreeBSD)
	OS_VERSION="FreeBSD"
	OS_TYPE="BSD"
	Version="FreeBSD `uname -r`"
	;;
*)
	;;
esac

while true; do
if [ -n "$Version" ];
then

	$myecho "\nYour system appears to be running $Version.\nIs this correct ? [y/n] \c"
	read OS_CONFIRM
	case "$OS_CONFIRM" in
	Y|y|N|n)
		break;;
	esac
fi
done

if [ "$OS_CONFIRM" = N -o "$OS_CONFIRM" = n -o -z "$Version" ];
then

cat << SelectVersion

Please select your UNIX version.

  1. Sun Solaris 2.4 or 2.5
  2. Sun Solaris 2.6, 7, 8, 9
  3. HP-UX 10.x or 11.x
  4. IBM AIX 4.3 or 5.1
  5. RedHat Linux 6.x, 7.x or 8.x
  6. SCO OpenServer 5.0.6
  7. FreeBSD
  8. Other

SelectVersion

$myecho "Select <1-8>: \c"
read Version

case $Version in

1) OS_VERSION="Solaris2.5" ;;
2) OS_VERSION="Solaris2.6" ;;
3) OS_VERSION="HPUX" ;;
4) OS_VERSION="AIX43" ;;
5) OS_VERSION="LINUX" ;;
6) OS_VERSION="SCO" ;;
7) OS_VERSION="FreeBSD" ;;
*) $myecho "Unsupported UNIX version. Exiting ...."
	exit 1 ;;
esac
fi

#############################################
##
## While loop until user confirms entry
##
#############################################
page=1

while true; do

HOSTNAME=
QNAME=
PRINTER_NAME=
DEVICE_OPTION=N


#############################################
##
## Enter hostname of printer
##
#############################################

$myecho "\nEnter printer's hostname."

$myecho "\nThis hostname MUST exist in your hosts file or"
$myecho "in naming service such as DNS, NIS, etc.\n"

$myecho "Hostname: \c"
read HOSTNAME

if test -z "$HOSTNAME"
then $myecho "Invalid hostname. Exiting ...."
     exit 1
fi

#############################################
##
## Enter printer name
##
## PRINTER_NAME will be used as local printer name for DOS
## QNAME will be used for Network Installation Support
##
#############################################

$myecho "\nPlease enter printer name."

if [ "$OS_VERSION" = HPUX ]
then
	$myecho "(Maximum characters allowed for the printer name is 11) "
	$myecho "(The characters must be letters, digits and underscores) \c"
elif [ "$OS_VERSION" = AIX43 ]
then
	$myecho "(Maximum characters allowed for the printer name is 16) \c"
elif [ "$OS_VERSION" = Solaris2.6 -o "$OS_VERSION" = Solaris2.5 ]
then
        $myecho "(Maimum characters allowed for the printer name is 14)" 

fi

$myecho "\nThis is the printer you will print to [Default = lp_${HOSTNAME}]: \c"
read PRINTER_NAME

####################################
#
# Test Pringer Name with Host Name
#
####################################
if [ "$PRINTER_NAME" = "$HOSTNAME" ]
then
	$myecho "Printer Name cannot be same as a Host Name, Please make a re-install"
	exit 1
fi

if test -z "$PRINTER_NAME"
then
	PRINTER_NAME=lp_${HOSTNAME}
fi

if [ "$OS_VERSION" = HPUX ]
then
	NUM_LEN=${#PRINTER_NAME}
	if [ "$NUM_LEN" -gt 11 ]
	then
		$myecho "The queue name is $NUM_LEN long.... \c"
		$myecho "Invalid queue name. Exiting ...."
     		exit 1
	fi
	if expr match "$PRINTER_NAME" '^[0-9,A-Z,a-z,_]*$' >/dev/null
	then
		$myecho " \c"
	else
		$myecho "Invalid queue name. Exiting ...."
		exit 1
	fi
elif [ "$OS_VERSION" = AIX43 ]
then
	NUM_LEN=${#PRINTER_NAME}
	if [ "$NUM_LEN" -gt 16 ]
	then
		$myecho "The queue name is $NUM_LEN long.... \c"
		$myecho "Invalid queue name. Exiting ...."
     		exit 1
	fi
elif [ "$OS_VERSION" = Solaris2.6 -o "$OS_VERSION" = Solaris2.5 ]
then
	if [ "`$myecho $PRINTER_NAME | wc -c`" -gt 15 ]
	then
		$myecho "The queue name is over 14 characters.\c"
		$myecho "Invalid queue name. Exiting ...."
		exit 1
	fi
elif [ "$OS_VERSION" = LINUX ]
then
	if test `expr "$PRINTER_NAME" : '[0-9,A-Z,a-z,_,-,.]*'` -ne ${#PRINTER_NAME}
	then
		$myecho "Invalid queue name. Exiting ...."
		exit 1
	fi
elif [ "$OS_VERSION" = FreeBSD ]
then
	if ! expr "$PRINTER_NAME" : '^[0-9,A-Z,a-z,_,-,.]*$' >/dev/null
	then
		$myecho "Invalid queue name. Exiting ...."
		exit 1
	fi
fi

QNAME=${PRINTER_NAME}

#############################################
##
## If Device Option Support is available,
## ask if it needs to be installed
##
#############################################

if [ "$OS_VERSION" = Solaris2.5 -o "$OS_VERSION" = Solaris2.6 \
	-o "$OS_VERSION" = HPUX -o "$OS_VERSION" = AIX43 -o "$OS_VERSION" = SCO ];
then

	if [ "$DOS_AVAILABLE" = Y ];
	then
		while true; do
		$myecho "Would you like to install Device Option Support? [y/n] \c"
		read DOS

		case "$DOS" in
			Y|y)
				DEVICE_OPTION=Y
				break;;
			N|n)
				break;;
		esac
		done
	fi
fi

if [ "$OS_VERSION" = LINUX ];
then
	if rpm -qa | grep [Ll][Pp][Rr]ng > /dev/null 2>&1
	then
	  if [ "$DOS_AVAILABLE" = Y -a -x `which file` ];
	  then
		while true; do
		$myecho "Would you like to install Device Option Support? [y/n] \c"
		read DOS
		case "$DOS" in
			Y|y)
				DEVICE_OPTION=Y
				break;;
			N|n)
				break;;
		esac
		done
	  fi
	fi

	if rpm -qa | grep printconf  > /dev/null
	then
		PRINTCAP_DFLT=/etc/printcap.local
	elif rpm -qa | grep redhat-config-printer  > /dev/null
	then
		PRINTCAP_DFLT=/etc/printcap.local
	else
		PRINTCAP_DFLT=/etc/printcap
	fi
########################################
#
# If it is RedHat Enterprise version
# then set PRINTCAP_DFLT=/etc/printcap
# update 05/12/2004
#
########################################
	if uname -r | grep EL > /dev/null
	then 
		PRINTCAP_DFLT=/etc/printcap
	fi
		FPIDIR_DFLT=/usr/share/linux_filters

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME

	if [ "$DEVICE_OPTION" = Y ];
	then
#		FPIPTR=$SPOOLDIR/filter
		$myecho "\nPlease specify printer filter installation directory if necessary."
		$myecho " [Default = ${FPIDIR_DFLT} ]: \c"
		read FPIDIR
		if test -z "$FPIDIR"
		then
			FPIDIR=$FPIDIR_DFLT
		fi
		#LNXFPI=$SPOOLDIR/$PRINTER_NAME.filter
		LNXFPI=$FPIDIR/$PRINTER_NAME.filter
	fi

	$myecho "\nPlease specify location of printcap if necessary."
	$myecho " [Default = ${PRINTCAP_DFLT} ]: \c"
	read PRINTCAP
	if test -z "$PRINTCAP"
	then
		PRINTCAP=$PRINTCAP_DFLT
	fi
fi

if [ "$OS_VERSION" = FreeBSD ];
then
	$myecho "Checking for LPRng..."
	if pkg_info -a | grep LPRng > /dev/null 2>&1
	then
	  if [ "$DOS_AVAILABLE" = Y -a -x `which file` ];
	  then
		while true; do
		$myecho "Would you like to install Device Option Support? [y/n] \c"
		read DOS
		case "$DOS" in
			Y|y)
				DEVICE_OPTION=Y
				break;;
			N|n)
				break;;
		esac
		done
	  fi
	fi

	FPIDIR_DFLT=/usr/local/libexec/filters
	PRINTCAP_DFLT=/etc/printcap

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME

	if [ "$DEVICE_OPTION" = Y ];
	then
		$myecho "\nPlease specify printer filter installation directory if necessary."
		$myecho " [Default = ${FPIDIR_DFLT} ]: \c"
		read FPIDIR
		if test -z "$FPIDIR"
		then
			FPIDIR=$FPIDIR_DFLT
		fi
		LNXFPI=$FPIDIR/$PRINTER_NAME.filter
	fi

	$myecho "\nPlease specify location of printcap if necessary."
	$myecho " [Default = ${PRINTCAP_DFLT} ]: \c"
	read PRINTCAP
	if test -z "$PRINTCAP"
	then
		PRINTCAP=$PRINTCAP_DFLT
	fi
fi

#############################################
##
## Confirm entry
##
#############################################
while true; do

case $OS_VERSION in

LINUX)
cat << LN_ConfirmEntry

#################################################

  Product Name    	: $PRODUCT_NAME
  Printer Hostname	: $HOSTNAME
  Printer Name    	: $PRINTER_NAME
  Device Option Support : $DEVICE_OPTION
  Filter Directory      : $FPIDIR
  Printcap File         : $PRINTCAP

#################################################

LN_ConfirmEntry
;;

FreeBSD)
cat << LN_ConfirmEntry

#################################################

  Product Name    	: $PRODUCT_NAME
  Printer Hostname	: $HOSTNAME
  Printer Name    	: $PRINTER_NAME
  Device Option Support : $DEVICE_OPTION
  Filter Directory      : $FPIDIR
  Printcap File         : $PRINTCAP

#################################################

LN_ConfirmEntry
;;

*)
cat << ConfirmEntry

#################################################

  Product Name    	: $PRODUCT_NAME
  Printer Hostname	: $HOSTNAME
  Printer Name    	: $PRINTER_NAME
  Device Option Support : $DEVICE_OPTION

#################################################

ConfirmEntry
;;
esac


$myecho "Is this correct ? [y/n] \c"

read CONFIRMED
case "$CONFIRMED" in
	Y|y|N|n)
		break;;
esac
done

if [ "$CONFIRMED" = Y -o "$CONFIRMED" = y ]
then
	break
fi

#############################################
##
## End of While loop
##
#############################################

done

#############################################
##
## Modify the script with the QNAME
## QNAME is used only if DEVICE_OPTION=Y
##      QNAME=rp_{$PRINTER_NAME}
##
############################################

if [ "$DEVICE_OPTION" = Y ]
then
	QNAME=rp_${PRINTER_NAME}
	if [ "$OS_VERSION" = SCO ]
	then

#######################
# SCO filter:
# change to QNAME on filter
# 08/08/2003
#######################
		sed "s/REMOTE_PRINTER=/REMOTE_PRINTER=$QNAME/" $FILTER_TMP | \
		sed 's"/usr/bin/rm"/bin/rm"' > /tmp/$FILTER_TMP
	else
		sed "s/REMOTE_PRINTER=/REMOTE_PRINTER=$QNAME/" $PRINTER_SCRIPT > /tmp/$PRINTER_SCRIPT
	fi
fi

#############################################
##
## Create printer entry
##
## The filter is put in:
##	/etc/lp/interfaces/		in Solaris
##	/etc/lp/interface/		in HP-UX
##	/usr/local/bin/myfilters/	in AIX by default
##	/usr/share/linux-filters/	in Linux by default
##	/usr/local/libexec/filters/	in FreeBSD by default
##
#############################################

$myecho "\nCreating a printer entry, please stand by..."

case $OS_VERSION in

Solaris2.5)
	/usr/lib/lpsystem -t bsd -R 0 $HOSTNAME
	/usr/lib/lpadmin -p $QNAME -s $HOSTNAME!$PORTNAME -T unknown -I any
	/usr/lib/accept $QNAME
	/usr/bin/enable $QNAME
	
	if [ "$DEVICE_OPTION" = Y ]
	then
#########################################
#
# merge Three filtes, then install filter
#
#########################################
	cat ./sun_head ./$PRINTER_SCRIPT ./sun_tail > $FILTER_TMP
	sed "s/PRINTER_HOSTNAME=/PRINTER_HOSTNAME=$HOSTNAME/"  $FILTER_TMP > /tmp/$FILTER_TMP
		/usr/lib/lpadmin 	-p $PRINTER_NAME \
					-i /tmp/$FILTER_TMP \
					-T unknown \
					-I any \
					-v /dev/null \
					-A none

		/usr/lib/accept $PRINTER_NAME
		/usr/bin/enable $PRINTER_NAME
	fi
	;;

Solaris2.6)
	/usr/lib/lpsystem -t bsd -R 0 $HOSTNAME
	/usr/sbin/lpadmin -p $QNAME -s $HOSTNAME!$PORTNAME -T unknown -I any

	if [ "$DEVICE_OPTION" = Y ]
	then
#############################################
#
#  Merge Three files, then install filter 
#
#############################################
	cat ./sun_head ./$PRINTER_SCRIPT ./sun_tail > $FILTER_TMP
	sed "s/PRINTER_HOSTNAME=/PRINTER_HOSTNAME=$HOSTNAME/"  $FILTER_TMP > /tmp/$FILTER_TMP
		/usr/lib/lpadmin 	-p $PRINTER_NAME \
					-i /tmp/$FILTER_TMP \
					-T unknown \
					-I any \
					-v /dev/null \
					-A none

		/usr/lib/accept $PRINTER_NAME
		/usr/bin/enable $PRINTER_NAME
	fi
	;;

HPUX)
	if [ -f "/etc/lp/interface/$PRINTER_NAME" ]
	then
		$myecho "\"$PRINTER_NAME\" already there, overwrite ? [y/n] \c"
		read CONFIRMED
		if [ "$CONFIRMED" != Y -a "$CONFIRMED" != y ]
		then
			exit 1
		fi
		./uninstall $PRINTER_NAME > /dev/null
		sleep 1
	fi

	/usr/lib/lpshut

	/usr/lib/lpadmin -p$QNAME -v/dev/null -mrmodel -orm$HOSTNAME \
		-orp$PORTNAME -ob3
	if [ "$?" -ne 0 ]
	then
		$myecho "Installation cannot be done at this time, check your system and try again"
                /usr/lib/lpsched
		exit "$?"
	fi
	/usr/lib/accept $QNAME
	/usr/bin/enable $QNAME

	if [ "$DEVICE_OPTION" = Y ]
	then
########################
#
# Make a filter for HP. Install filter
# 08/08/2003
######################
	        cat ./hp_head ./$PRINTER_SCRIPT ./hp_tail > /tmp/$FILTER_TMP
		/usr/lib/lpadmin -p$PRINTER_NAME \
			-v/dev/null \
			-i/tmp/$FILTER_TMP \
			-orm$HOSTNAME \
			-orp$PORTNAME \
			-ob3
		if [ "$?" -ne 0 ]
		then
			$myecho "Installation cannot be done at this time, check your system and try again"
                        /usr/lib/lpsched
			exit "$?"
		fi
		/usr/lib/accept $PRINTER_NAME
		/usr/bin/enable $PRINTER_NAME
	fi

	/usr/lib/lpsched
	;;

AIX43)
	AIX_FILTER_DIR=/usr/local/bin/myfilters
	if test ! -d $AIX_FILTER_DIR
	then
		mkdir -p $AIX_FILTER_DIR
	fi
	if [ "$DEVICE_OPTION" = Y ]
	then
		DEV_NAME=${PRINTER_NAME}.dev
		AIX_FILTER=${AIX_FILTER_DIR}/${PRINTER_NAME}.filter
		RMT_PRINTER_NAME=${PRINTER_NAME}.rmt
		RMT_DEV_NAME=${DEV_NAME}.rmt
		cat ./aix_head > $AIX_FILTER
		awk 'BEGIN {i=0}; /LNX_TUB_E/ {exit} /LNX_TUB_B/ {i=1}
			 { if(i==1) print $0 };' $PRINTER_SCRIPT >> $AIX_FILTER
		echo "RMT_PRINTER_NAME=${RMT_PRINTER_NAME}" >> $AIX_FILTER 
		cat ./aix_tail >> $AIX_FILTER
		chown root:printq $AIX_FILTER
		chmod 755 $AIX_FILTER
		/usr/sbin/piomisc_base mkpq_other -q$RMT_PRINTER_NAME -d$RMT_DEV_NAME -b/usr/lib/lpd/rembak -u 'TRUE' -h$HOSTNAME -r$PORTNAME
		if [ "$?" -ne 0 ]
		then
			exit "$?"
		fi
		/usr/sbin/piomisc_base mkpq_other -q$PRINTER_NAME -d$DEV_NAME -b$AIX_FILTER -u 'TRUE' -h$HOSTNAME -r$PORTNAME
	else
		/usr/lib/lpd/pio/etc/piomisc_ext mkpq_remote_ext -q$PRINTER_NAME -h$HOSTNAME -r$PORTNAME -t'aix' -C'FALSE' -d$PRODUCT_NAME
	fi

	;;

LINUX)
	if grep "^$PRINTER_NAME:" $PRINTCAP 2> /dev/null > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" already there, overwrite ? [y/n] \c"
		read CONFIRMED
		if [ "$CONFIRMED" != Y -a "$CONFIRMED" != y ]
		then
			exit 1
		fi
		./uninstall $PRINTER_NAME > /dev/null
		sleep 1
	fi

	if test ! -d $SPOOLDIR
	then
		mkdir $SPOOLDIR
		#chgrp lp $SPOOLDIR
		chown lp:lp $SPOOLDIR
		chmod 700 $SPOOLDIR
	fi

	if test ! -d $FPIDIR
	then
		mkdir $FPIDIR
		chmod 755 $FPIDIR
	fi

	if test ! -f $PRINTCAP
	then
		touch $PRINTCAP
	fi

	cat /dev/null > printcap.tmp
#	echo '##PRINTTOOL3## REMOTE' > printcap.tmp
        echo "$PRINTER_NAME:\\" > printcap.tmp
	echo "	:sd=$SPOOLDIR:\\" >> printcap.tmp
	echo "	:mx#0:\\" >> printcap.tmp
	echo "	:sh:\\"  >> printcap.tmp
	echo "	:rm=$HOSTNAME:\\"  >> printcap.tmp

	if [ "$DEVICE_OPTION" = Y ]
	then
		cat lnx_head > $LNXFPI
		awk 'BEGIN {i=0}; /LNX_TUB_E/ {exit} /LNX_TUB_B/ {i=1}
			 { if(i==1) print $0 };' $PRINTER_SCRIPT >> $LNXFPI
		case "$PRINTER_SCRIPT" in
		./sol_eb105*)
			cat lnx_tail.efi >> $LNXFPI ;;
		*)
			cat lnx_tail >> $LNXFPI ;;
		esac
		chmod 755 $LNXFPI
		#chown lp:lp $LNXFPI
#		ln -s $LNXFPI $FPIPTR

		echo "	:mc=1:\\"  >> printcap.tmp
		echo "	:if=$LNXFPI:\\"  >> printcap.tmp
	else
		echo "	:mc=0:\\"  >> printcap.tmp
	fi
	echo "	:lpd_bounce=true:\\" >> printcap.tmp
	echo "	:rp=$PORTNAME:" >> printcap.tmp

	cat printcap.tmp >> $PRINTCAP

	if [ -x "/etc/rc.d/lpd" ]
	then
		if /etc/rc.d/lpd status > /dev/null
		then
			/etc/rc.d/lpd restart
		else
			/etc/rc.d/lpd start
		fi
	elif [ -x "/etc/rc.d/init.d/lpd" ]
	then
		if /etc/rc.d/init.d/lpd status > /dev/null
		then
			/etc/rc.d/init.d/lpd restart
		else
			/etc/rc.d/init.d/lpd start
		fi
	else
		$myecho "\nPlease make sure to restart lpd daemon, otherwise it will not print."
	fi

	;;

SCO)

	PRINTCAP=/etc/printcap
	PATH=/bin:/usr/bin:/etc:/etc/conf/bin
	WORKDIR=/usr/lpd/local
	SPOOL=/usr/spool
	LPD=/usr/spool/lpd
	SOURCE=/usr/lib/tcprt/rlp
	LP_FILES="/usr/bin/lp /usr/bin/cancel /usr/bin/lpstat"
	LPADMIN=/usr/spool/lp/admins/lp/printers
	remote_exists=0
	SIGINT=2
#############################################
##
## Enable Remote Print Service for SCO
##
#############################################

enable_remote_print_service() {

    ############check and reset the envirement#####################   
	if [ -f $SOURCE/install_1 ]
	then
		rm -rf $WORKDIR $LPD $PRINTCAP
	fi
	
	if [ -f $SOURCE/install_2 ]
	then
		for i in lp lpr lpstat cancel
		do
			cp $WORKDIR/$i /usr/bin/$i
			chown bin:lp /usr/bin/$i
			chmod 2111 /usr/bin/$i
		done
		cp $WORKDIR/lpmove /usr/lib/lpmove
		chown bin:lp /usr/lib/lpmove
		chmod 2111 /usr/lib/lpmove
		
		rm -rf $WORKDIR $LPD $PRINTCAP
	fi

    ##################install remote print service##############
	trap "" $SIGINT

	if  [  -d "$WORKDIR" ] ; then
		lf $WORKDIR/* > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			remote_exists=1
#			exit 0
		fi
	fi
	
	if [ "$remote_exists" = 0 ]; then
	    touch $SOURCE/install_1

	    # ensure directory path is searchable
	    if  [ ! -d "$WORKDIR" ] ; then
		    a=`umask`
		    umask 022
		    mkdir -p $WORKDIR 
		    umask $a
	    fi
	    if  [ ! -d "$SPOOL" ] ; then
		    mkdir $SPOOL
		    chmod 775 $SPOOL
		    chown root $SPOOL
		    chgrp bin $SPOOL
	    fi
	    if  [ ! -d "$LPD" ] ; then
		    mkdir $LPD
		    chmod 775 $LPD
		    chown root $LPD
		    chgrp daemon $LPD
	    fi
	    if  [ ! -w "$PRINTCAP" ] ; then
		    echo "# Remote Line Printer (BSD format)"> $PRINTCAP
		    chmod 644 $PRINTCAP
		    chown root $PRINTCAP
		    chgrp daemon $PRINTCAP
	    fi

	    # Save old rlp commands
	    if [ ! -f /tmp/tcprt_upgrade -a $remote_exists -eq 0 ]; then
		    for i in lp lpr lpstat cancel; do
			    if [ ! -x "$WORKDIR/$i" ]; then
					    cp /usr/bin/$i $WORKDIR
					    chown bin:lp $WORKDIR/$i
					    chmod 2111 $WORKDIR/$i
			    fi
		    done
		    if [ ! -x "$WORKDIR/lpmove" ]; then
			    cp /usr/lib/lpmove $WORKDIR
			    chown bin:lp $WORKDIR/lpmove
			    chmod 2111 $WORKDIR/lpmove
		    fi
	    fi

	    rm $SOURCE/install_1
	    touch $SOURCE/install_2

	    # Install new lp commands
	    for i in $LP_FILES /usr/lib/lpmove
	    do
		    base=`basename $i`
		    cp $SOURCE/$base $i
	    done

	    rm $SOURCE/install_2
	    ###enable lp daeman
	    /usr/lib/lpd
        fi
}
##########End of enable_remote_print_service################

	if grep "^$QNAME:" $PRINTCAP 2> /dev/null > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" already there, overwrite ? [y/n] \c"
		read CONFIRMED
		if [ "$CONFIRMED" != Y -a "$CONFIRMED" != y ]
		then
			exit 1
		fi
		./uninstall $PRINTER_NAME > /dev/null
	fi

	if test ! -d /usr/spool/lpd
	then
		mkdir /usr/spool/lpd
	fi

	SPOOLDIR=/usr/spool/lpd/$QNAME

	if test ! -d $SPOOLDIR
	then
		mkdir $SPOOLDIR
		chgrp daemon $SPOOLDIR
	fi

	if test ! -f $PRINTCAP
	then
		touch $PRINTCAP
	fi

	cat <<- PCAP_END >> $PRINTCAP

	$QNAME:\\
	        :lp=:rm=$HOSTNAME:rp=$PORTNAME:sd=$SPOOLDIR:mx#0:
	PCAP_END

	if [ "$DEVICE_OPTION" = Y ]
	then
		/usr/lib/lpshut
		/usr/lib/lpadmin -p$PRINTER_NAME \
			-v/dev/null \
			-i/tmp/$FILTER_TMP
		/usr/lib/lpsched
		/usr/lib/accept $PRINTER_NAME
		/usr/bin/enable $PRINTER_NAME
	fi
        enable_remote_print_service
	;;

FreeBSD)

	PRINTCAP=/etc/printcap

	if grep "^$PRINTER_NAME:" $PRINTCAP 2> /dev/null > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" already there, overwrite ? [y/n] \c"
		read CONFIRMED
		if [ "$CONFIRMED" != Y -a "$CONFIRMED" != y ]
		then
			exit 1
		fi
		./uninstall $PRINTER_NAME > /dev/null
	fi

	SPOOLDIR=/var/spool/lpd/$PRINTER_NAME
	#LOGFILE=$SPOOLDIR/logfile
	LOGFILE=/var/log/lpd-errs

	if test ! -d $SPOOLDIR
	then
		mkdir $SPOOLDIR
		chown daemon $SPOOLDIR
		chgrp daemon $SPOOLDIR
	fi

	if test ! -d $FPIDIR
	then
		mkdir $FPIDIR
		chmod 755 $FPIDIR
	fi

	if test ! -f $LOGFILE
	then
		touch $LOGFILE
		chown daemon $LOGFILE
		chgrp daemon $LOGFILE
	fi

	if test ! -f $PRINTCAP
	then
		touch $PRINTCAP
	fi

 	if [ "$DEVICE_OPTION" = Y ]
	then
	        cat ./sco_head ./$PRINTER_SCRIPT ./sco_tail > $FILTER_TMP
		cat lnx_head > $LNXFPI
		awk 'BEGIN {i=0}; /LNX_TUB_E/ {exit} /LNX_TUB_B/ {i=1}
			 { if(i==1) print $0 };' $PRINTER_SCRIPT >> $LNXFPI
		case "$FILTER_TMP" in
		*sol_eb105*)
		sed 's/csplit -q -f $ps_tmp $ps_tmp "\/^setup\/"/split -p "^setup" $ps_tmp $ps_tmp/g' lnx_tail.efi | \
		sed 's/csplit -q -f $ps_tmp $ps_tmp "\/%%EndSetup\/"/split -p "%%EndSetup" $ps_tmp $ps_tmp/g' | \
		sed 's/$ps_tmp"00"/$ps_tmp"aa"/g' | sed 's/$ps_tmp"01"/$ps_tmp"ab"/'g >> $LNXFPI ;;
		*)
		sed 's/csplit -q -f $ps_tmp $ps_tmp "\/^setup\/"/split -p "^setup" $ps_tmp $ps_tmp/g' lnx_tail | \
		sed 's/csplit -q -f $ps_tmp $ps_tmp "\/%%EndSetup\/"/split -p "%%EndSetup" $ps_tmp $ps_tmp/g' | \
		sed 's/$ps_tmp"00"/$ps_tmp"aa"/g' | sed 's/$ps_tmp"01"/$ps_tmp"ab"/'g >> $LNXFPI ;;
		esac
		chmod 755 $LNXFPI

	        echo "$PRINTER_NAME:\\" > printcap.tmp
		echo "	:rm=$HOSTNAME:\\"  >> printcap.tmp
		echo "	:rp=$PORTNAME:\\"  >> printcap.tmp
		echo "	:sd=$SPOOLDIR:\\" >> printcap.tmp
		echo "	:lf=$LOGFILE:\\"  >> printcap.tmp
		echo "	:if=$LNXFPI:\\"  >> printcap.tmp
		echo "	:lpd_bounce=true:\\" >> printcap.tmp
		echo "	:mx#0:" >> printcap.tmp
		cat printcap.tmp >> $PRINTCAP

		/usr/local/sbin/checkpc -f > /dev/null 2>&1
#	$myecho "\nPlease make sure to restart lpd daemon, otherwise it will not print."

	else
		cat <<- PCAP_END >> $PRINTCAP

		$PRINTER_NAME:\\
	        	:rm=$HOSTNAME:\\
		        :rp=$PORTNAME:\\
		        :sd=$SPOOLDIR:\\
		        :lf=$LOGFILE:\\
	        	:sh:\\
		        :mx#0:
		PCAP_END
	fi

	killall lpd
	if grep 'lpd_enable="YES"' /etc/rc.conf > /dev/null
	then
		/usr/sbin/lpd
	else
		/usr/local/sbin/lpd
	fi
	;;

*)
	$myecho "Error creating a printer entry. Exiting ...."
	exit 1
	;;
esac

#############################################
##
## Check if the printer entry is created successfuly
##
#############################################

if test $? = 0; then
	$myecho "Printer entry \"$PRINTER_NAME\" created successfully."
else
	$myecho "Error creating a printer entry. Exiting ...."
	exit 1
fi

#############################################
##
## Message to user
##
#############################################

$myecho "\nYou can verify the installation by issuing the following command."

case $OS_TYPE in 

SYSV)
	if [ "$OS_VERSION" = AIX43 ]
	then
		$myecho "	qprt -P$PRINTER_NAME your_file"
	else
		$myecho "	lp -d $PRINTER_NAME your_file"
	fi
	;;
BSD)
	if [ "$OS_VERSION" = SCO ]
	then
		$myecho "	lp -d$PRINTER_NAME your_file"
	elif [ "$OS_VERSION" = FreeBSD -a "$DEVICE_OPTION" = Y ]
	then
		$myecho "	/usr/local/bin/lpr -P$PRINTER_NAME your_file"
	else
		$myecho "	lpr -P$PRINTER_NAME your_file"
	fi
	;;
esac	

#############################################
##
## Copy uninstall to /usr/bin
##
#############################################

cp -f ./uninstall /usr/bin/ric_uninstall
chmod 755 /usr/bin/ric_uninstall

SAMPLES=SampleFiles
if [ -d "$SAMPLES" ]
then
	if [ ! -d "/usr/share" ]; then mkdir -p /usr/share; fi
	cp -rf $SAMPLES /usr/share/unixfilter$SAMPLES
fi

########################
#
# Copy Ricoh man page 
#
########################

if [ -d "/usr/share/man/man1" ]; then
	for i in "*.1"; do cp -f *.1 /usr/share/man/man1; done;
fi

#############################################
##
## Remove temporary file
##
#############################################

if [ "$OS_VERSION" = LINUX -o "$OS_VERSION" = FreeBSD -o "$OS_VERSION" = SCO ]
then
	/bin/rm -f printcap.tmp
	/bin/rm -rf /tmp/${PRINTER_SCRIPT}
else
if test -f /tmp/${PRINTER_SCRIPT}
then
	/usr/bin/rm -rf /tmp/${PRINTER_SCRIPT}
fi
if test -f /tmp/${FILTER_TMP}
then
	/usr/bin/rm -rf /tmp/${FILTER_TMP}
fi
if test -f ./${FILTER_TMP}
then
	/usr/bin/rm -rf ./${FILTER_TMP}
fi
fi

#############################################
##
## Configure queue
##
#############################################

if [ -f "rpconfig" ]; then
	cp -f ./rpconfig /usr/bin/rpconfig
	chmod 755 /usr/bin/rpconfig
	$myecho "\nDo you want to set up the default values for this queue now? [Y/n] \c"
	read CONFIG_CONFIRM
	case "$CONFIG_CONFIRM" in
	Y|y)
		rpconfig $PRINTER_NAME
		;;
	*)
	$myecho "\n You can set up this queue by run \"rpconfig\" <queue_name> ."
		;;
	esac
fi
# end of installation script			
