#!/bin/sh
# $OpenBSD: INSTALL,v 1.5 2003/12/31 16:03:30 naddy Exp $
#
# Pre/post-installation setup of Bitlbee

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/usr/local}
DB_DIR=/var/bitlbee
CONFIG_DIR=/etc/bitlbee
SAMPLE_CONFIG_DIR=/usr/local/share/examples/bitlbee
BITLBEEUSER=_bitlbee
BITLBEEGROUP=_bitlbee
ID=509

do_notice_existingdir() {
    cat << EOF

+---------------
| You appear to already have a Bitlbee-settings dir in $DB_DIR
|
| This directory has been preserved. If you want to start from
| from the default, you have to perform this step as root:
|
|      # rm -rf $DB_DIR/*
|
+---------------

EOF
}

do_usergroup_install() {
    # Create bitlbee user and group
    if groupinfo -e $BITLBEEGROUP; then
	echo "===> Using $BITLBEEGROUP group for bitlbee"
    else
	echo "===> Creating $BITLBEEGROUP group for bitlbee"
	groupadd -g $ID $BITLBEEGROUP
    fi

    if userinfo -e $BITLBEEUSER; then
	echo "===> Using $BITLBEEUSER user for bitlbee"
    else
	echo "===> Creating $BITLBEEUSER user for bitlbee"
	useradd -g $BITLBEEGROUP \
		-d /nonexistent \
		-L daemon \
		-c 'Bitlbee Account' \
		-s /sbin/nologin \
		-u $ID \
		$BITLBEEUSER \
		    2>&1| grep nonexistant >&2
    fi
}

do_database_install() {
    install -d -m 0770 -o $BITLBEEUSER -g $BITLBEEGROUP $DB_DIR
}

do_manual_notice() {
    cat << EOF
+---------------
| To enable bitlbee you have to add a line like:
|
|    127.0.0.1:6667    stream  tcp     nowait $BITLBEEUSER /usr/local/libexec/bitlbee bitlbee
|
| to your /etc/inetd.conf and restart inetd with
|
|    # kill -HUP \`cat /var/run/inetd.pid\`
|
+---------------

EOF
}

do_warning() {
    cat << EOF
+---------------
| WARNING!
|
| This port has changed the user/group it uses from bitlbee to $BITLBEEUSER.
| If you installed this port before, ensure all permissions are set correctly
| with:
|
|   # ls -ld $DB_DIR
|   # chown -R $BITLBEEUSER:$BITLBEEGROUP $DB_DIR
|
| And don't forget to remove the old user/group.
+---------------

EOF
}

do_config_install() {
    install -d -o root -g wheel -m 755 $CONFIG_DIR
    install -o root -g wheel -m 644 $SAMPLE_CONFIG_DIR/bitlbee.conf $CONFIG_DIR
    install -o root -g wheel -m 644 $SAMPLE_CONFIG_DIR/motd.txt $CONFIG_DIR
    cat << EOF

+---------------
| The $1 configuration file, $CONFIG_DIR/bitlbee.conf,
| has been installed.  Please view this file and change
| the configuration to meet your needs.
+---------------

EOF
}

do_config_notice() {
    cat << EOF

+---------------
| The existing $1 configuration file, $CONFIG_DIR/bitlbee.conf,
| has NOT been changed.  You may want to compare it to the
| current sample file, $SAMPLE_CONFIG_DIR/bitlbee.conf,
| and update your configuration as needed.
+---------------

EOF
}

# Verify/process the command
#
case $2 in
    PRE-INSTALL)
	if [ -d $DB_DIR ]; then
	    do_notice_existingdir
	fi
	do_usergroup_install
	;;
    POST-INSTALL)
	if [ ! -d $DB_DIR ]; then
	    do_database_install
	fi
	do_manual_notice
	do_warning
	if [ ! -f $CONFIG_DIR/bitlbee.conf ]; then
	    do_config_install $1
	else
	    do_config_notice $1
	fi
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
