#!/bin/sh
 
 # gmssetup
 # ========
 
 # This file is part of Gerolf Markup Shredder, 
 # written by G. D. Brettschneider (1999-2008). All rights reserved.
 # Send corrections to: MarkupShredder(at)Gerolf.org (www.Gerolf.org)

 export GMS_DATE=20080107
 export GMS_VERSION="0.08a"
   
 # ============================================================================
 
 # Initialize debugging mode, main codepage, linking mode:
 
 export GMS_DEBUG="0"
 export GMS_CODEPAGE="CP1252"
 export GMS_LINKS="internal"
  
 # Welcome message: -----------------------------------------------------------
 
 if [ "$1" != "-shell" -a "$2" != "-shell" ]; then   
    xterm -fg black -bg white -e "$0" -shell "$1"
   exit
 fi
 shift
 
 echo
 setterm -clear -reset -cursor off
 left=" Running:      GMS setup ..."
 if [ "$1" != "" ]; then 
   left=" Running:      GMS quick setup ..."; fi
 right="                       Gerolf Markup Shredder $GMS_VERSION"
 if [ "$1" != "" ]; then 
   right="                 Gerolf Markup Shredder $GMS_VERSION"; fi
 arg="///////////////////////////////////////"
 echo "$left$right"
 echo " $arg$arg"
 echo
 
 # Root user check: -----------------------------------------------------------
 
 if [ "$USER" != "root" ]; then
   echo "               This application wants to create startup links in "
   echo "                '\usr\bin' directory."
   echo
   echo "               You may have to log in as user 'root' to run "
   echo "               GMS setup."
   echo
   echo  -n "               Press [Enter] to continue or $quit"
   read
   setterm -reset 
   echo "$left$right"
   echo " $arg$arg"
   echo
 fi 

 unset left
 unset right
 unset arg
 
 # Get folder location: =======================================================
     
 # Get GMS_SETTING (current directory):
  
 # Check if current directory differs from script path: 
   cd .. ###
   test=$(pwd) ###
   cd - > "$test/nil" ###   
   if [ -f "$test/nil" ]; then rm "$test/nil"; fi ###   
   if [ ! -f "$PWD/gmssetup" ]; then
     if [ -f "$0" ]; then export PWD=$(dirname "$0"); fi; fi
   test=
 export GMS_SETTING=$PWD
   
 # Debug: ---------------------------------------------------------------------

 # Called scripts may contribute to gmsdebug.log. 
 # Renaming to gmssetup.log takes place at the end of this gmssetup script.
   export Z="gmsdebug.log"
   # Remove old log files in curent folder:
     if [ -f gmssetup.log ]; then rm gmssetup.log; fi
     if [ -f gmsdebug.log ]; then rm gmsdebug.log; fi
   # gmssetup log file header:
     echo " gmssetup.log" > $Z
     arg="///////////////////////////////////////"
       echo " $arg$arg" >> $Z
     unset arg
     echo " This is the setup log file for Gerolf Markup Shredder." >> $Z
     echo " To write the run time debug log file, set GMS_DEBUG=Z." >> $Z
     echo " Format: called script (parameter 1) ... (parameter 9)," >> $Z
     echo " followed by values of important environment variables." >> $Z
     echo " 'shredder': reads user input and calls script modules." >> $Z
     echo >> $Z
   # gmssetup parameters and environment variables:
     echo " $0 ($1) ($2) ($3) ($4) ($5) ($6) ($7) ($8) ($9)" >> $Z
     echo "   GMS_VERSION=$GMS_VERSION" >> $Z
     echo "   GMS_CODEPAGE=$GMS_CODEPAGE" >> $Z
     echo "   GMS_SETTING=$GMS_SETTING" >> $Z
   export Z="$GMS_SETTING/gmsdebug.log"

 # Go up one level and call it GMS_ROOT: --------------------------------------

 cd "$PWD"
 cd ..
 export PWD=$(pwd)
 echo " Defining:     Installation directories ..."
 echo
 export GMS_ROOT=$PWD
 echo " GMS_ROOT:       $PWD"
 echo "   GMS_ROOT=$PWD" >> "$Z"
 echo
 
 # Load folders and check if there's a folder for temorary files: -------------

 if [ -d "$GMS_SETTING" ]; then cd "$GMS_SETTING"; fi
 source "$GMS_SETTING/folder" called_by gmssetup
 if [ ! -d "$GMS_TEMP" ]; then mkdir "$GMS_TEMP"; fi
  
 # Setting variables: =========================================================
 
 # Browser interface variables:
 
 # Maximal size and number of files: 
   export GMS_MAXSIZE=1234567
   export GMS_MAXFILES=123
 # Width of text area (join/split): 
   export GMS_WIDTH_ONE=60
   export GMS_WIDTH_TWO=40
 # Height of text area (join/split): 
   export GMS_HEIGHT_ONE=30
   export GMS_HEIGHT_TWO=40
 # Font size, in point (join/split): 
   export GMS_SIZE_ONE=10
   export GMS_SIZE_TWO=8

 if [ ! -d "$GMS_ROOT/tmp" ]; then mkdir "$GMS_ROOT/tmp"; fi
 if [ -d "$GMS_ROOT/tmp" ]; then chmod 777 "$GMS_ROOT/tmp"; fi ###
 
 # Textmode interface variables: ----------------------------------------------
 
 # Initialize colors & pattern (R: set by random) and animation mode:
 
 # Foreground colors: 
   export GMS_TEXT=R
   export GMS_HOTKEY=R
   export GMS_PATTERN=R
 # Background colors: 
   export GMS_BANNER=R
   export GMS_SHADE=R
   export GMS_DESKTOP=R
 # Fill character: 
   export GMS_LETTER=R
 # Menu animation:
   export GMS_ANIMATE="on"
 
 # Programs: ------------------------------------------------------------------
 
   export GMS_VIEWER=less
   export GMS_EDITOR=vi
   if [ -f /usr/bin/gedit ]; then export GMS_EDITOR=gedit; fi
   if [ -f /usr/bin/kedit ]; then export GMS_EDITOR=kedit; fi
   if [ -f /usr/bin/emacs ]; then export GMS_EDITOR=emacs; fi
   if [ -f /usr/bin/xemacs ]; then export GMS_EDITOR=xemacs; fi
   if [ -f /usr/bin/quanta ]; then export GMS_EDITOR=quanta; fi
   if [ -f /opt/kde/bin/kwrite ]; then export GMS_EDITOR=kwrite; fi # SuSE
   if [ -f /opt/kde2/bin/kwrite ]; then export GMS_EDITOR=kwrite; fi # "
   if [ -f /opt/kde3/bin/kwrite ]; then export GMS_EDITOR=kwrite; fi # "
   if [ -f /usr/bin/kwrite ]; then export GMS_EDITOR=kwrite; fi
   if [ -f /usr/bin/nedit ]; then export GMS_EDITOR=nedit; fi
   export GMS_BROWSER=lynx
   if [ -f /usr/bin/links ]; then export GMS_BROWSER=links; fi
   if [ -f /usr/bin/opera ]; then export GMS_BROWSER=opera; fi
   if [ -f /opt/kde/bin/konqueror ]; then export GMS_BROWSER=konqueror; fi #SuSE
   if [ -f /opt/kde2/bin/konqueror ]; then export GMS_BROWSER=konqueror; fi # "
   if [ -f /opt/kde3/bin/konqueror ]; then export GMS_BROWSER=konqueror; fi # "
   if [ -f /usr/bin/konqueror ]; then export GMS_BROWSER=konqueror; fi
   if [ -f /usr/bin/netscape ]; then export GMS_BROWSER=netscape; fi
   if [ -f /usr/bin/mozilla ]; then export GMS_BROWSER=mozilla; fi
   export GMS_ANALYST=tidy
   export GMS_TSETTER=pdfetex 
   export GMS_READER=acroread
   if [ -f /usr/bin/gv ]; then export GMS_READER=gv; fi
   if [ -f /usr/bin/ghostviev ]; then export GMS_READER=ghostviev; fi
   if [ -f /usr/bin/xpdf ]; then export GMS_READER=xpdf; fi
   if [ -f /usr/bin/kghostviev ]; then export GMS_READER=kghostviev; fi
   if [ -f /usr/bin/kpdf ]; then export GMS_READER=kpdf; fi
   if [ -f /usr/bin/acroread ]; then export GMS_READER=acroread; fi
   
 # Set script and binary permissions: =========================================
 
 for i in g_color g_file g_font g_good g_launch g_list g_menu g_palet g_plug \
   g_prog g_rain g_save g_select g_vars g_wel gms l_banner l_box l_code \
   l_color l_desk l_file l_gms l_good l_list l_menu l_prog l_rain l_save \
   l_select l_wel shredder; 
 do chmod 755 "$GMS_SHELL/$i"; done
 
 for i in afm2tfm pdfetex reply tidy ttf2afm vptovf; 
 do chmod 755 "$GMS_BINARIES/$i"; done 
 
 chmod 755 "$GMS_ROOT/doc/reply/mk_reply"
 
 # Launcher sript: ============================================================

 # Create a new 'gerolf' script:
 
 echo " Building:     GMS launcher script ..."
 echo "                 $GMS_SETTING/gerolf"
 echo
 source "$GMS_SHELL/g_launch" -build called_by gmssetup
 if [ ! -f "gerolf" ]; then
   echo "               Error: Could not build launcher script."
   exit
 fi 
 
 # Show lower desktop border: -------------------------------------------------

 export REPLY_BANNER=15; export REPLY_TEXT=0
 export REPLY_OFFSET=0; export REPLY_SIZE=80
 export arg=///////////////////////////////////////
   "$GMS_BINARIES/reply" -banner 23 " $arg$arg"
 unset arg
 "$GMS_BINARIES/reply" -banner 24 "Running under $OSTYPE ..."
 export REPLY_OFFSET=79; export REPLY_SIZE=3
 "$GMS_BINARIES/reply" -banner 11 " "
 echo

 # Create a link from 'gerolf' and 'gms' to /usr/bin: -------------------------
 
 echo " Creating:     Links from 'gerolf' and 'gms' to ..."
 echo "                 /usr/bin"
 if [ -d "/usr/bin" ]; then
   if [ -L "/usr/bin/gms" ]; then rm "/usr/bin/gms"; fi 
   ln -s "$GMS_SHELL/gms" "/usr/bin/gms"
   if [ -L "/usr/bin/gerolf" ]; then rm "/usr/bin/gerolf"; fi 
   ln -s "$GMS_SETTING/gerolf" "/usr/bin/gerolf" 
 fi 
  
 # Check if links have been created: ------------------------------------------
  
 if [ ! -L "/usr/bin/gms" ]; then
   echo
   echo "               Could not link 'gms' script to"
   echo "                '/usr/bin' directory."
 fi
 if [ ! -L "/usr/bin/gerolf" ]; then
   echo
   echo "               Could not link 'gerolf' script to"
   echo "                '/usr/bin' directory."
 fi
 
 # Create KDE menu directories: -----------------------------------------------
  
 function KDE_menu {
   GMS_KDE=$1
   if [ -d "$GMS_KDE" ]; then 
     if [ -d "$GMS_KDE/share/applnk" ];  then
       GMS_KDE="$GMS_KDE/share/applnk/Office"
       if [ ! -d "$GMS_KDE" ]; then mkdir "$GMS_KDE"; fi
       GMS_KDE="$GMS_KDE/Markup Shredder"
       if [ ! -d "$GMS_KDE" ]; then mkdir "$GMS_KDE"; fi
     fi   
     GMS_LINK="$GMS_KDE/gerolf.kdelnk"
       echo "# KDE Config File" > "$GMS_LINK"
       echo "[KDE Desktop Entry]" >> "$GMS_LINK"
       echo "Exec=\"$GMS_ROOT/gerolf\"" >> "$GMS_LINK"
       echo "Icon=run" >> "$GMS_LINK"
       echo "Name=gerolf" >> "$GMS_LINK"
       echo "Terminal=true" >> "$GMS_LINK"
       echo "Type=Application" >> "$GMS_LINK"
     chmod 755 "$GMS_LINK"
     GMS_LINK="$GMS_KDE/gmssetup.kdelnk"
       echo "# KDE Config File" > "$GMS_LINK"
       echo "[KDE Desktop Entry]" >> "$GMS_LINK"
       echo "Exec=\"$GMS_ROOT/gmssetup\"" >> "$GMS_LINK"
       echo "Icon=run" >> "$GMS_LINK"
       echo "Name=gmssetup" >> "$GMS_LINK"
       echo "Terminal=true" >> "$GMS_LINK"
       echo "Type=Application" >> "$GMS_LINK"
     chmod 755 "$GMS_LINK"
   fi
   GMS_KDE=
 }
 
 # Get KDE directory: 
  
 if [ -d "/var/lib/menu/kde" ]; then # Aurox Linux 9x
   KDE_menu "/var/lib/menu/kde"; fi
 if [ -d "/opt/kde3" ]; then # SuSE Linux 9x
   KDE_menu "/opt/kde3"; fi
 if [ -d "/opt/kde2" ]; then # SuSE Linux 8x
   KDE_menu "/opt/kde2"; fi
 if [ -d "/opt/kde" ]; then # SuSE Linux 7x   
   KDE_menu "/opt/kde"; fi
 if [ -d "$KDEDIR" ]; then 
   KDE_menu "$KDEDIR"; fi
 
 # Create [GMS_ROOT] level link to gerolf: -----------------------------------
 
 GMS_LINK="$GMS_ROOT/gerolf"
 GMS_GEROLF="$GMS_ROOT/etc/gerolf"
 echo "#!/bin/sh" > "$GMS_LINK"
 echo " \"$GMS_GEROLF\" \$1 \$2 \$3 \$4 \$5 \$6 \$7 \$8 \$9" >> "$GMS_LINK"
 chmod 755 "$GMS_LINK"
 
 GMS_LINK="$HOME/Desktop/gerolf"
 GMS_GEROLF="$GMS_ROOT/etc/gerolf"
 echo "#!/bin/sh" > "$GMS_LINK"
 echo " \"$GMS_GEROLF\" \$1 \$2 \$3 \$4 \$5 \$6 \$7 \$8 \$9" >> "$GMS_LINK"
 chmod 755 "$GMS_LINK"
  
 # Create [GMS_ROOT] level link to gmssetup: ----------------------------------

 GMS_LINK="$GMS_ROOT/gmssetup"
 echo "#!/bin/sh" > "$GMS_LINK"
 echo >> "$GMS_LINK"
 echo " cd \"$GMS_ROOT/etc\"" >> "$GMS_LINK"
 echo " \"$GMS_ROOT/etc/gmssetup\" \$1" >> "$GMS_LINK"
 
 GMS_LINK="$HOME/Desktop/gmssetup"
 echo "#!/bin/sh" > "$GMS_LINK"
 echo >> "$GMS_LINK"
 echo " cd \"$GMS_ROOT/etc\"" >> "$GMS_LINK"
 echo " \"$GMS_ROOT/etc/gmssetup\" \$1" >> "$GMS_LINK"
 chmod 755 "$GMS_LINK"
 
 GMS_GEROLF=
 GMS_LINK=
                
 # Font map and format creation: ==============================================
  
 # Get Windows fonts: ---------------------------------------------------------
 
 if [ ! -d "$GMS_ROOT/fonts" ]; then 
   mkdir "$GMS_ROOT/fonts"; fi
   chmod 755 "$GMS_ROOT/fonts"
 if [ ! -d "$GMS_ROOT/fonts/ttf" ]; then 
   mkdir "$GMS_ROOT/fonts/ttf"; fi
   chmod 755 "$GMS_ROOT/fonts/ttf"
   
 echo
 echo
 echo \
 "               Please enter the path to your Windows True Type fonts here,"
 echo \
 "               if this is a dual boot system, e.g. \"/hda1/WINDOWS/Fonts\"."
 echo
 echo  -n " Your answer:    " 
 setterm -cursor on
   read winfonts   
 setterm -cursor off
 
 if [ -d "$winfonts" ]; then cd "$winfonts"
   echo " Copying:        $winfonts ..." ###
   for j in *.ttf; do 
     if [ -f $j ]; then 
       cp $j "$GMS_ROOT/fonts/ttf"
       chmod 755 "$GMS_ROOT/fonts/ttf/$j"
     fi
   done    
   if [ -d "$GMS_ROOT/fonts/ttf" ]; then 
     cd "$GMS_ROOT/fonts/ttf"
     for j in u*.ttf; do
       if [ -f $j ]; then rm $j; fi; done ###
   fi 
 else
   if [ "$winfonts" = "" ]; then winfonts="True type font directory." ###
     echo "               Missing path to standard Windows fonts." ###
   else  echo "                 $winfonts"; fi ###
   echo -n ###
   echo "               Press [Ctrl+C] to break and restart gmssetup ..." ###
   sleep 2s ###
 fi
 winfonts=
  
 # Write font map:
 
 if [ "$1" != "-quick" ]; then
   source "$GMS_SHELL/gms" -w -quiet called_by gmssetup
   if [ ! -f "$GMS_SETTING/font.map" ]; then
     echo " Error:        Could not build font map."
   fi 
 fi
   
 # Initialize format: ---------------------------------------------------------
  
 export REPLY_OFFSET=0; export REPLY_SIZE=17
 "$GMS_BINARIES/reply" -banner 18 Initializing:
 export REPLY_OFFSET=14; export REPLY_SIZE=68
 "$GMS_BINARIES/reply" \
   -banner 18 "TeX format file (this may take some time) ..."
 source "$GMS_SHELL/gms" -i -quiet called_by gmssetup 
 export REPLY_BANNER=15; export REPLY_TEXT=0
 export REPLY_OFFSET=14; export REPLY_SIZE=68
 "$GMS_BINARIES/reply" -banner 18 "  $GMS_BINARIES/gerolf.(e)fmt"
  
 if [ ! -f "$GMS_BINARIES/gerolf.efmt" -a \
   ! -f "$GMS_BINARIES/gerolf.fmt" ]; then
   echo " Error:        Could not build TeX format file."
 fi 
 
 # Write memo (load default.htm): ---------------------------------------------

 export GMS_FOLDER="$GMS_TEMPLATE/default"
 export GMS_FILE=default.htm
 source "$GMS_SHELL/g_vars" -write_memo called_by gmssetup
    
 # Epilogue: ==================================================================
 
 echo; echo
 echo " Startup call: gerolf ... textmode interface (random colors)"
 echo "               gms ... command line interface (black/white)"
 echo
 echo  -n " /////////////////////////////////////"
 echo "/////////////////////////////////////////"
 
 # Draw startup/break info on screen:
     echo -n " Done:         GMS setup."
     echo -n "            Press [Enter] to start or [Ctrl+C] to quit"
 # Set break cursor at a nice place:
     export REPLY_OFFSET=13; export REPLY_SIZE=0
       "$GMS_BINARIES/reply" -banner 22 
 # Clean up environment:
     source  "$GMS_SHELL/g_vars" -clear called_by gmssetup
 # Write gmsdebug.log footer:
     arg="///////////////////////////////////////"
        echo " $arg$arg" >> "$Z"
        echo -n " $GMS_SETTING" >> "$Z"
     unset arg
 # Rename gmsdebug.log to gmssetup.log:
     if [ -f "$Z" ]; then mv "$Z" "$GMS_SETTING/gmssetup.log"; fi
 # Final unsets:
     export Z=
     export GMS_DEBUG=
     export GMS_ROOT=
     export GMS_SETTING=
     export backup_base=
     export backup_file=
     export backup_folder=
 # Allow user interruption or start textmode interface: 
     setterm -cursor on
     read
     gerolf called_by gmssetup
