#!/bin/sh
# postinst script for test
#
# see: dh_installdeb(1)

# TODO: For now disable "set -e" since I have not figured out how to get sqlite3 to SHUT UP!
#set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

configure()
{
DATE=$(date --rfc-3339='date')
CONFIG_BACKUP=/etc/OpenBTS/OpenBTS.dump-$DATE

# add user openbts
if ! getent passwd openbts > /dev/null ; then
	echo 'Adding system user for OpenBTS' 1>&2
	adduser --system --group --quiet \
		--home /home/openbts \
		--no-create-home --disabled-login \
		--gecos "OpenBTS daemon" \
		openbts
fi

# add user openbts to some groups
for group in sudo asterisk www-data; do
	if groups openbts | grep -w -q -v $group; then
		adduser openbts $group
	fi
done

if [ ! -e $CONFIG_BACKUP ]; then
        sqlite3 /etc/OpenBTS/OpenBTS.db ".dump" > $CONFIG_BACKUP
fi

sqlite3 /etc/OpenBTS/OpenBTS.db ".read /etc/OpenBTS/OpenBTS.example.sql" > /dev/null 2>&1

chown openbts:openbts /home/openbts/openbtsconfig

}

case "$1" in
    configure)
	configure
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
