#!/bin/sh
#
#pragma ident  "@(#)sdm.script 2.2  02/12/04 SMI"
#
# Copyright (c) 2001-2006 by Sun Microsystems, Inc.
# All rights reserved.
#

LC_ALL=C; export LC_ALL
LANG=C; export LANG

SDM_VERSION='2.0'

# The script starts Sun (TM) Download Manager

echo "Sun (TM) Download Manager ${SDM_VERSION}"
echo "Copyright (c) 2001-2006 by Sun Microsystems, Inc."
echo "All rights reserved."

get_sdm_home() {
  # Setup SDM_HOME variable if it isn't set
  if [ -z "${SDM_HOME}" ]
  then

    # this approach is better than hardcoded '.', because it allows
    # users to move sdm around and doesn't force them to always start
    # it from sdm directory
    SDM_SCRIPT_DIR=`dirname "$0"`
    # whatever it is, get the real full path
    SDM_HOME=`cd "${SDM_SCRIPT_DIR}" && pwd`
    # that is the question: if $0 is symlink, do we want to resolve it?
    # we better not, let's assume they know what they doing

    if [ -z "${SDM_HOME}" ]; then
      # that should never happen. anyway _now_ '.' could be the best choice.
      # or should we try `pwd`? I think `pwd` is better. it will help us
      # to find jar file later
      SDM_HOME=`pwd`
    fi

  fi
}

get_sdm_jar() {
  # Check that Java classes for the Sun (TM) Download Manager are available
  if [ -s "${SDM_HOME}/sdm.jar" ]; then
    return
  else
    _expected="${SDM_HOME}/sdm.jar"
    # hehe, they don't keep jar file and shell script together. good guys.
    # maybe it's standard unix layout? let's do some AI here
    # but don't do anything if they supplied SDM_HOME for us -- assume they
    # know what are they doing. If they have defined SDM_HOME, then
    # SDM_SCRIPT_DIR will not be there
    if [ -n "${SDM_SCRIPT_DIR}" ]; then
      # SDM_HOME was not defined
      _mydir=`dirname "${SDM_HOME}"`
      # let's try a couple of standard locations
      _jar_subdir="share/sdm-${SDM_VERSION} share/sdm share lib/sdm-${SDM_VERSION} lib/sdm lib"
      for dir in ${_jar_subdir}; do
        SDM_HOME="${_mydir}/${dir}"
        if [ -s "${SDM_HOME}/sdm.jar" ]; then
          unset _expected _jar_subdir _mydir
          return
        else
          _expected="${_expected}, ${SDM_HOME}/sdm.jar"
        fi
      done
      unset _jar_subdir _mydir
    fi
  fi

  echo "ERROR: Unable to find Java classes for the Sun (TM) Download"
  echo "ERROR: Manager. Please, setup the SDM_HOME environment variable,"
  echo "ERROR: or put the script in the home directory of the utility."
  echo "ERROR: expected jar file locations:"
  echo "ERROR: ${_expected}"
  exit 1
}

get_sdm_prop() {
  # Setup SDM_PROP environment variable if it isn't set
  if [ -z "${SDM_PROP}" ]; then

    # by default user settings MUST go to the user directory
    SDM_PROP="${HOME}/.SunDownloadManager"

    # check if the prefs dir is there
    if [ ! -d "${SDM_PROP}" ]; then
      # ok, here is the problem, not all build-in tests understand -e
      # maybe use /usr/bin/test ??? well, if SDM_PROP exists, mkdir
      # will say "exists but not a directory anyway...
      #if [ -e "${SDM_PROP}" ]; then
        # it's NOT a directory, but it exists
        #echo "ERROR: '${SDM_PROP}' must be a directory!"
        #exit 1
      #fi
      # if it's not there yet -- create one
      mkdir -p "${SDM_PROP}"
      if [ $? -ne 0 ]; then
        echo "ERROR: cannot create '${SDM_PROP}' directory!"
        exit 1
      fi
    fi

  else

    # if they do have SDM_PROP defined already, it's a good idea to check,
    # is it a directory and is it writable or not
    if [ -d "${SDM_PROP}" ]; then
      touch "${SDM_PROP}/.sdm.props.dir.test.$$"
      if [ $? -ne 0 ]; then
        echo "ERROR: SDM_PROP='${SDM_PROP}' is read only!"
        exit 1
      fi
      rm -f "${SDM_PROP}/.sdm.props.dir.test.$$"
    else
      # assume that they know what are they doing if they want to
      # redefine default SDM_PROP, and do not create missing dir
      # automatically, just report
      echo "ERROR: SDM_PROP must be a directory name!"
      exit 1
    fi

  fi
}

sdm_install_file() {
  # install_file source target
  _target_dir=`dirname "$2"`
  if [ ! -d "${_target_dir}" ]; then
    mkdir -p "${_target_dir}" || return 1
  fi
  unset _target_dir
  if [ ! -f "$2" ]; then
    cp "$1" "$2" || return 1
  fi
  return 0
}


################################################################################
## actual work starts here
################################################################################

get_sdm_home

get_sdm_jar

get_sdm_prop

# Let's check the SDMOnline.properties.
 if [ ! -f "${SDM_PROP}/SDMOnline.properties" ]
 then
       # Copy the SDMOnline.properties from original package.
       # Don't care about result as SDM's Java code will try
       # to regenerate SDMOnline.properties if doesn't exist.
       /usr/bin/cp "${SDM_HOME}/SDMOnline.properties" \
               "${SDM_PROP}/SDMOnline.properties" >/dev/null 2>&1
 fi


# Update CLASSPATH environment variable
# don't add just ':' if CLASSPATH is not set
CLASSPATH=${CLASSPATH:+$CLASSPATH:}${SDM_HOME}/sdm.jar
export CLASSPATH

echo "Starting Java interpreter..."

# Show version of Java interpreter
java -version
if [ $? -ne 0 ]
then
  echo "ERROR: Java interpreter reported a problem. Please, check the"
  echo "ERROR: output log."
  # what's "output log"? maybe "check the output above" is better?
  exit 1
fi

# Run the Sun (TM) Download Manager in Java interpreter
# do exec, so user won't have shell process hanging around just to check the
# exit status. usually those who can understand what does the "Java interpreter
# reported a problem" mean are smart enough to check $?, other users will be
# confused anyway. If user has a problem with JDK, previous call should
# catch it.
exec java \
  "-Dhomedir=${SDM_HOME}" "-Dpropdir=${SDM_PROP}" \
  com.sun.sdm.SunDownloadManager $*

# EOF
