#!/bin/sh
. /usr/share/debconf/confmodule
db_version 2.0

set -e

NAME=inadyn
RUNDIR=/run/$NAME
CACHEDIR=/var/cache/$NAME
CONF=/etc/$NAME.conf

if [ "$1" = "configure" ]
then
	# Create debian-inadyn group if it isn't already there
	if ! getent group debian-inadyn >/dev/null
	then
		# Add system group: debian-inadyn
		addgroup --system debian-inadyn >/dev/null
	fi

	# Create debian-inadyn user if it isn't already there
	if ! getent passwd debian-inadyn >/dev/null
	then
		# Add system user: debian-inadyn
		adduser \
		 --system \
		 --ingroup debian-inadyn \
		 --home $RUNDIR \
		 --no-create-home \
		 --gecos "inadyn dyndns client" \
		 --shell /bin/false \
		 --disabled-login \
		 --disabled-password \
		 debian-inadyn >/dev/null
	fi

	db_get inadyn/wizard
	if [ "$RET" != "false" ]
	then
	    # Create inadyn.conf if it does not yet exist, or
	    # Recreate if it was created by us
	    if [ ! -f $CONF -o -n "`grep -s "##DEBCONF##" $CONF`" ]
	    then
		db_get inadyn/provider
		provider=$RET
		db_get inadyn/username
		username=$RET
		db_get inadyn/password
		password=$RET
		db_get inadyn/hostname
		hostname=$RET
		cat <<EOF > $CONF
# Inadyn v2.0 configuration file, automatically generated by debconf
# Do not edit this file directly unless you select 'Advanced' during
# your next invocation of dpkg-reconfigure inadyn
#
# NOTE: remove this line if you edit the file manually! ##DEBCONF##
#
# For details on the syntax of this file, see inadyn.conf(5)
period = 300

provider $provider {
	username = $username
	password = $password
	hostname = { $hostname }
}
EOF
		chown root:debian-inadyn $CONF
		chmod 640 $CONF
	    fi
	fi

	if [ -f $CONF ]
	then
            own=`stat --printf="%u" "$CONF" 2>/dev/null`
            grp=`stat --printf="%g" "$CONF" 2>/dev/null`
            if [ "$own" = "0" -a "$grp" = "0" ]
            then
                if ! dpkg-statoverride --list $CONF >/dev/null 2>&1
                then
                    dpkg-statoverride --update --add root debian-inadyn 640 $CONF
                fi
            fi
	fi

        for item in $CACHEDIR
	do
		own=`stat --printf="%u" "$item" 2>/dev/null`
		grp=`stat --printf="%g" "$item" 2>/dev/null`
		if [ "$own" = "0" -a "$grp" = "0" ]
		then
			if ! dpkg-statoverride --list $item >/dev/null 2>&1
			then
				dpkg-statoverride --update --add \
					debian-inadyn debian-inadyn 755 $item
			fi
		fi
	done

        for item in $CACHEDIR/*
        do
                [ "$item" = "$CACHEDIR/*" ] && continue

                own=`stat --printf="%u" "$item" 2>/dev/null`
                grp=`stat --printf="%g" "$item" 2>/dev/null`

                if [ "$own" = "0" -a "$grp" = "0" ]
                then
                        chown debian-inadyn "$item"
                        chgrp debian-inadyn "$item"
                fi
        done

        if [ -d $RUNDIR ]
        then
                for item in $RUNDIR $RUNDIR/*
                do
                        [ "$item" = "$RUNDIR/*" ] && continue

                        own=`stat --printf="%u" "$item" 2>/dev/null`
                        grp=`stat --printf="%g" "$item" 2>/dev/null`

                        if [ "$own" = "0" -a "$grp" = "0" ]
                        then
                                chown debian-inadyn "$item"
                                chgrp debian-inadyn "$item"
                        fi
                done
        fi
fi

#DEBHELPER#

exit 0
