#!/bin/sh
#
# 	postinst -- Post installation script for GoAhead
#
#	Copyright (c) Embedthis Software LLC, 2003-2014. All Rights Reserved.
#

ME_PRODUCT=${settings.product}
ME_NAME="${settings.title}"
ME_BIN_PREFIX="${prefixes.bin}"
ME_LIB_PREFIX="${prefixes.lib}"
ME_LOG_PREFIX="${prefixes.log}"
ME_SPL_PREFIX="${prefixes.spool}"

###############################################################################

setPerms() {
	local g u

	for u in www-data _www nobody Administrator ; do
		grep "$u" /etc/passwd >/dev/null
		if [ $? = 0 ] ; then
			username=$u
			break
		fi
	done
	for g in www-data _www nobody nogroup Administrator ; do
		grep "$g" /etc/group >/dev/null
		if [ $? = 0 ] ; then
			groupname=$g
			break
		fi
	done
	if [ "$username" = "" -o "$groupname" = "" ] ; then
		echo "Can't find a suitable user/group for $ME_PRODUCT" 1>&2
		exit 255
	fi
    mkdir -p "$ME_SPL_PREFIX" "$ME_SPL_PREFIX/cache" "$ME_LOG_PREFIX"
    chown $username "$ME_SPL_PREFIX" "$ME_SPL_PREFIX/cache" "$ME_LOG_PREFIX"
    chgrp $groupname "$ME_SPL_PREFIX" "$ME_SPL_PREFIX/cache" "$ME_LOG_PREFIX"
    chmod 755 "$ME_SPL_PREFIX" "$ME_SPL_PREFIX/cache" "$ME_LOG_PREFIX"
}

###############################################################################
#
#	Main
#

cd /
action=$1

case $action in
	configure)							# when new
        setPerms
		oldVersion=$2
        "$ME_BIN_PREFIX/linkup" Install /
        #appman -v install
        #if [ "$APPWEB_DONT_START" != 1 ] ; then
        #    appman enable
        #    appman start
        #fi
        echo
		echo "$ME_NAME installation successful."
		;;
	abort-upgrade)						# when old
		;;
	abort-remove)
		# 	sometimes conflictor's-postinst abort-remove in-favor package new-version
		;;
esac
exit 0
