#!/bin/sh
#
# $OpenBSD: INSTALL,v 1.2 2004/06/24 23:58:34 marcm Exp $

P_NAME=gdm

DIRS='
gdm/Init
gdm/PostLogin
gdm/PostSession
gdm/PreSession
gdm/modules
'

FILES='
gdm/gdm.conf
gdm/locale.alias
gdm/modules/AccessDwellMouseEvents
gdm/modules/AccessKeyMouseEvents
'

SCRIPTS='
gdm/Init/Default
gdm/PostLogin/Default.sample
gdm/PostSession/Default
gdm/PreSession/Default
gdm/XKeepsCrashing
gdm/Xsession
'

set -e
PREFIX=${PKG_PREFIX:-/usr/local}
PATH=/bin:/usr/bin:/sbin:/usr/sbin

do_create_gdm_dirs() {
	if [ ! -d "/var/db/gdm" ]; then
		echo "Creating gdm auth dir: /var/db/gdm"
		install -d -g _x11 -m 1770 /var/db/gdm
	fi
	if [ ! -d "/var/log/gdm" ]; then
		echo "Creating gdm log dir: /var/log/gdm"
		install -d -m 0755 /var/log/gdm
	fi
}

do_post() {

	DEST_PFX=/etc/X11
	SOURCE_PFX=/usr/local/share/examples/gdm

	echo 
	echo "+--------------- ${P_NAME}"

	# install dirs if necessary
	for d in ${DIRS}; do
		if [ ! -d "${DEST_PFX}/$d" ]; then
			install -d ${DEST_PFX}/$d
		fi
	done

	# install or take note of existing config files
	for f in ${FILES}; do
		if [ -f "${DEST_PFX}/$f" ]; then
			OLD_CONFS="${OLD_CONFS} $f"
		else
			if ! install -m 644 ${SOURCE_PFX}/$f ${DEST_PFX}/$f; then
				echo "| ERROR: The following file could not be installed, exiting: ${DEST_PFX}/$f"
				exit 1
			fi
			NEW_CONFS="${NEW_CONFS} $f"
		fi
	done

	# install or take note of existing config scripts
	for f in ${SCRIPTS}; do
		if [ -f "${DEST_PFX}/$f" ]; then
			OLD_CONFS="${OLD_CONFS} $f"
		else
			if ! install -m 755 ${SOURCE_PFX}/$f ${DEST_PFX}/$f; then
				echo "| ERROR: The following file could not be installed, exiting: ${DEST_PFX}/$f"
				exit 1
			fi
			NEW_CONFS="${NEW_CONFS} $f"
		fi
	done

	# print status report
	if [ -n "${NEW_CONFS}" ]; then
		echo "| The following NEW configuration files have been installed:"
		echo "|"
		for f in ${NEW_CONFS}; do
			echo "| ${DEST_PFX}/$f"
		done 
	fi
	
	if [ -n "${OLD_CONFS}" ]; then
		if [ -n "${NEW_CONFS}" ]; then
			echo "|"
		fi
		echo "| The following OLD configuration files were found and have NOT been"
		echo "| overwritten:"
		echo "|"
		for f in ${OLD_CONFS}; do
			echo "| ${DEST_PFX}/$f"
		done
		echo "|"
		echo "| You should however manually compare them to their equivalents in"
		echo "|"
		echo "| ${SOURCE_PFX}/${P_NAME}"
		echo "|"
		echo "| and update your configuration as needed." 
	fi
	echo "+--------------- ${P_NAME}"
	echo 
}

# verify proper execution
#
if [ $# -ne 2 ]; then
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
fi

# Verify/process the command
#
case $2 in
	PRE-INSTALL)
		;;
	POST-INSTALL)
		do_create_gdm_dirs
		do_post
		;;
	*)
		echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
		exit 1
		;;
esac

exit 0
