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

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

perm_gnubatch() {
    chown gnubatch:daemon $*
    chmod 755 $*
}

perm_suid_gnubatch() {
    chown gnubatch:daemon $*
    chmod 4755 $*
}

perm_suid_root() {
    chown root:root $*
    chmod 4755 $*
}

perm_sgid_tty() {
    chown gnubatch:tty $*
    chmod 2755 $*
}

perm_sugid_gnubatch() {
    chown gnubatch:daemon $*
    chmod 6755 $*
}

perm_sugid_root() {
    chown root:daemon $*
    chmod 6755 $*
}

fixperms() {
    origdir=`pwd`

    cd /var/spool
    perm_gnubatch gnubatch

    cd /usr/bin
    perm_suid_gnubatch  gbch-charge gbch-cichange gbch-cilist gbch-hols gbch-jchange \
			gbch-jdel gbch-jlist gbch-jstat gbch-q gbch-quit gbch-r \
			gbch-rr gbch-start gbch-uchange gbch-ulist gbch-user gbch-var \
			gbch-vlist gbch-xq gbch-xr gbch-xuser

    perm_gnubatch gbch-filemon gbch-xfilemon gbch-atcover

    cd /usr/sbin
    perm_gnubatch gbch-pmenu gbch-hostedit gbch-passwd
    perm_gnubatch gbch-btuconv gbch-ciconv gbch-cjlist gbch-cvlist gbch-ripc

    cd /usr/lib/gnubatch
    perm_gnubatch .
    perm_suid_root btsched btexec btpwchk btmdisp xbnetserv bgtkldsv
    perm_suid_gnubatch jobdump
    perm_sgid_tty btwrite dosbtwrite
    perm_gnubatch bgtksave

    cd /usr/lib/gnubatch/cgi-bin
    perm_gnubatch .
    perm_sugid_gnubatch btjccgi btjcgi btjcrcgi btjdcgi btjvcgi btvccgi btvcgi
    #perm_sugid_root rbtjcgi rbtjccgi rbtjdcgi rbtjvcgi rbtjcrcgi rbtvcgi rbtvccgi
    
    cd /usr/share/gnubatch
    chown -R gnubatch:daemon .

    cd $origdir
}

case "$1" in
    configure)
        if ! id -u gnubatch > /dev/null 2>&1 ; then
            adduser --system --no-create-home --home /var/spool/gnubatch --gecos 'GNUbatch system user,,,' --ingroup daemon gnubatch
        fi
        fixperms
    ;;

    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
