#!/bin/sh

# Set the display device to the iLiad screen
export DISPLAY=:0

# By default, make set the shell's working directory to the current directory.
workingdir=`pwd`

# The directory of this script
scriptdir=`/usr/bin/dirname "$0"`

# The filename of this script
scriptbase=`/usr/bin/basename "$0"`

# Change to script directory and execute the config script
cd "$scriptdir"
. ./xshell.cfg

# Make a human-readable version string
versionstring="xshell terminal interface v`echo $version | sed "s/\(.\)\(.\)\(.\)/\1.\2.\3/"`"

printhelp() {
  # Print the standard help message
  echo
  echo $versionstring
  echo 
  echo "Launches a windowed command line terminal"
  echo
  echo "Usage: $scriptbase [--hold] [--working-directory PATH] [--execute CMD] [--passargs ARGS]"
  echo
  echo
  echo "  --hold               Holds the terminal open after command/session terminates"
  echo
  echo "  --working-directory  Sets the working path to the specified directory"
  echo
  echo "  --execute CMD        Executes a command"
  echo
  echo "  --passargs ARGS      Passes remaining arguments to the terminal program"
  echo
  echo "  --help               Print this help message and exit"
  echo
  echo "  --xshellversion      Prints the version of this xshell interface"
  echo 
  echo "  --terminalinfo       Prints the terminal being used"
  echo


}
 
# Parse command line arguments

# If a command line option defines a variable, that variable name will be in ac_prev.
ac_prev=

# While the number of unprocessed arguments is nonzero,
while test $# -ne 0 ; do

  # The current argument is ac_option
  ac_option="$1"

  # Set the variable named in ac_prev to ac_option.
  if test -n "$ac_prev" ; then
    eval "$ac_prev=\$ac_option"
    ac_prev=
    shift   
    continue
  fi

  case $ac_option in

    --xshellversion)
      echo "$versionstring" 
      exit 0;;

    --xshellversionnum)
      echo "$version"
      exit 0;;
	  
    --execute)
      ac_prev=toexecute ;;  

    --hold)
      hold=1 ;;

    --passargs)
      # Throw away the processed command line arguments
      shift $(($OPTIND))
      # Set $passargs to remaining command line arguments
      passargs="$@" 
      break ;;    

    --help | --xshellhelp)
      printhelp
      exit 0 ;;

    --terminalinfo) 
      echo "$terminalinfo"
      exit 0 ;;

    --working-directory)
      ac_prev=workingdir ;;

    *)
      echo "Error: Unrecognized parameter $1" >&2
      printhelp
      exit 1 ;;

    esac

  # Advance to the next argument
  shift

done

# Determine optional command line parameters to pass to mrxvt
params="$passargs"
if test "$hold" -eq 1 ; then
  params="-hold True $params"
fi
if test -n "$toexecute" ; then
  params="$params -e $toexecute"
fi
 

# Change to the mrxvt directory
# This is important since the hacked libX11.so looks for iliad_refresh.conf
# in the current directory.  We can still preserve the working directory by
# passing it as a command line argument to mrxvt.
cd mrxvt

# Gets the full name of the mrxvt directory
shelldir=`pwd`

# Sets the path to look for shared libraries
export LD_LIBRARY_PATH=$shelldir/lib

# Register more X11 fonts (see fonts/misc/fonts.alias for shortnames)
fontdir="$shelldir/fonts/misc"
# Only run xset if our fonts directory isn't already in the font path.
if !(xset q | grep "$fontdir" 1>| /dev/null) ; then
  ./xset +fp "$fontdir" > ./errors.txt 2>&1
fi

# Clean up any orphan mrxvt processes
# Note: this will usually result in a "no processes killed" error,
# which is normal.
/usr/bin/killall mrxvt >> ./errors.txt 2>&1


# Run mrxvt
# Note: mrxvtrc defines keyboard macros and screen font.
./mrxvt -wd "$workingdir" -cf "$shelldir/mrxvtrc" $params >> ./errors.txt 2>&1
