#! /bin/sh

if test $# != 1; then
    echo "Usage: $0 <service>" >&2
    exit 1
fi

if test -r /etc/pam.d/$1 || grep "^$1" /etc/pam.conf >/dev/null 2>&1; then
    echo "PAM service \"$1\" already configured."
    exit 0
fi

if test -d /etc/pam.d; then
    if test ! -w /etc/pam.d; then
	echo "Error: need write access to /etc/pam.d/ to install PAM service definition!" >&2
	exit 1
    fi
    for sv in kde login; do
	if test -r /etc/pam.d/$sv; then
	    echo "Copying PAM service definition file \"$sv\" to \"$1\"."
	    sed -e 's/^\( *[^#].*pam_securetty\)/#\1/' < /etc/pam.d/$sv > /etc/pam.d/$1
	    exit 0
	fi
    done
    echo "Error: no template PAM service file for \"$1\" found!" >&2
    exit 1
elif test -f /etc/pam.conf; then
    if test ! -w /etc/pam.conf; then
	echo "Error: need write access to /etc/pam.conf to install PAM service definition!" >&2
	exit 1
    fi
    for sv in kde login; do
	serv=`grep "^$sv[ 	]" /etc/pam.conf | grep -v pam_securetty`
	if test -n "$serv"; then
	    echo "Copying service definition entry \"$sv\" to \"$1\"."
	    echo >>/etc/pam.conf
	    echo "$serv" | sed -e "s/^$sv\\([ 	]\\)/$1\\1/" >>/etc/pam.conf
	    exit 0
	fi
    done
    echo "Error: no template PAM service entry for \"$1\" found!" >&2
    exit 1
else
    if test "`whoami`" != root; then
	echo "Error: need root priviledges to install PAM service definitions!" >&2
    else
	echo "Error: don't know where to store the PAM service definition for \"$1\"!" >&2
    fi
    exit 1
fi
