#!/bin/sh
#  $Id: start_cdm.sh 104166 2006-11-07 13:53:28Z mikemc $
#  Copyright (C) 1998-2000 The Santa Cruz Operation
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: start script for visionfs
#  Author: duncans
#
#  Syntax: start cdm [--quiet]
#
# error codes have to be well defined for the other scripts to work
# 0 = server was started
# 1 = server was already running
# 2 = services are disabled
# 3+ = other error
EXIT_SUCCESS=0
EXIT_ALREADY=1
EXIT_DISABLED=2
EXIT_OTHER=3

if [ -f $TTAMSG/common -a -f $TTAMSG/start_cdm ]; then
  . $TTAMSG/common
  . $TTAMSG/start_cdm
else
  echo "Unable to open message file: $TTAMSG/start_cdm"
  exit 1
fi


case "$trace" in
x) set -x
esac

# parse arguments
verbose=true
for o in "$@"; do
  case "$o" in
  --help )
    echo "$lf_usage" 1>&2
    exit $EXIT_OTHER
    ;;
  --quiet )
    verbose=false
    ;;
  esac
done

# Make sure that they are running as root
u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi


# By default we don't want our server to drop core.
# COMMENT OUT THE FOLLOWING LINE IF YOU WANT THE SERVER TO DROP CORE FILES
ulimit -c 0

# cd to the directory containing this script, so that we can use
# relative pathnames
ttacommand=$0
case "$ttacommand" in
/*) cd `dirname $ttacommand`
    ;;
*)  fullpath=`pwd`/$ttacommand
    cd `dirname $fullpath`
esac


# check the status command exists
statusscript=$TTADIR/bin/scripts/status_cdm
if [ ! -x "$statusscript" ]; then
  if [ "$verbose" = "true" ]; then echo "$start_cdm_missing_script$statusscript" 1>&2; fi
  exit $EXIT_OTHER
fi

# ask the status command what state we're in at the moment
$statusscript --quiet
case "$?" in
  0)
    if [ "$verbose" = "true" ]; then echo "$start_cdm_already"; fi
    exit $EXIT_ALREADY
    ;;
  1)
    # fall through and start the server
    ;;
  2)
    if [ "$verbose" = "true" ]; then echo "$start_cdm_disabled"; fi
    exit $EXIT_DISABLED
    ;;
  *)
    if [ "$verbose" = "true" ]; then echo "$start_cdm_status_error"; fi
    exit $EXIT_OTHER
    ;;
esac

# don't do anything unless the jserver is running
  jserverup=false
  if [ -f "$TTADATADIR/info/jserver.pid" ]; then
    # may be more than one entry in the jserver pid file
    for pid in `cat $TTADATADIR/info/jserver.pid`; do
      if ps -p $pid >/dev/null 2>&1 ; then
        jserverup=true
      fi
    done
  fi


if [ "$jserverup" != "true" ]; then
  if [ "$verbose" = "true" ]; then echo "$start_cdm_no_jserver"; fi
  exit $EXIT_OTHER
fi

# the real binary
binname=$TTADIR/bin/bin/ttacdmd
if [ ! -x "$binname" ]; then
  if [ "$verbose" = "true" ]; then echo "$start_cdm_missing_binary$binname" 1>&2; fi
  exit $EXIT_OTHER
fi

# get the asad port number. This is used for the rest of the config
bootstrapfile=$TTADATADIR/serverconfig/global/bootstrap.properties
if [ ! -f "$bootstrapfile" ]; then
  if [ "$verbose" = "true" ]; then echo "$start_cdm_no_propfile$bootstrapfile"; fi
  exit $EXIT_OTHER
fi
[ -s "$bootstrapfile" -a -r "$bootstrapfile" ] && {
eval `tr -d '\\\\' <"$bootstrapfile" | egrep -v "^[ 	]*#|'|^tarantella\.config\.(name|type)=" | sed "/=/!d
:dotloop
s/^\([^=]*\)\.\([^=]*=.*\)\$/\1_\2/
tdotloop
s/^\([^=]*\)=\(.*\)/\1='\2'/"`
}

asadport=${tarantella_config_array_port:-5427}

# tell them we're trying to start
if [ "$verbose" = "true" ]; then echo "$start_cdm_starting"; fi

# start the real binary with a minimal path
$binname $TTADIR $asadport

case "$?" in
0)
  if [ "$verbose" = "true" ]; then echo "$start_cdm_started"; fi
  exit $EXIT_SUCCESS
  ;;
*)
  if [ "$verbose" = "true" ]; then echo "$start_cdm_failed"; fi
  exit $EXIT_OTHER
  ;;
esac
