#!/bin/sh
#
# $OpenBSD: INSTALL,v 1.1 2004/02/16 12:33:18 jakob Exp $
#
# Pre/post-installation setup of stunnel

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/usr/local}
CONFIG_DIR=/etc/stunnel
SAMPLE_CONFIG_DIR=$PREFIX/share/examples/stunnel
CHROOT_DIR=/var/stunnel

STUNNELUSER=_stunnel
STUNNELGROUP=_stunnel
STUNNELUID=528
STUNNELGID=528

do_usergroup_install()
{
  # Create stunnel user and group
  groupinfo -e $STUNNELGROUP
  if [ $? -eq 0 ]; then
    echo "===> Using $STUNNELGROUP group for stunnel"
  else
    echo "===> Creating $STUNNELGROUP group for stunnel"
    groupadd -g $STUNNELGID $STUNNELGROUP
  fi
  userinfo -e $STUNNELUSER
  if [ $? -eq 0 ]; then
    echo "===> Using $STUNNELUSER user for stunnel"
  else
    echo "===> Creating $STUNNELUSER user for stunnel"
    useradd -u $STUNNELUID -g $STUNNELGROUP -d $CHROOT_DIR \
      -L daemon -c 'stunnel account' -s /sbin/nologin $STUNNELUSER
  fi
}

do_chroot_dir_install()
{
  install -d -o root -g wheel -m 755 $CHROOT_DIR
}

do_notice_conf()
{
  echo
  echo " The existing $1 configuration files in $CONFIG_DIR have NOT"
  echo " been changed.  You may want to compare them to the current samples in"
  echo " $SAMPLE_CONFIG_DIR, and update your configuration"
  echo " files as needed."
  echo
}

do_install_conf()
{
  install -d -o root -g wheel -m 755 $CONFIG_DIR
  install -o root -g wheel -m 644 $SAMPLE_CONFIG_DIR/stunnel.conf-sample \
    $CONFIG_DIR/stunnel.conf
  echo
  echo " An $1 sample configuration file has been installed in $CONFIG_DIR."
  echo " Please view this file and change the configuration to meet your needs."
  echo
}

# verify proper execution
#
if [ $# -ne 2 ]; then
    echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
    exit 1
fi

# Verify/process the command
#
case $2 in
    PRE-INSTALL)
	do_usergroup_install
	;;
    POST-INSTALL)
	if [ ! -d $CHROOT_DIR ]; then
	  do_chroot_dir_install
	fi
	if [ ! -d $CONFIG_DIR ]; then
	   do_install_conf $1
	elif [ ! -f $CONFIG_DIR/stunnel.conf ]; then
	   do_install_conf $1
	else
	   do_notice_conf $1
	fi
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
