#!/bin/sh
# @(#) $Header: option6solaris,v 1.11 93/02/22 15:07:31 ss2 Exp $

set -a

INTFDIR=/etc/lp/interfaces
MODELDIR=/usr/spool/lp/model
SPOOLDIR=/etc/lp/printers

SPOOLNAME=$1
PERIPHNAME=$1

if [ ! -f $HPNP/hpnpf ]
then
  echo ""
  echo "The spooler software must not have been installed since"
  echo "$HPNP/hpnpf does not exist.  Install the NPI"
  echo "spooler software before configuring the spooler."
  echo ""
  $ECHO "Press the return key to return to the main menu ... $NL"
  read RESP
  exit 1
fi

# Get the Queue name 
# Only use the first label if we have a domain name
#
SPOOLNAME=`echo $SPOOLNAME | awk -F. '{print $1}'`

if [ -d $INTFDIR ]
then
  echo "Currently configured printers in ${INTFDIR}:"
  echo "-----------------------------------------------"
  PERIPHS=`ls $INTFDIR | grep -v model.orig`
  if [ "$PERIPHS" = "" ]
  then
    echo "(no printer has been configured)"
  else
    ls $INTFDIR | grep -v model.orig | more
  fi
fi

while [ 0 ]
do
   getspoolname  "option6solaris" "$SPOOLNAME" ""
   if [ $? != 0 ]
   then 
     exit
   fi

   SPOOLNAME=`cat $TMP/SPOOLNM`
   rm $TMP/SPOOLNM

     #  Check if this printer is already set up in spooler.
   if [ -f $INTFDIR/model.orig/$SPOOLNAME ]
   then
      echo " "
      echo "$SPOOLNAME exists already. Try another name! "
      SPOOLNAME=""
   else
      break;
   fi
done


getname "option6solaris" "$PERIPHNAME"
if [ "$?" != "0" ]
then
  exit 1
fi
PERIPHNAME=`cat $TMP/NAME`
XTNAME=`cat $TMP/NAME`
rm -f $TMP/NAME


        #
	# Get port  number
	# eg is it a serial or parallel printer

if [ ${ADMINDIR}x = "x" ]
then
  # ADMINDIR should be set already, - if not then set it to default value
  ADMINDIR=/usr/bin/X11/700X/admin
fi

$ADMINDIR/whichport
if [ $? != 0 ]
then
  exit 100
else
  PERIPHPORT=`cat $TMP/PERIPHPORT`
  rm  $TMP/PERIPHPORT
fi



if [ ! -d $INTFDIR/model.orig ]
then
  echo "Creating subdirectory $INTFDIR to hold the original model script" >> $LOG
  echo "" >> $LOG
  echo mkdir $INTFDIR/model.orig >> $LOG
  mkdir $INTFDIR/model.orig
  echo chown lp $INTFDIR/model.orig >> $LOG
  chown lp $INTFDIR/model.orig
  echo chgrp bin $INTFDIR/model.orig >> $LOG
  chgrp bin $INTFDIR/model.orig
  echo "" >> $LOG
fi


while [ 0 ]
do
  echo  ""
  cat $HPNP/cfg/option6solarisinfo
  echo  ""
  $ECHO "Enter model script name: ('*' to see choices ) $NL"  
  read MODEL
  if [ -z "$MODEL" ]
  then
    continue
  fi
  case "$MODEL" in
    q)  exit 1 ;;
    \?) echo "" 
	echo "Enter '*' to list the directory of model files.  Once you have"
        echo "entered a model script name, the configuration will continue."
	echo ""
	$QUITHELP
	;;
    \*) echo ""
        ls -FC $MODELDIR ;;
    *) if [ -f "$MODELDIR/$MODEL" ]
       then
	 if grep /usr/lib/plotdvr "$MODELDIR/$MODEL" >/dev/null 2>&1
	 then
	   echo ""
	   echo "This model script uses /usr/lib/plotdvr and this filter is"
	   echo "incompatible with a network plotter.  Choose a different"
	   echo "model script.  The model script \"dumbplot\" may be an"
	   echo "acceptable alternative."
	 else
	   break
	 fi
       else
	 echo ""
	 echo "Model $MODELDIR/$MODEL does not exist."
       fi 
  esac
done


# If model name starts with net_..., allow them to change default here
XLANG=""
OK=""
if [ $MODEL = "net_lj4x.sol.xt" -o $MODEL = "net_lj3plus.sol.xt" -o $MODEL = "net_dsnj.sol.xt" ]
then
  while [ 0 ]
  do
    echo ""
    echo "The $MODEL default is AUTO for langauage switching between PCL/HPGL and"
    echo "PostScript.  PostScript files must have the standard header in order to" 
    echo "be recognized correctly in this AUTO mode."
    echo "\nDo you want to change the default to a specific language?"
    if [ $MODEL != "net_dsnj.sol.xt" ]
    then 
         echo "(ps/pcl/auto, default=auto ) $NL"
    else
         echo "(ps/hpgl/hpgl2/auto,  default=auto ) $NL"
    fi

    read OK
	if [ "$OK" = 'q' ]
	then
         exit 1
	fi
    if [ "$OK" = "" -o "$OK" = "auto" ]
    then
       break
    fi
    if [ "$OK" = "ps" ]
    then
       XLANG="PS" 
       break
    fi
    if [ "$OK" = "pcl" -a "$MODEL" != "net_dsnj.sol.xt" ]
    then
       XLANG="PCL"
       break
    fi
    if [ "$OK" = "hpgl" -a "$MODEL" = "net_dsnj.sol.xt" ]
    then
       XLANG="HPGL"
       break
    fi
    if [ "$OK" = "hpgl2" -a "$MODEL" = "net_dsnj.sol.xt" ]
    then
         XLANG="HPGL2"
         break
    fi
  done
fi


echo  ""
echo "Ready to shut down the spooling system, configure the new destination,"
echo "and start the spooling system again.  When the spooling system is shut"
echo "down all printing will stop.  Any print requests that are currently"
echo "printing will be reprinted in their entirety when the spooling system"
echo "is restarted."

while [ 0 ]
do
  echo ""
  $ECHO "OK to continue? (y/n/q default=y) $NL"
  read OK
  if [ "$OK" = "" -o "$OK" = 'y' ]
  then
    break
  fi
  if [ "$OK" = 'n' -o "$OK" = 'q' ]
  then
    exit 1
  fi
  if [ "$OK" = '?' ]
  then
    echo ""
    echo "Enter \"y\" to add the spooler destination."
    echo ""
    echo "Enter \"q\" or \"n\" to return to the main menu."
  fi
done

#
# Create the filter definition file in /etc/lp/fd
#

if [ -f "$INTFDIR/$SPOOLNAME" ]
then
	echo "Re-creating the filter definition file in /etc/lp/fd for $SPOOLNAME"
	echo /usr/lib/reject $SPOOLNAME | tee -a $LOG
	/usr/lib/reject $SPOOLNAME >/dev/null
	echo /usr/bin/disable $SPOOLNAME | tee -a $LOG
	/usr/bin/disable $SPOOLNAME >/dev/null
fi

addfilterdef $SPOOLNAME

echo "" | tee -a $LOG
echo "Adding $SPOOLNAME to spooler" >> $LOG
echo "" >> $LOG

STARTLP=0
echo /usr/lib/lpshut | tee -a $LOG
if /usr/lib/lpshut
then
  STARTLP=1
else
  echo "/usr/lib/lpshut: scheduler not running" >> $LOG
fi

# Change the language setting and move the model to model.orig
if [ -n "$XLANG" ]
then
  sed -e "/^PERSONALTY/s/AUTO/$XLANG/" $MODELDIR/$MODEL > $INTFDIR/model.orig/$SPOOLNAME

else 
  cp $MODELDIR/$MODEL $INTFDIR/model.orig/$SPOOLNAME
fi
  echo cp $MODELDIR/$MODEL $INTFDIR/model.orig/$SPOOLNAME | tee -a $LOG


echo chown lp $INTFDIR/model.orig/$SPOOLNAME | tee -a $LOG
chown lp $INTFDIR/model.orig/$SPOOLNAME 

echo chgrp bin $INTFDIR/model.orig/$SPOOLNAME | tee -a $LOG
chgrp bin $INTFDIR/model.orig/$SPOOLNAME

echo chmod 755 $INTFDIR/model.orig/$SPOOLNAME | tee -a $LOG
chmod 755 $INTFDIR/model.orig/$SPOOLNAME
sync

echo /usr/lib/lpadmin -p$SPOOLNAME -v/dev/null -m$MODEL $CLASSOPT | tee -a $LOG
/usr/lib/lpadmin -p$SPOOLNAME -v/dev/null -m$MODEL $CLASSOPT

if [ "$PERIPHNAME" != "$SPOOLNAME" ]
then
  echo sed -e "s/^PERIPH=/PERIPH=$PERIPHNAME/" $HPNP/hpnp.model.sol \> $INTFDIR/$SPOOLNAME | tee -a $LOG
  sed -e "s/^PERIPH=/PERIPH=$PERIPHNAME/" $HPNP/hpnp.model.sol > $INTFDIR/$SPOOLNAME
else
  echo cp $HPNP/hpnp.model.sol $INTFDIR/$SPOOLNAME | tee -a $LOG
  cp $HPNP/hpnp.model.sol $INTFDIR/$SPOOLNAME
fi

#
# edit model wrapper to communicate with
# the right X Station port
# UDP port 9100 = printer connected to serial port, 9101 = parallel
# 9102 = second serial port
#

if [ "$PERIPHPORT" = "9101" ]
then
  sed -e 's/-x $PERIPH/-x $PERIPH -p 9101/g' $INTFDIR/$SPOOLNAME > $TMP/tmp.$$
else
  if [ "$PERIPHPORT" = "9102" ]
  then
    sed -e 's/-x $PERIPH/-x $PERIPH -p 9102/g' $INTFDIR/$SPOOLNAME > $TMP/tmp.$$
  else
    sed -e 's/-x $PERIPH/-x $PERIPH -p 9100/g' $INTFDIR/$SPOOLNAME > $TMP/tmp.$$
  fi
fi

cp $TMP/tmp.$$ $INTFDIR/$SPOOLNAME
rm $TMP/tmp.$$



if [ "$STARTLP" = "1" ]
then
  echo /usr/lib/lpsched | tee -a $LOG
  /usr/lib/lpsched
else
  echo scheduler was not restarted | tee -a $LOG
fi

echo /usr/bin/enable $SPOOLNAME | tee -a $LOG
/usr/bin/enable $SPOOLNAME >> $LOG

echo /usr/lib/accept $SPOOLNAME | tee -a $LOG
/usr/lib/accept $SPOOLNAME >> $LOG

if [ "$CLASS" != "" ]
then
  echo /usr/lib/accept $CLASS | tee -a $LOG
  /usr/lib/accept $CLASS >> $LOG
fi

echo "" >> $LOG

echo ""
echo "The spooler destination $SPOOLNAME has been added to the spooler."

# for default printer
while [ 0 ]
do
  echo ""
  $ECHO "Do you want this printer queue to be the system default? (y/n default=n) $NL"
  read OK
  if [ "$OK" = 'n' -o "$OK" = "" ]
  then
    exit 1
  fi
  if [ "$OK" = 'y' ]
  then
    /usr/lib/lpadmin -d$SPOOLNAME
    break
  fi
  if [ "$OK" = '?' ]
  then
    echo ""
    echo "Enter \"y\" if you want it to be the default."
    echo ""
    echo "Enter \"n\" or press <return> otherwise "
  fi
done


$ECHO "Press the return key to continue ... $NL"
read RESP
exit 0

