#!/sbin/sh
#@(#)HP X Station Admin Scripts
#
# getspoolname	get the printer name
#

OPTION=$1
DEFAULT=$2
TYPE=$3

case $OS in

  hp)
    if [ "$TYPE" = "class" ]
    then
      DEFAULT=""
    else
      echo $DEFAULT | grep '^[a-zA-Z0-9_]*$' > /dev/null
      VALID=$?
      if [ "$VALID" -eq 0 -a -n "$DEFAULT" ]
      then
        # Name can be only 14 chars or less
        LENGTH=`expr "$DEFAULT" : '.*'`
        if [ "$LENGTH" -gt 14 ]
        then
          VALID=1
        fi
      fi
      if [ "$VALID" -ne 0 ]
      then
        DEFAULT=""
      fi
    fi
    ;;
  sun4)
    ;;
esac

while [ 0 ] 
do
  if [ -n "$DEFAULT" ]
  then
    echo ""
    $ECHO "Enter name of printer [q|?] (default=$DEFAULT): $NL"
  else
    echo ""
    if [ "$TYPE" = "class" ]
    then
      $ECHO "Enter the spooler class for the peripheral to join (optional): $NL"
    else
      $ECHO "Enter name of printer to remove [q|?] : $NL"
    fi
  fi
  read SPOOLNM
  if [ "$SPOOLNM" = "q" ]
  then
    exit 100
  fi
  if [ "$SPOOLNM" = "" ]
  then
    if [ "$TYPE" = "class" ]
    then
      exit 2
    fi
    if [ -n "$DEFAULT" ]
    then
      SPOOLNM=$DEFAULT
      break
    else
      continue
    fi
  fi
  if [ "$SPOOLNM" = "?" ]
  then
    case $OPTION in
      option6hp)
	if [ "$TYPE" = "class" ]
	then
          echo ""
	  echo "A class defines a group of peripherals.  A peripheral class can be"
	  echo "used as a print destination instead of a peripheral name.  The first"
	  echo "available peripheral in the peripheral class will print the next"
	  echo "job queued to that peripheral class.  If no peripheral class is"
	  echo "entered, the peripheral will not be added to any class."
	else
	  cat <<EOF

Enter the <name> to be used as the destination in an "lp -d<name>" command.
The printer name can match the X station's name, but it does not have to.
For example, the X station name could be "george" and the printer name
could be "laser1".

EOF
	fi
	cat <<EOF

The name is limited to 14 characters and must consist entirely
of the characters A-Z, a-z, 0-9, and _ (underscore).

EOF
        continue
	;;
      option6sun)
	cat <<EOF

Enter the printer <name> to be used in an "lpr -P<name>" command.
The printer name can match the X station's name, but it does not have to.
For example, the X station name could be "george" and the printer name
could be "laser1".

EOF
        ;;
      option8hp)
        echo ""
        echo "Enter the name used as the destination in an \"lp\" command."
	echo "This destination is removed from the print spooler."
	echo ""
	;;
      option8sun)
	echo ""
        echo "Enter the <name> used in an \"lpr -P<name>\" command."
	echo "The /etc/printcap entry for this peripheral will be removed.  You"
	echo "will be asked if the spool directory for this peripheral should"
	echo "also be removed."
	echo ""
	;;
    esac
    continue
  fi
  case $OPTION in
    option6hp|option8hp)
      echo "$SPOOLNM" | grep '^[a-zA-Z0-9_]*$' > /dev/null
      VALID=$?
      if [ "$VALID" -eq 0 ]
      then
        # Name can be only 14 chars or less
        LENGTH=`expr "$SPOOLNM" : '.*'`
        if [ "$LENGTH" -gt 14 ]
        then
          VALID=1
        fi
      fi
      if [ "$VALID" -ne 0 ]
      then
        echo ""
        echo "$SPOOLNM is an invalid name.  The name is limited to 14"
        echo "characters and must consist entirely of the characters A-Z,"
        echo "a-z, 0-9, and _ (underscore)."
	echo ""
	continue
      fi
      ;;
    option6sun|option8sun)  # no limits
      ;;
  esac
  break
done

echo "$SPOOLNM" > $TMP/SPOOLNM
exit 0
