#!/bin/sh
#  $Id: status_cdm.sh 103141 2005-06-20 19:23:10Z peterc $
#  Copyright (C) 1999-2000 The Santa Cruz Operation
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: sscript to describe the state of the LDA server
#  Author: duncans
#
#  Syntax: status cdm [--quiet]
#
#  there is a well defined set of exit codes
#  0 = running
#  1 = not running
#  2 = not enabled
#  3+ = error running script
EXIT_RUNNING=0
EXIT_NOT_RUNNING=1
EXIT_DISABLED=2
EXIT_ERROR=3

if [ -f $TTAMSG/common -a -f $TTAMSG/status_cdm ]; then
  . $TTAMSG/common
  . $TTAMSG/status_cdm
else
  echo "Unable to open message file: $TTAMSG/status_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

## see if cdm is enabled
cdmfile=$TTADATADIR/serverconfig/global/cdm.properties
if [ ! -f "$cdmfile" ]; then
  if [ "$verbose" = "true" ]; then echo "$status_cdm_no_propfile"; fi
  exit $EXIT_DISABLED
fi

# start the service even if it is disabled
# %%propstoshellvars $cdmfile
# cdmenabled=${tarantella_config_cdm_enabled:-0}
# 
# if [ "$cdmenabled" != 1 -a "$cdmenabled" != true ]; then
#   if [ "$verbose" = "true" ]; then echo "$status_cdm_disabled"; fi
#   exit $EXIT_DISABLED
# fi

# if no pidfile, have to assume it's not running
pidfile=$TTADATADIR/info/cdmserver.pid
if [ ! -f "$pidfile" ]; then
  if [ "$verbose" = "true" ]; then echo "$status_cdm_no_pidfile"; fi
  exit $EXIT_NOT_RUNNING
fi

# check the process in the pidfile is still running
kill -0 `cat $pidfile` 1>/dev/null 2>&1
if [ "$?" != "0" ]; then
  if [ "$verbose" = "true" ]; then echo "$status_cdm_no_process"; fi
  exit $EXIT_NOT_RUNNING
fi

if [ "$verbose" = "true" ]; then
  # contact the port to get the full info on connections
  echo "$status_cdm_running"
fi

# everything is fine
exit $EXIT_RUNNING
