#!/bin/bash
#
#	postinstall -- Post install script for binary install
#
#	Copyright (c) Embedthis Software LLC, 2003-2014. All Rights Reserved.
#
###############################################################################

PKG_DIR="$1"
INS_DIR="$2"

COMPANY="${settings.company}"
PRODUCT="${settings.product}"
NAME="${settings.title}"
LIB_PREFIX="${prefixes.lib}"
LOG_PREFIX="${prefixes.log}"
SPL_PREFIX="${prefixes.spool}"
BIN_DIR="${prefixes.bin}"

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

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 $NAME" 1>&2
		exit 255
	fi
    mkdir -p "$SPL_PREFIX" "$SPL_PREFIX/cache" "$LOG_PREFIX"
    chown $username "$SPL_PREFIX" "$SPL_PREFIX/cache" "$LOG_PREFIX"
    chgrp $groupname "$SPL_PREFIX" "$SPL_PREFIX/cache" "$LOG_PREFIX"
    chmod 755 "$SPL_PREFIX" "$SPL_PREFIX/cache" "$LOG_PREFIX"

    chmod 644 /Library/LaunchDaemons/com.${COMPANY}.${PRODUCT}.plist
}

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

#
#   Link current versions to /usr/local/bin
#
${BIN_DIR}/linkup Install

setPerms

#
#   Install, enable and start launch daemon
#
# ${BIN_DIR}/appman install
# ${BIN_DIR}/appman enable
# ${BIN_DIR}/appman start

exit 0
