#!/bin/bash
#
# init-config, Copyright 2010 Dr. Volker Zell
#
# This file is part of the Cygwin port of sysvinit.

# ======================================================================
# Initialization
# ======================================================================
PROGNAME=$(basename $0)
_tdir=$(dirname $0)
PROGDIR=$(cd $_tdir && pwd)

CSIH_SCRIPT=/usr/share/csih/cygwin-service-installation-helper.sh

# Subdirectory where the new package is being installed
PREFIX=/usr

# Directory where the config files are stored
SYSCONFDIR=/etc
DEVDIR=/dev
LOGDIR=/var/log
RUNDIR=/var/run
LOCKDIR=/var/lock/subsys

source ${CSIH_SCRIPT}

# ======================================================================
# Routine: install_service
#   Install init as a service
# ======================================================================
install_service() {

  if csih_is_nt
  then

    # Check if init is installed and remove on user request.
    if cygrunsrv -Q init > /dev/null 2>&1
    then
      csih_warning "The init service is already installed."
      echo
      if csih_request "Do you want to reinstall it with different args?"
      then
        cygrunsrv -E init
        cygrunsrv -R init
      fi
    fi

    # Install init service if it is not already installed
    if ! cygrunsrv -Q init > /dev/null 2>&1
    then
      echo
      csih_warning "The following function requires administrator privileges!"
      if csih_request "Do you want to install init as service?"
      then
        if cygrunsrv -I init -d "CYGWIN init" -p /sbin/init -a -i -s INT
        then
	  echo
	  csih_inform "The init service has been installed under the LocalSystem"
	  csih_inform "account (also known as SYSTEM). To start the service now, call"
          csih_inform "\`net start init' or \`cygrunsrv -S init'. Otherwise, it"
          csih_inform "will start automatically after the next reboot."
	  echo
	  csih_inform "Check ${SYSCONFDIR}/inittab and ${SYSCONFDIR}/rc first, if they suit your needs."
        fi

        # now, if successfully installed, do some final configuration
        if cygrunsrv -Q init >/dev/null 2>&1
        then
          chown SYSTEM.544 "${SYSCONFDIR}/inittab"
          chown SYSTEM.544 "${SYSCONFDIR}/rc"
        else
          csih_warning "Something went wrong installing the init service." 
        fi

      fi # user allowed us to install init
    fi # init already installed
  fi # csih_is_nt
} # --- End of install_service --- #


# ======================================================================
# Main Entry Point
# ======================================================================


# Check how the script has been started.  If
#   (1) it has been started by giving the full path and
#       that path is /etc/postinstall, OR
#   (2) Otherwise, if the environment variable
#       CONFIG_AUTO_ANSWER_NO is set
# then set auto_answer to "no".  This allows automatic
# creation of the config files in /etc w/o overwriting
# them if they already exist.  In both cases, color
# escape sequences are suppressed, so as to prevent
# cluttering setup's logfiles.
if [ "$PROGDIR" = "/etc/postinstall" ]
then
  csih_auto_answer="no"
  csih_disable_color
fi
if [ -n "${CONFIG_AUTO_ANSWER_NO}" ]
then
  csih_auto_answer="no"
  csih_disable_color
fi


# ======================================================================
# Parse options
# ======================================================================
while :
do
  case $# in
  0)
    break
    ;;
  esac

  option=$1
  shift

  case "$option" in
  -d | --debug )
    set -x
    csih_trace_on
    ;;

  -y | --yes )
    csih_auto_answer=yes
    ;;

  -n | --no )
    csih_auto_answer=no
    ;;

  *)
    echo "usage: ${PROGNAME} [OPTION]..."
    echo
    echo "This script creates a basic init configuration."
    echo
    echo "Options:"
    echo "    --debug  -d     Enable shell's debug output."
    echo "    --yes    -y     Answer all questions with \"yes\" automatically."
    echo "    --no     -n     Answer all questions with \"no\" automatically."
    echo
    exit 1
    ;;

  esac
done

# ======================================================================
# Action!
# ======================================================================

# Check for ${SYSCONFDIR} directory
csih_make_dir "${SYSCONFDIR}" "Cannot create global configuration files."
chmod 775 "${SYSCONFDIR}"
setfacl -m u:system:rwx "${SYSCONFDIR}"

# Check for ${DEVDIR} directory
csih_make_dir "${DEVDIR}" "Syslogging using init will not work."
chmod 775 "${DEVDIR}"
setfacl -m u:system:rwx "${DEVDIR}"

# Check for ${LOGDIR} directory
csih_make_dir "${LOGDIR}" "Syslogging using init will not work."
chmod 775 "${LOGDIR}"
setfacl -m u:system:rwx "${LOGDIR}"

# Check for ${RUNDIR} directory
csih_make_dir "${RUNDIR}" "PID files of running processes will not be created."
chmod 775 "${RUNDIR}"
setfacl -m u:system:rwx "${RUNDIR}"

# Check for ${LOCKDIR} directory
csih_make_dir "${LOCKDIR}" "LOCK files of running processes will not be created."
chmod 775 "${LOCKDIR}"
setfacl -m u:system:rwx "${LOCKDIR}"

# Create /var/run/utmp if it doesn't exist.
touch ${RUNDIR}/utmp
chmod 644 ${RUNDIR}/utmp
setfacl -m u:system:rw- ${RUNDIR}/utmp

# Create /var/log/wtmp if it doesn't exist.
touch ${LOGDIR}/wtmp
chmod 644 ${LOGDIR}/wtmp
setfacl -m u:system:rw- ${LOGDIR}/wtmp

# Check if /etc/inittab exists. If yes, ask for overwriting
if csih_request "Do you want to overwrite existing ${SYSCONFDIR}/inittab file?"
then
  rm -f "${SYSCONFDIR}/inittab"
  if [ -f "${SYSCONFDIR}/inittab" ]
  then
    echo "Can't overwrite. ${SYSCONFDIR}/inittab is write protected."
  fi
fi

if [ ! -f "${SYSCONFDIR}/inittab" ]
then
  echo "Creating default ${SYSCONFDIR}/inittab file"
  cat > ${SYSCONFDIR}/inittab << EOF
# id:runlevels:action:process
id:3:initdefault:
rc::bootwait:/etc/rc
#S0:2345:respawn:/sbin/agetty -L -T vt100 -n ttyS0 9600 ttyS0
EOF
fi
chmod 644 "${SYSCONFDIR}/inittab"
setfacl -m u:system:rw- "${SYSCONFDIR}/inittab"

# Check if /etc/rc exists. If yes, ask for overwriting
if csih_request "Do you want to overwrite existing ${SYSCONFDIR}/rc file?"
then
  rm -f "${SYSCONFDIR}/rc"
  if [ -f "${SYSCONFDIR}/rc" ]
  then
    echo "Can't overwrite. ${SYSCONFDIR}/rc is write protected."
  fi
fi

if [ ! -f "${SYSCONFDIR}/rc" ]
then
  echo "Creating default ${SYSCONFDIR}/rc file"
  cat > ${SYSCONFDIR}/rc << EOF
#!/bin/sh
# Clean up utmp database
>/var/run/utmp
#rm -rf /tmp/*
EOF
fi
chmod 755 "${SYSCONFDIR}/rc"
setfacl -m u:system:rwx "${SYSCONFDIR}/rc"

# maybe: csih_auto_answer=no will skip,
# interactive user will get a chance to override
install_service


echo
echo "Configuration finished. Have fun!"
