#!/bin/bash
#
# syslogd-config, Copyright 2005 Corinna Vinschen
#
# This file is part of the Cygwin port of Berkley inetutils.

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

_required_csih_version=0.9.7
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

source ${CSIH_SCRIPT}
if ! csih_version_ge $csih_VERSION $_required_csih_version
then
  iuVER=$(cygcheck -cd inetutils | sed -n -e '/inetutils/p' | awk '{print $2}')
  csih_error_multi "iu-config version $iuVER requires csih-$_required_csih_version"
    "or above. You have csih-${csih_VERSION}. Please update the csih package."
fi

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

  # Check if syslog-ng is installed and remove on user request.
  if cygrunsrv -Q syslog-ng > /dev/null 2>&1
  then
    csih_warning "The syslog-ng service is already installed.  You can not"
    csih_warning "run both, syslog-ng and syslogd in parallel."
    echo
    if csih_request "Do you want to deinstall the syslog-ng service in favor of syslogd?"
    then
      cygrunsrv -E syslog-ng
      cygrunsrv -R syslog-ng
    fi
  fi

  # Install syslogd service if it is not already installed
  if ! cygrunsrv -Q syslogd > /dev/null 2>&1
  then
    echo
    csih_warning "The following function requires administrator privileges!"
    if csih_request "Do you want to install syslogd as service?"
    then
      if cygrunsrv -I syslogd -d "CYGWIN syslogd" -p /usr/sbin/syslogd -a -D
      then
	  echo
	  csih_inform "The syslogd service has been installed under the LocalSystem"
	  csih_inform "account (also known as SYSTEM). To start the service now, call"
        csih_inform "\`net start syslogd' or \`cygrunsrv -S syslogd'. Otherwise, it"
        csih_inform "will start automatically after the next reboot."
	  echo
	  csih_inform "Check ${SYSCONFDIR}/syslog.conf first, if it suits your needs."
	  echo
	  csih_inform "Keep in mind that any file mentioned in ${SYSCONFDIR}/syslog.conf"
	  csih_inform "must exist and be readable and writable for the SYSTEM account."
	  csih_inform "Oh and, use tabs, not spaces in ${SYSCONFDIR}/syslog.conf..."
      fi

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

    fi # user allowed us to install syslogd
  fi # syslogd already installed
} # --- 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
#       SYSLOGD_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 "${SYSLOGD_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 syslogd 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!
# ======================================================================

# explicitly invoke _csih_setup early, to ensure basic directories
# exist and have correct permissions (/etc, /var/[log|run|empty])
_csih_setup

# Check for ${DEVDIR} directory
csih_make_dir "${DEVDIR}" "syslogging using syslogd will not work."
chmod 775 "${DEVDIR}"               >&/dev/null || /bin/true
setfacl -m u:system:rwx "${DEVDIR}" >&/dev/null || /bin/true
setfacl -m g:544:rwx "${DEVDIR}"    >&/dev/null || /bin/true
csih_check_access "${DEVDIR}" "rwx"

# Validate permissions on configuration file
chmod 644 "${SYSCONFDIR}/syslog.conf"               >&/dev/null || /bin/true
setfacl -m u:system:rw- "${SYSCONFDIR}/syslog.conf" >&/dev/null || /bin/true
setfacl -m g:544:rw- "${SYSCONFDIR}/syslog.conf"    >&/dev/null || /bin/true
csih_check_access "${SYSCONFDIR}/syslog.conf" "rw."

# Create /var/log/messages if it doesn't exist.
touch "${LOGDIR}/messages"                   >&/dev/null || /bin/true
chmod 644 "${LOGDIR}/messages"               >&/dev/null || /bin/true
setfacl -m u:system:rw- "${LOGDIR}/messages" >&/dev/null || /bin/true
setfacl -m g:544:rw- "${LOGDIR}/messages"    >&/dev/null || /bin/true
csih_check_access "${LOGDIR}/messages" "rw."

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


echo
echo "Configuration finished. Have fun!"

