#!/bin/sh
# postinst script for gnuspool
#
#   Copyright 2009 Free Software Foundation, Inc.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 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_gnuspool() {
    chown gnuspool:daemon $*
    chmod 755 $*
}

perm_suid_gnuspool() {
    chown gnuspool:daemon $*
    chmod 4755 $*
}

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

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

perm_sugid_gnuspool() {
    chown gnuspool:daemon $*
    chmod 6755 $*
}

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

fixperms() {
    origdir=`pwd`

    cd /var/spool
    perm_gnuspool gnuspool

    cd /usr/bin
    perm_suid_gnuspool gspl-rpr gspl-pr gspl-charge gspl-pq gspl-start gspl-user gspl-ulist \
        gspl-uchange gspl-plist gspl-qchange gspl-qlist gspl-qdel gspl-stop
    perm_suid_gnuspool gspl-xpq gspl-xuser
    perm_suid_gnuspool gspl-lpq gspl-lprm
    perm_suid_gnuspool gspl-lpcover gspl-lpstatc

    cd /usr/sbin
    perm_gnuspool gspl-ptrinstall gspl-pmenu gspl-hostedit gspl-xhostedit gspl-passwd
    perm_gnuspool gspl-cjlist gspl-cplist gspl-spuconv gspl-ripc gspl-setspdir gspl-netwcmd
    perm_gnuspool gspl-getsnmp

    cd /usr/lib/gnuspool
    perm_gnuspool .
    perm_suid_root spshed spexec spmdisp sppwchk xtnetserv
    perm_suid_gnuspool spd spjobdump
    perm_gnuspool spdinit
    perm_sgid_tty spwrite dosspwrite
    perm_gnuspool pclbanner psbanner
    perm_gnuspool gtksave
    perm_suid_root xtlprm xtlpd xtlpq xtlpc
    perm_gnuspool xtlhp xtftp xtelnet
    perm_suid_root xilp

    cd /usr/lib/gnuspool/cgi-bin
    perm_gnuspool .
    perm_sugid_gnuspool sqcgi sqvcgi sqdcgi sqccgi sqcrcgi spcgi spccgi
    #perm_sugid_root rsqcgi rsqccgi rsqcrcgi rsqdcgi rsqvcgi rspcgi rspccgi
    
    cd /usr/share/gnuspool
    chown -R gnuspool:daemon .

    cd $origdir
}

case "$1" in
    configure)
        if ! id -g lp > /dev/null 2>&1 ; then
            addgroup --system lp
        fi
        if ! id -u gnuspool > /dev/null 2>&1 ; then
            adduser --system --no-create-home --home /var/spool/gnuspool --gecos 'GNUspool system user,,,' --ingroup lp gnuspool
        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
