#!/bin/bash
# Configure the ntp service for the appliance
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
CONFIGOPT=${1}
if [ -f $NETAWSS/.profile.vars ]; then
 . $NETAWSS/.profile.vars
else
 . /var/NAIENV/.profile.vars
fi
PATH=$wsPATH:$PATH
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

create_jail()
{
    JAIL=/var/ntpd.jail

    [[ -e /var/lock/subsys/ntpd ]] && /etc/init.d/ntpd stop

    # Create the jail
    if [[ $JAIL/dev -ef /var/dev ]]; then
	/bin/umount $JAIL/dev || return
    fi
    /bin/rm -rf ${JAIL}
    /bin/mkdir -p ${JAIL}/lib/i686
    /bin/mkdir -p ${JAIL}/etc
    /bin/mkdir -p ${JAIL}/usr/sbin
    /bin/mkdir -p ${JAIL}/etc
    /bin/mkdir -p ${JAIL}/etc/ntp
    /bin/mkdir -m 0777 -p ${JAIL}/tmp
    /bin/mkdir $JAIL/dev && /bin/mount --bind /var/dev $JAIL/dev

    # Create the files in the jail
    /bin/cp -a --parents \
	/etc/host.conf \
	/etc/hosts \
	/etc/localtime \
	/etc/ntp.conf \
	/etc/ntp/step-tickers \
	/etc/resolv.conf \
	/etc/nsswitch.conf \
	/lib/libnss* \
	/lib/libresolv* \
	$JAIL

    # request restart
    [[ -f $WS_RESTART_LIST ]] && echo ntpd >>"$WS_RESTART_LIST"

}

create_sysconfig_ntpd()
{
	echo "OPTIONS=\"-T ${JAIL}\"" > /etc/sysconfig/ntpd
}

start()
{
    # check ntpd is active
    /sbin/chkconfig --level 3 ntpd || return 0	# nothing to do

    # Create the jail
    create_jail

    # Create the configuration options to use the jail
    create_sysconfig_ntpd
}

case $1 in
    (start|restart|reload) start ;;
    (stop|status) : ;;
    (*) echo $"Usage: $0 {start|stop|restart|reload|status}"; exit 1 ;;
esac

exit 0
