#!/bin/sh

#############################################
##
## Ricoh UNIX Printing Solution
## 
##
## Version 11.0  04/29/2004
## Copyright 1999-2004
## Ricoh Engineering
##
#############################################

#############################################
##
## For debugging, uncomment below: set -x
##
#############################################
#set -x

#############################################
##
## 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

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

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

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

PRINTER_NAME=
PRINTER_CONFIG_PATH=/var/spool/lpd/$printer/
PRINTER_CONFIG=$PRINTER_CONFIG_PATH/$printer.config
Version=
OS_VERSION=
OS_NAME=
OS_TYPE=
exit_code=0

getvmi() {
rvmi=
if [ -z "$ln" ]; then
	rvmi=
elif [ "$orientation" = "landscape" ]; then
	rvmi=`echo $ln|awk '{printf("%.2f",(8.5-1)*48/$1)}'`
else
case $paper in
letter)
	rvmi=`echo $ln|awk '{printf("%.2f",(11-1)*48/$1)}'`
	;;
legal)
	rvmi=`echo $ln|awk '{printf("%.2f",(14-1)*48/$1)}'`
	;;
*)
	ln=
	;;
esac
fi
}

pconfig() {
if [ -z "$1" ]
then
menu="paper pitch orientation punch staple duplex ln"
paper=letter
pitch=10
itray=auto
color=yes
duplex=off
ln=
usercode=
else
getvmi
outbin=
if [ -n "$staple" -a "$staple" != "off" ];then
	outbin="shift2"
fi
if [ -n "$punch" -a "$punch" != "off" ];then
	outbin="shift2"
fi

cat << Config > "$1"
paper=$paper
pitch=$pitch
itray=$itray
color=$color
duplex=$duplex
ln=$ln
usercode=$usercode
rvmi=$rvmi
Config
fi
}

pconfig

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

OS_NAME=`uname -s`

case $OS_NAME in
SunOS)
	OS_VERSION=`uname -r`
	OS_TYPE="SYSV"
	OS_VERSION="Solaris"
	Version=`uname -sr`
	;;
HP-UX)
	OS_VERSION="HPUX"
	OS_TYPE="SYSV"
	Version="HP-UX `uname -r`"
	;;
AIX)
	OS_VERSION="AIX"
	OS_TYPE="SYSV"
	Version="IBM AIX `uname -v`.`uname -r`"
	;;
Linux)
	OS_VERSION="LINUX"
	OS_TYPE="BSD"
	Version="LINUX"
	;;
SCO_SV)
	OS_VERSION="SCO"
	OS_TYPE="BSD"
	Version="SCO OpenServer `uname -v`"
	;;
FreeBSD)
	OS_VERSION="FreeBSD"
	OS_TYPE="BSD"
	Version="FreeBSD `uname -r`"
	;;
*)
	;;
esac

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 or upper
  3. HP-UX
  4. IBM AIX
  5. RedHat Linux
  6. SCO OpenServer
  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="AIX" ;;
5) OS_VERSION="LINUX" ;;
6) OS_VERSION="SCO" ;;
7) OS_VERSION="FreeBSD" ;;
*) $myecho "Unsupported UNIX version. Exiting ...."
	exit 1 ;;
esac
fi

#############################################
##
## Get printer queue
##
#############################################

if [ -n "$1" ]
then
	PRINTER_NAME=$1
else
	$myecho "Please enter the printer queue: \c"
	read PRINTER_NAME
fi

if [ -z "$PRINTER_NAME" ]
then
	$myecho "No printer queue specified. Exiting"
	exit 1
fi

#############################################
##
## Printer Queue Config
##
#############################################


case $OS_VERSION in

AIX)
	if ! grep -w "$PRINTER_NAME:" /etc/qconfig > /dev/null
	then
	    $myecho "\"$PRINTER_NAME\" not found"
	    error_code=1
	fi

	;;

LINUX)
	if [ -e /etc/printcap.local ]
	then
		PRINTCAP=/etc/printcap.local
	else
		PRINTCAP=/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=/etc/printcap
	fi

	if test ! -f $PRINTCAP
	then
		$myecho 'Could not find "printcap" file'
		$myecho "please enter the full path of printcap file name: \c"
		read $PRINTCAP
	fi

	if ! grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" not found"
		exit 1
	fi

	;;

FreeBSD)
	PRINTCAP=/etc/printcap

	if test ! -f $PRINTCAP
	then
		$myecho 'Could not find "printcap" file'
		$myecho "please enter the full path of printcap file name: \c"
		read $PRINTCAP
	fi

	if ! grep "^$PRINTER_NAME[|:]\{0,1\}[\\]\{0,1\}$" $PRINTCAP > /dev/null
	then
		$myecho "\"$PRINTER_NAME\" not found"
		exit 1
	fi

	;;

Solaris)
	if ! grep -w "$PRINTER_NAME:" /etc/printers.conf > /dev/null
	then
	    $myecho "\"$PRINTER_NAME\" not found"
	    exit 1
	fi
	;;

HPUX)
	if [ ! -f "/etc/lp/interface/$PRINTER_NAME" ]
	then
		$myecho "\"$PRINTER_NAME\" not found"
		exit 1
	fi
	;;

SCO)
	if [ ! -f "/usr/spool/lp/admins/lp/interfaces/$PRINTER_NAME" ]
	then
		$myecho "\"$PRINTER_NAME\" not found or no filter attached"
		exit 1
	fi
	;;

*)
	$myecho "$OS_VERSION not supported. Exiting ...."
	exit 1
	;;
esac

if [ -d /var/spool/lpd ]
then
	PRINTER_CONFIG_PATH=/var/spool/lpd/$PRINTER_NAME/
	PRINTER_CONFIG=$PRINTER_CONFIG_PATH/$PRINTER_NAME.config
else
	mkdir -p /var/spool/lpd
	PRINTER_CONFIG_PATH=/var/spool/lpd/$PRINTER_NAME/
	PRINTER_CONFIG=$PRINTER_CONFIG_PATH/$PRINTER_NAME.config
fi

if [ -f "$PRINTER_CONFIG" ]
then
	. "$PRINTER_CONFIG"
fi

while true; do
getvmi

cat << SelectSetting

Please select settings.
Note: the value have to be the same as defined in the UserManual

  1. Paper Size=$paper 
  2. Font Size=$pitch
  3. Input Tray=$itray
  4. Color=$color
  5. Duplex=$duplex
  6. Form Line=$ln
  7. User Code=$usercode

  q. Quit without saving
  e. Save and exit
  r. Reset
  f. Reset to factory settings

SelectSetting

$myecho "Select setting: \c"
read Setting

case $Setting in

1)
	$myecho "Paper Size=\c"
	read paper 
	;;
2)
	$myecho "Font Size=\c"
	read pitch
	;;

3)      $myecho "Input Tray=\c"
	read itray
	;;

4)
	$myecho "Color=\c"
	read color
	;;

5)
	$myecho "Duplex=\c"
	read duplex
	;;
6)
	$myecho "Form Lines=\c"
	read ln
	;;
7)   
        $myecho "User Code=\c"
        read usercode
	if test -n "$usercode";then 
        	usercode=`expr $usercode : '\([0-9]\{0,8\}\)'`
	fi 
        ;;
q|Q)
	exit 0
	;;
e|E)
	break
	;;
r|R)
	pconfig
	if [ -f "$PRINTER_CONFIG" ]
	then
		. "$PRINTER_CONFIG"
	fi
	;;
f|F)
	pconfig
	;;
esac
done

if [ ! -d "$PRINTER_CONFIG_PATH" ]
then
	mkdir "$PRINTER_CONFIG_PATH"
fi
pconfig "$PRINTER_CONFIG"

exit $exit_code
# end of configuration script
