#!/bin/sh

# This script adds a new virtual server from the command line
# It's designed for ISPs who might need to create a lot of servers

usage()
{
  echo
  echo "Adds a new virtual server"
  echo Usage: $0 '<name> <docroot> <ipname> <port>'
  echo
  exit
}

# Note we still allow five arguments as we used to take a password up to 
# Zeus 3.1.x, and people may have provisioning scripts that pass it
if [ $# -ne 4  -a $# -ne 5  ]
then
  usage
fi

# Take the arguments
NAME=$1
DOCROOT=$2
IPNAME=$3
PORT=$4  

# not used, but here to stop people using it for something else
PASSWD=$5


# check ZEUSHOME is set
if [ -z "$ZEUSHOME" ]
then
  echo "Please set \$ZEUSHOME to point to the directory under which Zeus applications are installed"
  exit 1
fi

if [ ! -d $ZEUSHOME/webadmin ]
then
    echo "This script should only be run on machines with the webadmin package on"
    exit 1
fi

PATH=$ZEUSHOME/webadmin/bin:$PATH
cd $ZEUSHOME

CONFFILE=$ZEUSHOME/webadmin/conf/sites/$NAME
CONFDIR=$ZEUSHOME/webadmin/conf/sites/
if [ -f $CONFFILE ]
then
  echo "Server $NAME already exists"
  exit 1
fi

# Copy across the defaults from the skel file
if [ -w $CONFDIR ]; then
   cp $ZEUSHOME/webadmin/conf/skel $CONFFILE
else
   echo "Cannot write to $CONFFILE: Permission denied"
   exit 1
fi

# add the extra settings
echo "docroot $DOCROOT" >> $CONFFILE
echo "ip_name $IPNAME"  >> $CONFFILE
echo "port $PORT"       >> $CONFFILE

