#! /bin/sh
#
# MAKE_ID: driver program for the insertion devices code for SHADOW.
#
# Author: Mumit khan <khan@xraylith.wisc.edu>
# Copyright (c) 1990-1999 Mumit Khan
#
# Source: src/source/id/make_id
#
# ----------------------------------------------
#               SHADOW
#    Center for X-ray Lithography
#  University of Wisconsin-Madison
#  3731 Schneider Dr., Stoughton, WI, 53589
# ------------------------------------------------
#
#

#
# external programs used:
#
#   cat
#   rm
#

#
# local variables for communication among modules.
#
debug=0				;# used wherever there is a reason ;-)

#
# This is the command procedure to activate either the wiggler or undulator.
# This must be called before running SOURCE.
#

# First of all, Where are all the programs? Set it to nothing if all the
# programs are in the PATH variable of the SHELL.

#
# Make sure the environment is correctly set up.
#
setup_env () 
{
  # what platform? We only care about Unix vs Windows32.
  if [ -n "$SystemRoot" -o -n "$SYSTEMROOT" -o -n "$COMSPEC" ]; then
    os=win32
    shell=sh
    # canonicalize the argv0.
    progname=`cygpath -u $progname 2>/dev/null || echo $progname`
    tmpdir=${TMPDIR-.}
  else
    os=unix
    shell=/bin/sh
    tmpdir=${TMPDIR-/tmp}
  fi

  # Deduce SHADOW_ROOT from argv0, but only if SHADOW_ROOT is not
  # defined by the user.
  if [ -z "$SHADOW_ROOT" ]; then
    case $progname in 
    \./* | /*)
      ;;
    *)
      IFS_SAVE="$IFS"
      IFS=:
      for d in $PATH; do
        if [ -f $d/$progname ]; then
	  progname=$d/$progname
	  break
	fi
      done
      IFS="$IFS_SAVE"
      ;;
    esac
    progdir=`dirname $progname`
    rootdir=`dirname $progname`
    shadow_root=`(cd $rootdir; pwd)`
    # echo "progdir = $progdir" >&2
    # echo "SHADOW_ROOT NOW set to $shadow_root" >&2
  else
    # echo "SHADOW_ROOT preset to $SHADOW_ROOT" >&2
    shadow_root=$SHADOW_ROOT
  fi

  LOGNAME=${LOGNAME-"User"}
  USER=${USER-$LOGNAME}

  SHADOW_ROOT=$shadow_root
  SHADOW_BIN_DIR=${SHADOW_BIN_DIR-$SHADOW_ROOT/bin}
  SHADOW_DATA_DIR=${SHADOW_DATA_DIR-$SHADOW_ROOT/data}
  SHADOW_ENV_FILE=${SHADOW_ENV_FILE-$tmpdir/shenv.$$}

  export USER LOGNAME 
  export SHADOW_ROOT SHADOW_BIN_DIR SHADOW_ENV_FILE

  echo os="$os" shell="$shell" progname="$progname" >&1
}

cleanup_and_exit ()
{
  rm -f $SHADOW_ENV_FILE
  if [ $# -eq 1 ]; then
    status=$1
  else
    status=0
  fi
  exit $status
}

handle_interrupt () 
{
  echo ""
  #[ $toplevel -eq 0 ] && {
  #  toplevel=1
  #  return
  #}
  echo "Exiting MAKE_ID due to interrupt ..."
  cleanup_and_exit 1
}

# Catch signals. ($xs kludge needed by Sun /bin/sh).
xs=0
trap 'cleanup_and_exit $xs' 0
trap 'handle_interrupt' 2 15
trap 'echo "Aborting."; xs=1; cleanup_and_exit' 1 3 13

eval setup_env >/dev/null

[ "$debug" -eq 1 ] && cat << EOB
------------------------------------------------------------------
OS              : $os
SHELL           : $shell
USER            : $USER
LOGNAME         : $LOGNAME
SHADOW_ROOT     : $SHADOW_ROOT
SHADOW_BIN_DIR  : $SHADOW_BIN_DIR
SHADOW_ENV_FILE : $SHADOW_ENV_FILE
------------------------------- x --------------------------------
EOB

# FIXME: This really should be done with a command line arg. -- MK
# The VMS version of MAKE_ID first runs EPATH, which sets a LOGICAL to say
# what device the user has picked. Since Unix processes can't set the
# environment of parent processes, we have to kludge it (sort of like 
# tset does) by writing the environment strings to a file and source'ing 
# the file. So define the SHADOW_ENV_FILE to pick a file. 

#
# Run epath to create the parameter and the trajectory files. EPATH writes
# the name of the device in $SHADOW_ENV_FILE.
#

$SHADOW_BIN_DIR/epath

device=`cat $SHADOW_ENV_FILE`
rm -f $SHADOW_ENV_FILE

# now depending on the return value, run appropriate programs.
case "$device" in 
*WIGGLER)
  $SHADOW_BIN_DIR/nphoton
  status=$?
  ;;
*UNDULATOR)
  $shell $SHADOW_BIN_DIR/undul
  status=$?
  ;;
*)
  echo "Error: EPATH did not return an appropriate device. No device chosen."
  status=1
  ;;
esac

cleanup_and_exit $status

