#!/bin/sh

#############################################
#
# RICOH UNIX Printing Solution
# COPYRIGHT (C) 1999-2009 RICOH COMPANY,LTD.
# All rights reserved
#
#############################################

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

pconfig() {
if [ -z "$1" ]
then
menu="paper pitch orientation punch staple duplex ln"
paper=letter
mediatype=auto
pitch=10
itray=auto
outbin=
duplex=off
edgetoedge=off
vmi=8
ln=
compress=
usercode=
else
cat << Config > "$1"
paper=$paper
mediatype=$mediatype
pitch=$pitch
itray=$itray
outbin=$outbin
duplex=$duplex
edgetoedge=$edgetoedge
vmi=$vmi
ln=$ln
compress=$compress
usercode=$usercode
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"
	    exit 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
		:
	else
	    $myecho "\"$PRINTER_NAME\" not found"
	    exit 1
	fi
	;;

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

SCO )
	if [ -f "/usr/spool/lp/admins/lp/interfaces/$PRINTER_NAME" ]
	then
		:
	else
		$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


cat << SelectSetting

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

  1. Paper Size=$paper
  2. Media Type=$mediatype
  3. Font Size=$pitch
  4. Input Tray=$itray
  5. Duplex=$duplex
  6. Edge to Edge=$edgetoedge
  7. VMI=$vmi
  8. Form Line=$ln
  9. Compress Font=$compress
 10. 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 "Media Type=\c"
	read mediatype
	;;
3 )
	$myecho "Font Size=\c"
	read pitch
	;;
4 )
	$myecho "Input Tray=\c"
	read itray
	;;
5 )
	$myecho "Duplex=\c"
	read duplex
	;;
6 )
	$myecho "Edge to Edge=\c"
	read edgetoedge
	;;
7 )
	$myecho "VMI=\c"
	read vmi
	;;
8 )
	$myecho "VMI must be 0!"
	$myecho "Form Line=\c"
	read ln
	$myecho "VMI=\c"
	read vmi
	;;
9 )
	$myecho "Compress Font=\c"
	read compress
	;;	
10 )
    $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
