#!/bin/sh

trapit ()
{
  jobs -x kill -TERM %?
  rm -f $1 $TMP1 $TMP2
  trap -
  reset
  exit 2
}

TMPOUT=`mktemp /tmp/lprfaxcommands.XXXXXX`
INPT=""

TITLE="Fax setup"
USERNAME=`whoami`
REPORT_TO="${USERNAME}@`hostname -f`"
FROM=`grep \`whoami\` /etc/passwd`
FROM=${FROM%%':/'*}
FROM=${FROM##*':'}
COVERPAGE="yes"
SPACER1="------------------------------------------------------"
SPACER2="---------"
if [ -e ~/.lprfaxrc ]
  then
    . ~/.lprfaxrc
  fi

RESOLUTION="fine"
TO=""
TO_FAX=""
TO_FAX_OUT=""
TO_FAX_TMP=""
TO_FAX_LEN=0
TO_FAX_CNT=0
POLL="yes"
TIME="now"
NOTES="yes"
declare -a NOTELINES

function prepit ()
{
  echo "START_FAX_HEADER"
  echo "USERNAME=\"$USERNAME\""
  test "$FROM" && echo "FROM=\"$FROM\""
  test "$REPORT_TO" && echo "REPORT_TO=\"$REPORT_TO\""
  test "$COVERPAGE" && echo "COVERPAGE=\"$COVERPAGE\""
  test "$POLL" && echo "POLL=\"$POLL\""
  test "$RESOLUTION" && echo "RESOLUTION=\"$RESOLUTION\""
  test "$TO" && echo "TO=\"$TO\""
  test "$TO_FAX" && echo "TO_FAX=\"$TO_FAX_TMP\""
  test "$TIME" && echo "TIME=\"$TIME\""
  test "$NOTES" && test "$COVERPAGE" == "yes" && echo "NOTES=\"$NOTES\""
  if [ "$NOTES" == "yes" ] && [ "$COVERPAGE" == "yes" ]
    then
      CNT=1
      while [ $CNT -le 18 ]
        do
          test "${NOTELINES[$CNT]}" && echo "NOTELINES[$CNT]=\"${NOTELINES[$CNT]}\""
          let CNT+=1
        done
    fi
  echo "END_FAX_HEADER"
}

function saveit ()
{
  test "$FROM" && echo "FROM=\"$FROM\""
  test "$REPORT_TO" && echo "REPORT_TO=\"$REPORT_TO\""
  test "$COVERPAGE" && echo "COVERPAGE=\"$COVERPAGE\""
  test "$POLL" && echo "POLL=\"$POLL\""
  test "$RESOLUTION" && echo "RESOLUTION=\"$RESOLUTION\""
  test "$NOTES" && echo "NOTES=\"$NOTES\""
}

function mainmenu ()
{
  while true
    do
      if [ "$COVERPAGE" == "no" ]
      then
        dialog --clear --title "$TITLE" --menu "" 24 78 16 \
Submit "to the queue now" Keep "personal data" "$SPACER2" "$SPACER1" From \
"$FROM" Report "to $REPORT_TO" Coverpage "$COVERPAGE" Poll "for documents \
$POLL" "$SPACER2" "$SPACER1" To "$TO" Fax "number $TO_FAX" \
Resolution "$RESOLUTION" When "$TIME (HH:MM or now)" 2>$TMPOUT
        ret=$?
      else
        if [ "$NOTES" == "no" ]
          then
            dialog --clear --title "$TITLE" --menu "" 24 78 16 \
Submit "to the queue now" Keep "personal data" "$SPACER2" "$SPACER1" From \
"$FROM" Report "to $REPORT_TO" Coverpage "$COVERPAGE" Poll "for documents \
$POLL" "$SPACER2" "$SPACER1" To "$TO" Fax "number $TO_FAX" \
Resolution "$RESOLUTION" When "$TIME (HH:MM or now)" Notes "$NOTES" \
2>$TMPOUT
            ret=$?
          else
            dialog --clear --title "$TITLE" --menu "" 24 78 16 \
Submit "to the queue now" Keep "personal data" "$SPACER2" "$SPACER1" From \
"$FROM" Report "to $REPORT_TO" Coverpage "$COVERPAGE" Poll "for documents \
$POLL" "$SPACER2" "$SPACER1" To "$TO" Fax "number $TO_FAX" \
Resolution "$RESOLUTION" When "$TIME (HH:MM or now)" Notes "$NOTES" \
"$SPACER2" "$SPACER1" Line1 "${NOTELINES[1]}" Line2 "${NOTELINES[2]}" \
Line3 "${NOTELINES[3]}" Line4 "${NOTELINES[4]}" Line5 "${NOTELINES[5]}" \
Line6 "${NOTELINES[6]}" Line7 "${NOTELINES[7]}" Line8 "${NOTELINES[8]}" \
Line9 "${NOTELINES[9]}" Line10 "${NOTELINES[10]}" Line11 "${NOTELINES[11]}" \
Line12 "${NOTELINES[12]}" Line13 "${NOTELINES[13]}" Line14 "${NOTELINES[14]}" \
Line15 "${NOTELINES[15]}" Line16 "${NOTELINES[16]}" Line17 "${NOTELINES[17]}" \
Line18 "${NOTELINES[18]}" 2>$TMPOUT
            ret=$?
          fi
        fi
      if [ $ret -ne 0 ]
        then
          break
        fi
      read INPT < $TMPOUT
      case $INPT
        in
           "From" )        dialog --title "$TITLE" --inputbox \
"Please enter your name" 8 50 "$FROM" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read FROM < $TMPOUT
                           fi
                         ;;
           "To" )        dialog --title "$TITLE" --inputbox \
"Please the name of the recipient" 8 50 "$TO" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read TO < $TMPOUT
                           fi
                         ;;
           "Fax" )        dialog --title "$TITLE" --inputbox \
"Please enter the recipients fax number" 8 50 "$TO_FAX" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read INPT < $TMPOUT
                             TO_FAX_CNT=0;
                             TO_FAX_LEN=${#INPT}
                             while [ $TO_FAX_CNT -lt $TO_FAX_LEN ]
                               do
                                 case ${INPT:$TO_FAX_CNT:1}
                                   in
                 [[:digit:]] )    TO_FAX_TMP=${TO_FAX_TMP}${INPT:$TO_FAX_CNT:1}
                                  ;;
                 ['('')''-'' '] ) ;;
                 * )              TO_FAX_TMP="ERROR"
                                  break
                                  ;;
                                   esac
                                 let TO_FAX_CNT+=1;
                               done
                             if [ "$TO_FAX_TMP" != "ERROR" ]
                               then
                                 TO_FAX="${INPT}"
                                 TO_FAX_OUT="${TO_FAX_TMP}"
                               fi
                           fi
                         ;;
           "Report" )    dialog --title "$TITLE" --inputbox \
"Please enter your email for status reports" 10 50 "$REPORT_TO" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read REPORT_TO < $TMPOUT
                           fi
                         ;;
           "Submit" )    if [ "$TO" ]&& [ "$TO_FAX" ]&& [ "REPORT_TO" ]&& \
[ "FROM" ]
                           then
                             break
                           else
                             dialog --title "$TITLE" --msgbox "You need to "\
"fill in the report email,\nYour name,\nthe recipient name and fax number" 7 40
                           fi
                         ;;
           "Keep" )      saveit > ~/.lprfaxrc
                         ;;
           "Coverpage" ) if [ "$COVERPAGE" == "yes" ]
                           then
                             yy="on" ; oo="off"
                           else
                             yy="off" ; oo="on"
                           fi 
                         dialog --title "$TITLE" --radiolist \
"Do you wish a cover page ?" 9 40 2 yes "" $yy no "" $oo 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read COVERPAGE < $TMPOUT
                           fi
                         ;;
           "Poll" )      if [ "$POLL" == "yes" ]
                           then
                             yy="on" ; oo="off"
                           else
                             yy="off" ; oo="on"
                           fi 
                         dialog --title "$TITLE" --radiolist \
"Do you wish to poll for documents ?" 9 40 2 yes "poll" $yy no "dont poll" \
$oo 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read POLL < $TMPOUT
                           fi
                         ;;
           "Notes" )     if [ "$NOTES" == "yes" ]
                           then
                             yy="on" ; oo="off"
                           else
                             yy="off" ; oo="on"
                           fi 
                         dialog --title "$TITLE" --radiolist \
"Do you wish to put notes on the cover page ?" 9 54 2 yes "" $yy no "" $oo \
2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read NOTES < $TMPOUT
                           fi
                         ;;
           "Line"* )     NUM=${INPT##Line}
                         dialog --title "$TITLE" --inputbox \
"Please enter notes line $NUM" 8 50 "${NOTELINES[$NUM]}" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read NOTELINES[$NUM] < $TMPOUT
                           fi
                         ;;
           "Resolution" )if [ "$RESOLUTION" == "fine" ]
                           then
                             yy="on" ; oo="off"
                           else
                             yy="off" ; oo="on"
                           fi 
                         dialog --title "$TITLE" --radiolist \
"Do you wish to poll for documents ?" 9 40 2 fine "" $yy normal "" \
$oo 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read RESOLUTION < $TMPOUT
                           fi
                         ;;
           "When"* )     dialog --title "$TITLE" --inputbox \
"When do you wish to send the fax (HH:MM or now)?" 8 60 "${TIME}" 2>$TMPOUT
                         if [ $? -eq 0 ]
                           then
                             read TIME < $TMPOUT
                           fi
                         ;;
           * )           ;;
         esac
    done
  return $ret
}

trap trapit EXIT

if [ "$TERM" == "dumb" ]
  then
    term=`ls /dev/fd/2 -l`
    term=${term##*'/'}
    term=`grep "${term}$" /etc/ttytype`
    term=${term%%[[:space:]]*}
    TERM=$term
    setterm -initialize
  fi

if [ "$TERM" == "dumb" ]
  then
    echo "Sorry dumb terminal interface not yet written" >&2
    rm -f $1
    trap -
    exit 1
  fi

if mainmenu
  then
    prepit > $1
    ret=0
  else
    rm $1
    ret=1
  fi

rm -f $TMP $TMPOUT

trap -
exit $ret
