#!/bin/sh
#
# vi:set ts=40 sw=2:
#

ACRO_PRODUCT="Distillr"
export ACRO_PRODUCT

BASENAME_ARGV0=`basename $0`
case "$BASENAME_ARGV0" in
  distill)  prod="Acrobat Distiller Command"  ;;
  distilld) prod="Acrobat Distiller Daemon"   ;;
esac

ver=3.0
install_dir=/disks/flex/local/Acrobat302

#
# Prepend a colon seperated environment variable
# $1 string to be prepended
# $2 environment variable
#
prepend()
{
  if [ -z "$2" -o "$2" = "$1" ] ; then
    echo "$1"
  else
    first="`expr "$2" : '\([^:]*\):'`"
    if [ "$first" = "$1" ] ; then
      echo "$2"
    else
      echo "${1}:${2}"
    fi
  fi
}


#
# Tests the version file in an installation directory.
#
test_install_dir()
{
  versFile="$1/$ACRO_PRODUCT/AcroVersion"
  if [ "$1" -a -f "$versFile" ] \
  && [ "`cat $versFile 2>/dev/null`" = "$ver" ] ; then
    return 0
  else
    return 1
  fi
}


#
# Return the resourcepath given:
#   1) site resource env
#   2) self resource env
#
get_upr_dirs()
{
  if [ "$2" ] ; then
    SELF_PATH=`echo $2 | sed -e "s|::|:$1:|g"`
  else
    SELF_PATH="$1"
  fi
  echo $SELF_PATH | sed -e 's|:| |g'
}

#
# Gets a UPR given:
#   1) UPR dir list
#   2) resource name
#
get_resource()
{
  for UPR_DIR in $1 ; do
    if [ ! -d $UPR_DIR ] ; then continue; fi
    if [ '*.upr' = "`cd $UPR_DIR 2>&1; echo *.upr`" ] ; then continue; fi
    for UPR_FILE in `ls $UPR_DIR/*.upr` ; do
      retVal=`sed -n -e "s|^$2=||p" $UPR_FILE`
      if [ "$retVal" ] ; then
         echo "$retVal" ; return ; fi
    done
  done
}


#
# Get the current working directory.
# Try to avoid automounter directories by checking
# if $HOME or $PWD is the same directory as pwd,
# and removing the automount directory component.
#
cwd="`pwd 2> /dev/null`"
if [ -z "$cwd" -o ! -d "$cwd" ] ; then
  echo "ERROR: Cannot determine current directory."
  exit 1
fi

if [ "$HOME" -a -d "$HOME" ] && [ "`cd / ; cd "$HOME" ; pwd`" = "$cwd" ] ; then
  cwd="$HOME"
elif [ "$PWD" -a -d "$PWD" ] && [ "`cd / ; cd "$PWD" ; pwd`" = "$cwd" ] ; then
  cwd="$PWD"
fi

if [ "$cwd" != / -a "${AUTOMOUNT_DIR=/tmp_mnt}" ] ; then
  tmp="`expr "$cwd" : "$AUTOMOUNT_DIR"'\(.*\)'`"
  if [ "$tmp" -a -d "$tmp" ] ; then
    if [ "`cd / ; cd "$tmp" ; pwd`" = "`pwd`" ] ; then
      cwd="$tmp"
    fi
  fi
fi

PWD="$cwd"
export PWD


#
# Setup ACRO_ARG0 to this script
#
arg0="$0"
if [ "$arg0" ] ; then
  case "$arg0" in
     /*) ;;
    ./*) arg0="$cwd/`expr "$arg0" : '\./\(.*\)'`" ;;
      *) arg0="$cwd/$arg0" ;;
  esac

  ACRO_ARG0="$arg0"
  export ACRO_ARG0
fi


#
# Try to find the installation directory
#
if ( test_install_dir "$install_dir" ) ; then
  ACRO_TOPLEVEL_DIR="$install_dir"
  export ACRO_TOPLEVEL_DIR
else
  script="$arg0"
  while [ "$script" ] ; do
    install_dir="`dirname "$script"`"
    if ( test_install_dir "$install_dir" ) ; then
      ACRO_TOPLEVEL_DIR="$install_dir"
      export ACRO_TOPLEVEL_DIR
      break
    fi

    install_dir="`dirname "$install_dir"`"
    if ( test_install_dir "$install_dir" ) ; then
      ACRO_TOPLEVEL_DIR="$install_dir"
      export ACRO_TOPLEVEL_DIR
      break
    fi

    if [ -h "$script" ] ; then
      new_script=`ls -l "$script" | sed 's/^.*-> *\(.*\) *$/\1/'`
      if [ "$new_script" -a "`expr "$new_script" : '/.*'`" = 0 ] ; then
        new_script="`dirname "$script"`/$new_script"
      fi
      script="$new_script"
    else
      break
    fi
  done

  if ( test_install_dir "$ACRO_TOPLEVEL_DIR" ) ; then
    :
  elif ( test_install_dir "$ACRO_HOME" ) ; then
    ACRO_TOPLEVEL_DIR="$ACRO_HOME"
    export ACRO_TOPLEVEL_DIR
  else
    echo "ERROR: Cannot find installation directory."
    exit 1
  fi
fi


#
# setup the configuration from uname
#
os_name=`uname -s`
if [ "$OSname" = "AIX" ] ; then
  os_release=`uname -a | ( read name host minor major foo ; echo $major.$minor )`
else
  os_release=`uname -r`
fi

case "$os_name" in
  SunOS)
    case "$os_release" in
      4.1.3*|4.1.4*)
        ACRO_CONFIG=sparcsun
        export ACRO_CONFIG
        ;;
      5.*)
        machine_type=`uname -p`
        case "$machine_type" in
          sparc)
            ACRO_CONFIG=sparcsolaris
            export ACRO_CONFIG
            ;;
        esac
        ;;
    esac
    ;;
  HP-UX)
    ACRO_CONFIG=hppahpux
    export ACRO_CONFIG
    ;;
  IRIX)
    ACRO_CONFIG=mipsirix
    export ACRO_CONFIG
    ;;
  AIX)
    ACRO_CONFIG=rs6000aix
    export ACRO_CONFIG
    ;;
esac

if [ -z "$ACRO_CONFIG" ] ; then
  echo "The OS named $os_name version $os_release is currently not installed."
  echo "Try running on a installed platform and connecting to your display."
  echo "Installed platform(s) include the following:"
  if [ -d "$ACRO_TOPLEVEL_DIR"/"$ACRO_PRODUCT"/sparcsun ] ; then
    echo "  SPARC/SunOS version 4.1.3 or 4.1.4"
  fi
  if [ -d "$ACRO_TOPLEVEL_DIR"/"$ACRO_PRODUCT"/sparcsolaris ] ; then
    echo "  SPARC/Solaris version 2.x"
  fi
  if [ -d "$ACRO_TOPLEVEL_DIR"/"$ACRO_PRODUCT"/hppahpux ] ; then
    echo "  HP/HP-UX version 9.0.x and 10.x"
  fi
  if [ -d "$ACRO_TOPLEVEL_DIR"/"$ACRO_PRODUCT"/mipsirix ] ; then
    echo "  MIPS/IRIX version 5.3 and 6.2"
  fi
  if [ -d "$ACRO_TOPLEVEL_DIR"/"$ACRO_PRODUCT"/rs6000aix ] ; then
    echo "  RS6000/AIX version 4.1.1"
  fi
  exit 1
fi

#
# Setup configuration specific environment variables
#
ACRO_EXEC_LIB="$ACRO_TOPLEVEL_DIR/AcroLib/$ACRO_CONFIG"
case "$ACRO_CONFIG" in
  sparcsun|sparcsolaris|mipsirix)
    LD_LIBRARY_PATH="`prepend "$ACRO_EXEC_LIB" "$LD_LIBRARY_PATH"`"
    export LD_LIBRARY_PATH
    ;;
  hppahpux)
    SHLIB_PATH="`prepend "$ACRO_EXEC_LIB" "$SHLIB_PATH"`"
    export SHLIB_PATH
    ;;
  rs6000aix)
    LIBPATH="`prepend "$ACRO_EXEC_LIB" "$LIBPATH"`"
    export LIBPATH
    ;;
esac


#
# Set the command and exec.
#
ACRO_EXEC_CMD="$ACRO_TOPLEVEL_DIR/$ACRO_PRODUCT/$ACRO_CONFIG/bin/$BASENAME_ARGV0"
ACRO_Y2KFIX_CMD="$ACRO_TOPLEVEL_DIR/$ACRO_PRODUCT/$ACRO_CONFIG/bin/distill.y2kfix.pl"

if [ -f "$ACRO_EXEC_CMD" ] ; then
  ACRO_RES_DIR=`sed -n -e "s|^ACRO_RES_DIR=||p" \
          $ACRO_TOPLEVEL_DIR/$ACRO_PRODUCT/Xtras/ACRO_RES_DIR`
  ACRO_RES_DIR=`eval echo $ACRO_RES_DIR`
  if [ "$BASENAME_ARGV0" = "distilld" ] ; then 
    UPR_DIRS="`get_upr_dirs "$ACRO_RES_DIR" "$PSRESOURCEPATH"`"
    ACRO_SYS_LOG="`get_resource "$UPR_DIRS" "DstlrSysLog"`"
    ACRO_SYS_LOG=`echo $ACRO_SYS_LOG | sed -e "s|\\\\\%|%|g"`
    ACRO_SYS_CFG="`get_resource "$UPR_DIRS" "DstlrSysConfig"`"
    ACRO_SYS_CFG=`echo $ACRO_SYS_CFG | sed -e "s|\\\\\%|%|g"`
    export ACRO_SYS_LOG
    export ACRO_SYS_CFG
    export ACRO_RES_DIR
    export BASENAME_ARGV0
    exec "$ACRO_EXEC_CMD" ${1+"$@"}
  else
    export ACRO_RES_DIR
    export BASENAME_ARGV0
    "$ACRO_EXEC_CMD"   ${1+"$@"} | "$ACRO_Y2KFIX_CMD" 
    "$ACRO_Y2KFIX_CMD" ${1+"$@"}
  fi
else
  echo "ERROR: $prod not installed for this configuration, \"$ACRO_CONFIG\"."
  exit 1
fi

