#!/bin/sh
#
# Virtual server startup script run by FreeVSD
# All user configurable behaviour is in /etc/rc.conf.

PATH=/usr/local/bin:/sbin:/usr/bin:/usr/sbin:/bin
export PATH

# Clean /tmp on startup
(cd /tmp && rm -rf *)

# Remove stale lock and pid files
rm -f /var/run/*.pid /var/lock/LCK* /var/lock/subsys/* /tmp/wrp*
# Remove dead pop sessions
rm -f /var/spool/mail/.[A-Za-z0-9]* 

>/var/log/utmp
if [ ! -f "/var/log/wtmp" ]; then
    >/var/log/wtmp
fi 

# Execute port redirection for HTTP and HTTPS
# Obtain hosts IP address, and strip any aliases
IP_ADDR=`hostname -i | sed -e 's/\(.*\) .*/\1/'`

# HTTP redirect:
vsdredirect ${IP_ADDR}:80:8080
# HTTPS redirect:
vsdredirect ${IP_ADDR}:443:8443

# Virtual server startup
if [ -f /etc/rc.vsd ]; then
    su - admin -c "/bin/sh /etc/rc.vsd"
fi

# Read the virtual server configuration file
if [ -f /etc/rc.conf ]; then
    . /etc/rc.conf
fi

# Start the SSH daemon. This is run as root because SSH gripes about 
# not being root when logging as users other than admin in the VS.
if [ "${sshd_enable}" = "YES" ]; then
  echo -n 'starting ssh server: '
  if [ -x "/usr/sbin/sshd" ]; then sshd_path="/usr/sbin/sshd"; fi
  ${sshd_path} &
  echo 'sshd'
fi

exit 0
