#!/bin/sh -e

if [ "$1" = "configure" ]; then
    SEQFILE=/var/spool/cron/atjobs/.SEQ
    if [ ! -f $SEQFILE ]; then
	echo 0 > $SEQFILE
	chmod 600 $SEQFILE
	chown daemon:daemon $SEQFILE
    fi

    if [ -z "$2" ]; then
	update-rc.d atd multiuser 89 11 > /dev/null
    else
	# Remove shutdown and reboot links; this init script does not need them.
	if dpkg --compare-versions "$2" lt "3.1.10ubuntu2"; then
	    rm -f /etc/rc0.d/K11atd /etc/rc6.d/K11atd
	fi

	if dpkg --compare-versions "$2" lt 3.1.9; then
	    chown daemon:daemon /var/spool/cron/atjobs /var/spool/cron/atspool /var/spool/cron/atjobs/.SEQ
	    chmod 1770 /var/spool/cron/atjobs /var/spool/cron/atspool 
	    # If we upgrade from an older version which still had a setuid root
	    # /usr/bin/at, change the /etc/at.deny permissions (since dpkg does not do it
	    # automatically).
	    if [ -f /etc/at.deny ] && ! dpkg-statoverride --list /etc/at.deny > /dev/null; then
		chown root:daemon /etc/at.deny
		chmod 0640 /etc/at.deny
	    fi
	    if [ -f /etc/at.allow ] && ! dpkg-statoverride --list /etc/at.allow > /dev/null; then
		chown root:daemon /etc/at.allow
		chmod 0640 /etc/at.allow
	    fi
	fi
    fi
fi

invoke-rc.d atd start
