#!/usr/bin/bash
#
# Copyright 2001-2026 WiKID Systems, Inc.
# License: WiKID Systems, Inc. All Rights Reserved.
#
# Control script designed to allow an easy command line interface
# to controlling WiKID. Not much here right now, since we're just
# wrapping the existing setup script, but stay tuned ...
#
# Adapted from apachectl, written by Marc Slemko, 1997/08/23
# 
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
# 
# pick up any necessary environment variables
if test -f /etc/WiKID/setenv.sh; then
  . /etc/WiKID/setenv.sh
fi
#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

ERROR=0
#if [ "x$ARGV" = "x" ] ; then 
#    ARGV="-h"
#fi

## make sure we've run the basic setup routines
$WIKID_HOME/sbin/sudo.sh root $WIKID_HOME/sbin/wikidserver_config.sh

CTL=$WIKID_HOME/sbin/wikid.pl
if [ "x$1" = "xrestart" ] ; then 
	$CTL stop
	sleep 3
	$CTL start
	ERROR=$?
elif [ "x$1" = "xpgsetup" ]; then
  	cmd=$1
  	shift
  	"$WIKID_HOME/sbin/wikidpg" setup
  	ERROR=$?
elif [ "x$1" = "xconfig" ] || [ "x$1" = "xquick-setup" ]; then
	cmd=$1
	shift 
	java com.wikidsystems.server.QuickSetup $ARGV
	ERROR=$?
	chown -R $WIKID_USER $WIKID_HOME
elif [ -x "$WIKID_HOME/sbin/wikidserver_$1.sh" ]; then
	cmd=$1
	shift 
	"$WIKID_HOME/sbin/wikidserver_$cmd.sh" $ARGV
	ERROR=$?
elif [ -x "$WIKID_HOME/sbin/$1.sh" ]; then
	cmd=$1
	shift 
	"$WIKID_HOME/sbin/$cmd.sh" $ARGV
	ERROR=$?
else
	$CTL $ARGV
	ERROR=$?
fi

exit $ERROR

