#!/bin/sh
#^^^^^^^^^ modify first line if necessary, don't delete this line
# (Bourne shell -- maybe Bash), the bourne shell call specified in first line
# is used for creation/modification of some more scripts!
#
# procedure to install xtem, see "Installation&Maintenance Guide" 
#                                 (file Installation.tex) for details
# call this shell script directly form this directory!
#
# Copyright(C) 1999  G. Lamprecht, W. Lotz, R. Weibezahn; IWD, Bremen University
################################################################################


# check and modify the following lines:

umask 022			; # prevents group/other-write permission
				  # for new files (e.g. at program compilation)

INSTPATH=""			; # normally empty, will then be set
				  # automatically (see below);
				  # must be set if used with "mount NFS"
				  # and using different export and mount pathes
				  # 
				  # If you set this variable, you must set it 
				  # such that *this* file is
				  #   $INSTPATH/xtem-n.nn/xteminstall
				  # (n.nn being the current version)

BINPATH="/usr/local/bin"	; # into this directory executeables and shell 
				  # scripts are copied; must be included in 
				  # search path variable $PATH

MANPATH="/usr/local/man/man1"	; # into this directory the man-page 
				  # is to be installed
    
WISHCALL="/usr/local/bin/wishx"	; # command to call wishx 
				  # (must include Tcl, Tk and TclX)

TCLCALL="/usr/local/bin/tclsh"	; # command to call tclsh or tcl 
				  # (Tcl without Tk, (i.e. "tclsh"), 
				  # can include TclX (i.e. "tcl"))

PRINTCOM="ABORT"		; # print command name (without options),
				  # usually  "lp"   (SYSV-Unix)
				  # or       "lpr"  (BSD-based Unix, e.g. Linux)
				  # 
				  # In order to force you to set this variable
				  # (correctly), we presetted it to "ABORT"

XTERMCALL="xterm -sb -sl 1024 -T xtem -n xtem"
				  # command to create an xterm window
				  # normally "xterm", 
				  # maybe    "aixterm"  under AIX etc.

LANGUAGES="{german deutsch} {english}"
				  # list of languages and possibly synonyms;
				  # the number of languages is not limited,
				  # each language is enclosed in braces and can 
				  # be followed by any number of synonyms

DEFLANGUAGE="german"		; # specifies the defaultlanguage

CC="gcc"			; # C-compiler for translation of C Programs

CC_OPTS=""			; # options for C-compiler (e.g. "-DAIX")

XiMaxBoxWidth=120		; # maximum box width (characters) 
				  # for (large) select boxes

################################################################################

# don't modify the following lines


newPrintingVst="no"

if   [ "$1" = "all" -o "$1" = "" ]
then
     basicInstallation="yes"; editSettingFiles="yes"

elif [ "$1" = "basic" ]
then 
     basicInstallation="yes"; editSettingFiles="no"
     if  [ "$2" = "printing" ]; then newPrintingVst="yes"; fi

elif [ "$1" = "settings" ]

then 
     basicInstallation="no";  editSettingFiles="yes"

else
      echo "usage: `basename $0` [ all | basic [ printing ] | settings ]"
      echo " basic      : basic installation of xtem (C-compilations, "
      echo "              setting files, file access mode, xtem shell-script, "
      echo "              man-pages etc.)"
      echo "   printing : printing.vst will be created even if already existing!"
      echo " settings   : allows editing the setting files by means of a menu"
      echo " all        : will first do basic installation "
      echo "             and then editing of the setting files (default) "
      exit
fi


if [ "$INSTPATH" = "" ]
then       path=`pwd`;   cd ..;   INSTPATH=`pwd`;   cd $path
fi

XTEMVERSION="7.03";                     ################# xtem version; xtem-Version
XTEMVPATH="$INSTPATH/xtem-$XTEMVERSION"

InstFile="$XTEMVPATH/xteminstall"
if [ ! -f "$InstFile" ]
then 
  echo "error: file \"$InstFile\" doesn't exist;"
  echo "check variable INSTPATH in file \"xteminstall\"";                 exit 1
fi
xVFile="$XTEMVPATH/xV.tcl"
if [ ! -f "$xVFile" ]
then 
  echo "error: file \"$xVFile\" doesn't exist!";                          exit 1
fi
grepVersion=`grep "set version " $xVFile`
grepXTEMVERSIONxV=`echo $grepVersion | cut -c14-17`
if [ "$XTEMVERSION" != "$grepXTEMVERSIONxV" ]
then
  echo "error: VERSION inconsistent: <$XTEMVERSION><$grepXTEMVERSIONxV>"; exit 1
fi


cd $XTEMVPATH


error=0
#
# check if file xtem.tcl exists & has correct version number, 
# directory write permissions O.K.
#
if [ ! -d "$XTEMVPATH" ]
then echo "error: directory \"$XTEMVPATH\" doesn't exist";               error=1
fi
if [ ! -w "$XTEMVPATH" ]
then echo "error: no write permission for \"$XTEMVPATH\"";               error=1
fi
if [ ! -f "$XTEMVPATH/xtem.tcl" ]
then echo "error: file \"$XTEMVPATH/xtem.tcl\" doesn't exist";           error=1
fi
#
#
# check if BINPATH and MANPATH are set correctly
#
if [ ! -d $BINPATH ]
then echo "Variable  BINPATH ($BINPATH) incorrect: directory not found"; error=1
fi
if [ ! -d $MANPATH ]
then echo "Variable  MANPATH ($MANPATH) incorrect: directory not found"; error=1
fi
#
#
# check if WISHCALL and TCLCALL are set correctly
#
if [ ! -f $WISHCALL ]
then echo "Variable  WISHCALL ($WISHCALL) incorrect: file not found";    error=1
fi
if [ ! -f $TCLCALL ]
then echo "Variable  TCLCALL ($TCLCALL)  incorrect: file not found";     error=1
fi
#
#
# check if PRINTCOM is set correctly
#
if [ "$PRINTCOM" = "ABORT" ]
then echo "Variable PRINTCOM incorrect,";                                error=1
fi
#
#
if [ "$error" = "1" ]
then 
     echo "installation aborted;"
     echo "check \"xteminstall\" and Installation Guide!"
     exit 1
fi
#

L="xteminstall.log"


# now installation can start
#

T="";							 echo "$T";echo "$T" >$L
T="start installation in: $XTEMVPATH";			 echo "$T";echo "$T">>$L
T="                   at: `date`";			 echo "$T";echo "$T">>$L
T="";							 echo "$T";echo "$T">>$L
T="(transcript will be written to file $L)";		 echo "$T";echo "$T">>$L
T="";							 echo "$T";echo "$T">>$L
T="";							 echo "$T";echo "$T">>$L

T="xteminstall first looks for:";			 echo "$T";echo "$T">>$L
T="    changecode, hyphen_show and pages_show.";	 echo "$T";echo "$T">>$L
T="(C compilation if necessary).";			 echo "$T";echo "$T">>$L

#
set `ls -t changecode*`
if [ "$1" != "changecode" ]
then       # changecode doesn't exist or exists and is older than changecode.c
  T="  compilation of changecode.c";			 echo "$T";echo "$T">>$L
  $CC $CC_OPTS -o changecode changecode.c			 |tee -a     $L
  if [ -f changecode ]; then  chmod a+rx changecode;  fi
fi
#
set `ls -t hyphen_show*`
if [ "$1" != "hyphen_show" ]
then       # hyphen_show doesn't exist or exists and is older than hyphen_show.c
  T="  compilation of hyphen_show.c";			 echo "$T";echo "$T">>$L
  $CC $CC_OPTS -o hyphen_show hyphen_show.c			 |tee -a     $L
  if [ -f hyphen_show ]; then  chmod a+rx hyphen_show;  fi
fi
#
set `ls -t pages_show*`
if [ "$1" != "pages_show" ]
then       # pages_show doesn't exist or exists and is older than pages_show.c
  T="  compilation of pages_show.c";			 echo "$T";echo "$T">>$L
  $CC $CC_OPTS -o pages_show pages_show.c			 |tee -a     $L
  if [ -f pages_show ]; then  chmod a+rx pages_show;  fi
fi

T="";							 echo "$T";echo "$T">>$L


CMD_PARAMS=" $L "
CMD_PARAMS="$CMD_PARAMS  # $INSTPATH        # $BINPATH         # $MANPATH "
CMD_PARAMS="$CMD_PARAMS  # $WISHCALL        # $TCLCALL "
CMD_PARAMS="$CMD_PARAMS  # $PRINTCOM        # $XTERMCALL       # $XTEMVERSION "
CMD_PARAMS="$CMD_PARAMS  # $LANGUAGES       # $DEFLANGUAGE "
CMD_PARAMS="$CMD_PARAMS  # $XiMaxBoxWidth   # $newPrintingVst "

CMD_BATCH="$TCLCALL $XTEMVPATH/xi1.tcl $CMD_PARAMS"

CMD_MENU="$WISHCALL $XTEMVPATH/xi2.tcl -n xtem_installation $CMD_PARAMS"


if [ "$basicInstallation" = "yes" ]; then
 T="Next (basic) part of installation is done by:";	 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
 T="$CMD_BATCH";					 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
 $CMD_BATCH; retInstBatch="$?"
 T="";							 echo "$T";echo "$T">>$L
 T="Return code of basic installation: $retInstBatch";	 echo "$T";echo "$T">>$L
 if [ "$retInstBatch" != "0" -a "$editSettingFiles" = "yes" ]; then
  T="";							 echo "$T";echo "$T">>$L
  T="Editing of setting files not done ";		 echo "$T";echo "$T">>$L
  T="in consequence of return code; ";			 echo "$T";echo "$T">>$L
  T="please check the reason and restart xteminstall ";	 echo "$T";echo "$T">>$L
  T="or do installatian manually according to the ";	 echo "$T";echo "$T">>$L
  T="installation Guide (file \"Installation.tex\")";	 echo "$T";echo "$T">>$L
  T="";							 echo "$T";echo "$T">>$L
  T="After this, if you want to edit the setting ";	 echo "$T";echo "$T">>$L
  T="files directly, you can call ";			 echo "$T";echo "$T">>$L
  T="       xteminstall settings";			 echo "$T";echo "$T">>$L
  T=" ";						 echo "$T";echo "$T">>$L
 elif [ "$retInstBatch" != "0" -a "$editSettingFiles" = "no" ]; then
  T="";							 echo "$T";echo "$T">>$L
  T="please check the reason and restart xteminstall ";	 echo "$T";echo "$T">>$L
  T="or do installatian manually according to the ";	 echo "$T";echo "$T">>$L
  T="installation Guide (file \"Installation.tex\")";	 echo "$T";echo "$T">>$L
  T="";							 echo "$T";echo "$T">>$L
  T="After this, if you want to edit the setting ";	 echo "$T";echo "$T">>$L
  T="files directly, you can call ";			 echo "$T";echo "$T">>$L
  T="       xteminstall settings";			 echo "$T";echo "$T">>$L
  T=" ";						 echo "$T";echo "$T">>$L
 elif [ "$retInstBatch" = "0" -a "$editSettingFiles" = "no" ]; then
  T="";							 echo "$T";echo "$T">>$L
  T="If you now want to edit the setting files,";	 echo "$T";echo "$T">>$L
  T="you can call ";					 echo "$T";echo "$T">>$L
  T="       xteminstall settings";			 echo "$T";echo "$T">>$L
  T="";							 echo "$T";echo "$T">>$L
 fi
 if [ "$retInstBatch" != "0" ]; then exit 1;  fi
else
 T="";							 echo "$T";echo "$T">>$L
 T="Basic installation was skipped as requested";	 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
fi


if [ "$editSettingFiles" = "yes" ]; then
 T=" ";							 echo "$T";echo "$T">>$L
 T="Installation menu for editing of the setting files"; echo "$T";echo "$T">>$L
 T="will be created by calling \"$WISHCALL\" and using"; echo "$T";echo "$T">>$L
 T="DISPLAY environment variable \"$DISPLAY\"";		 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
 if [ ! -f "$WISHCALL" ]; then
  T="Error: Variable WISHCALL=\"$WISHCALL\" ";		 echo "$T";echo "$T">>$L
  T="(set in file \"xteminstall\") is incorrect";	 echo "$T";echo "$T">>$L
  T="(file not found) ";				 echo "$T";echo "$T">>$L
 fi
 if [ "$DISPLAY" = "" ]; then
  T="Error: environement variable DISPLAY=\"$DISPLAY\" ";echo "$T";echo "$T">>$L
  T="is incorrect (is empty!) ";			 echo "$T";echo "$T">>$L
 fi
 T="   ";						 echo "$T";echo "$T">>$L
 T="$CMD_MENU";						 echo "$T";echo "$T">>$L
 T="";							 echo "$T";echo "$T">>$L
 $CMD_MENU; retInstMenu="$?"
 T="";							 echo "$T";echo "$T">>$L
 T="Return code of setting file edit menu: $retInstMenu";echo "$T";echo "$T">>$L
 if [ "$retInstMenu" != "0" ]; then
 T=" ";							 echo "$T";echo "$T">>$L
  T="(If menu doen't come up, check your DISPLAY ";	 echo "$T";echo "$T">>$L
  T=" environment variable and check WISHCALL in file "; echo "$T";echo "$T">>$L
  T=" \"xteminstall\" for correct settings!) ";		 echo "$T";echo "$T">>$L
 fi
 T=" ";							 echo "$T";echo "$T">>$L
 T=" retInstMenu=$retInstMenu  ";			 echo "$T";echo "$T">>$L
 T=" ";							 echo "$T";echo "$T">>$L
fi

if [ "$retInstMenu" != "0" ]; then exit 1;  fi

################################################################################
