#!/bin/sh
#
# Copyright Romolo Manfredini, Parma, Italy IT
#
# Credits to Patrick Volkerding, Moorhead, Minnesota USA
# as this script was strongly inspired by pkgtool.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is 
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Mon Jul 25 13:50:48 MET DST 1994
# 
# start of User configurable section:

if [ "$EDITOR" = "" ]; then
   EDITOR=vi
fi
if [ "$PRINTER" = "" ]; then
   PRINTER=lps
fi

# end of User configurable section:

show_help() {
   dialog --title "TeXmgr Help" --infobox \
"\nThese program have the aim to help you in handling TeX files \n\
expecially for cleaning directories and printing \n\n   \
Usage: texmgr [nomefile]." 7 76
}

delete_tex_files() {
rm *.log *.toc *.tp *.tps *.vr *.vrs *.ky *.kys *.aux *.cp *.cps *.pg *.pgs *.fn *.fns 2> /dev/null >/dev/null
 }

delete_all_tex_files() {
rm *.dvi *.ps *.log *.toc *.tp *.tps *.vr *.vrs *.ky *.kys *.aux *.cp *.cps *.pg *.pgs *.fn *.fns 2> /dev/null >/dev/null
 }

# Here, we read the list of arguments passed to the pkgtool script.
if [ $# -gt 0 ]; then # there are arguments to the command
 while [ $# -gt 0 ]; do
  case "$1" in
  "--help")
  show_help;
  sleep 10;
  exit
  "-?"
  show_help;
  sleep 10;
  exit
  esac
  if [ -t $1 ]; then
   TEXFILE=`basename $1|sed -e '/.tex/s///'`
   WORK_DIR=`dirname $1`
   cd $WORK_DIR
   break
  else
    show_help;
    sleep 5
    break;
  fi
 done
fi    # there were no arguments, so we'll get the needed information from the
      # user and then go on.
WORK_DIR=`pwd`
while [ 0 ]; do
if [ "$TEXFILE" = "" ]; then
 dialog --title "TeX Manager Version 1.0d" \
--menu "\nWelcome to TeXmgr V1.0d.\n\
\nWhich option would you like?\n\
\nCurrent directory is: $WORK_DIR \n" 19 74 6 \
"File" "Choose a .tex file to process in the current dir" \
"Clean" "Remove .log .aux etc files but leave .dvi and .ps" \
"VeryClean" "Remove also .dvi and .ps" \
"DirChange" "Change working directory" \
"NewFile" "Start with a new file" \
"Exit" "Exit TeXmgr" 2> /tmp/reply$$
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/reply$$
   reset
   exit  
  fi
  REPLY="`cat /tmp/reply$$`"
  rm -f /tmp/reply$$
  if [ "$REPLY" = "Exit" ]; then
   reset
   exit
  elif [ "$REPLY" = "File" ]; then
   dialog --title "SCANNING" --infobox "Please wait while \
TeXmgr looks for TeX files present in the current directory." 7 40
   echo 'dialog --menu "Please select the file you wish to process." 15 55 8 \' >/tmp/viewscr$$
   if [ "`ls *.tex 2>/dev/null`" = "" ]; then
       dialog --title "NO FILE FOUND" \
--msgbox "No *.tex file found in the  \
current directory, please change \
directory and then try again." 10 50
   else 
   ls *.tex|sed -e '/.tex/s///'|sed -e 's/.*/"&" "" \\/' >> /tmp/viewscr$$
   echo "2> /tmp/return\$\$" >> /tmp/viewscr$$
   while [ 0 ]; do
    source /tmp/viewscr$$
    if [ ! "`cat /tmp/return$$`" = "" ]; then
     TEXFILE=`cat /tmp/return$$`
     break
    else
     break 
    fi
   done
   rm -f /tmp/return$$ /tmp/viewscr$$ /tmp/tmpmsg
  fi
  elif [ "$REPLY" = "NewFile" ]; then
dialog --title "Starting a new TeX file" --inputbox "Please enter the new\
 file name:" 10 50 2>/tmp/newfile$$
   if [ $? = 1 ]; then
    rm -f /tmp/newfile$$
    reset
   else
    STRING="`cat /tmp/newfile$$`"
    rm -f /tmp/newfile$$
    if [ "$STRING" != "" ]; then
     WORK_DIR=`dirname $STRING`
     TEXFILE=`basename $STRING|sed -e '/.tex/s///'`
     cd $WORK_DIR
     WORK_DIR=`pwd`
    fi
   fi
  elif [ "$REPLY" = "Clean" ]; then
  delete_tex_files;
  elif [ "$REPLY" = "VeryClean" ]; then
  delete_all_tex_files;
  elif [ "$REPLY" = "DirChange" ]; then
dialog --title "SELECT SOURCE DIRECTORY" --inputbox "Please enter the new\
 working directory:" 10 50 2>/tmp/wrkdir$$
   if [ $? = 1 ]; then
    rm -f /tmp/wrkdir$$ 
    reset
    exit
   fi
   WORK_DIR="`cat /tmp/wrkdir$$`"
   rm -f /tmp/wrkdir$$
   if [ ! -d $WORK_DIR ]; then
    dialog --title "DIRECTORY NOT FOUND" \
--msgbox "The directory you have selected \
does not seem to exist. Please check the \
directory and then try again." 10 50
    reset
   else
    cd $WORK_DIR
    WORK_DIR=`pwd`
   fi;
  fi;
 else
 dialog --title "TeX Manager Version 1.0d" \
--menu "\nWelcome to TeXmgr V1.0d.\n \
\nWhich option would you like?\n \
\nCurrent directory is: $WORK_DIR \n" 19 74 6 \
"TeX" "From $TEXFILE.tex make $TEXFILE.dvi using TeX." \
"LaTeX" "From $TEXFILE.tex make $TEXFILE.dvi using LaTeX." \
"Dvips" "From $TEXFILE.dvi make $TEXFILE.ps." \
"Print" "Print $TEXFILE.ps using lpr -P $PRINTER." \
"Edit" "Edit $TEXFILE.tex using $EDITOR." \
"Menu" "Goto TOP level menu" 2> /tmp/reply$$
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/reply$$
   echo Menu >/tmp/reply$$
  fi
  REPLY="`cat /tmp/reply$$`"   
  rm /tmp/reply$$
  if [ "$REPLY" = "TeX" ]; then
   reset
   tex $TEXFILE.tex
   sleep 2
  elif [ "$REPLY" = "LaTeX" ]; then
   reset
   latex $TEXFILE.tex
   sleep 2
  elif [ "$REPLY" = "Dvips" ]; then
   reset
   dvips -o$TEXFILE.ps $TEXFILE.dvi
   sleep 2
  elif [ "$REPLY" = "Print" ]; then
   lpr -P $PRINTER $TEXFILE.ps
  elif [ "$REPLY" = "Edit" ]; then
   $EDITOR $TEXFILE.tex
  elif [ "$REPLY" = "Menu" ]; then
   TEXFILE=""
  fi
 fi
done

  