#!/bin/sh

name=musicipserver
examplesdir=/usr/local/share/examples/musicipserver
datadir=/usr/local/share/musicipserver
logdir=/var/log/musicipserver
rundir=/var/run/musicipserver
statedir=/var/db/musicipserver

serverlogfile=${logdir}/server.log
mipcorelogfile=${logdir}/mipcore.log
pidfile=${rundir}/${name}.pid
serverflogpidfile=${rundir}/${name}-server-flog.pid
mipcoreflogpidfile=${rundir}/${name}-mipcore-flog.pid
mipcoretailpidfile=${rundir}/${name}-mipcore-tail.pid
serverinfifo=${rundir}/${name}-fifo-in
serveroutfifo=${rundir}/${name}-fifo-out
mipcorelogfifo=${rundir}/${name}-mipcore-fifo

u=mipserv
g=mipserv
ugid=10002
homedir=/nonexistent
shell=/sbin/nologin
comment="MusicIP pseudo-user"

newsyslogfile=/etc/newsyslog.conf
logcomment="# added by audio/${name} port"
serverlogline="${serverlogfile}	${u}:${g} 644 3	   100	*     J     ${serverflogpidfile}"
mipcorelogline="${mipcorelogfile}	${u}:${g} 644 3	   100	*     J     ${mipcoreflogpidfile}"

case $2 in
PRE-INSTALL)
	if pw group show "${g}" >/dev/null 2>&1; then
		echo "Using existing group \"${g}\"."
	else
		echo "Creating group \"${g}\", (gid: ${ugid})."
		pw groupadd ${g} -g ${ugid}
		if [ $? != 0 ]; then
			echo "Failed to add group \"${g}\"."
			exit 1
		fi
	fi
	if pw user show "${u}" >/dev/null 2>&1; then
		echo "Using existing user \"${u}\"."
	else
		echo "Creating user \"${u}\", (uid: ${ugid})."
		pw useradd ${u} -u ${ugid} -g ${ugid} -h - \
		    -d ${homedir} -s ${shell} -c "${comment}"
		if [ $? != 0 ]; then
			echo "Failed to add user \"${u}\"."
			exit 1
		fi
	fi
	;;
POST-INSTALL)
	if egrep -q "^${serverlogfile}\>" ${newsyslogfile}; then
		echo "Using existing ${serverlogfile} entry in ${newsyslogfile}."
	else
		echo "Adding ${serverlogfile} entry to ${newsyslogfile}."
		echo "$logcomment" >> ${newsyslogfile}
		echo "$serverlogline" >> ${newsyslogfile}
	fi

	if egrep -q "^${mipcorelogfile}\>" ${newsyslogfile}; then
		echo "Using existing ${mipcorelogfile} entry in ${newsyslogfile}."
	else
		echo "Adding ${mipcorelogfile} entry to ${newsyslogfile}."
		echo "$logcomment" >> ${newsyslogfile}
		echo "$mipcorelogline" >> ${newsyslogfile}
	fi

	if [ ! -d ${logdir} ]; then
		mkdir ${logdir}
		chown ${u}:${g} ${logdir}
		chmod 755 ${logdir}	
	fi
	if [ ! -d ${rundir} ]; then
		mkdir ${rundir}
		chown ${u}:${g} ${rundir}
		chmod 755 ${rundir}	
	fi
	if [ ! -d ${statedir} ]; then
		mkdir ${statedir}
		chown ${u}:${g} ${statedir}
		chmod 755 ${statedir}	
	fi

	if [ ! -f ${serverlogfile} ]; then
		touch ${serverlogfile}
		chown ${u}:${g} ${serverlogfile}
		chmod 644 ${serverlogfile}
	fi

	if [ ! -f ${mipcorelogfile} ]; then
		touch ${mipcorelogfile}
		chown ${u}:${g} ${mipcorelogfile}
		chmod 644 ${mipcorelogfile}
	fi

	if [ ! -p ${statedir}/log.txt ]; then
		mkfifo ${statedir}/log.txt
		chown ${u}:${g} ${statedir}/log.txt
		chmod 644 ${statedir}/log.txt
	fi

	if [ ! -e ${statedir}/mmm.ini ]; then
		cp ${examplesdir}/mmm.ini.sample ${statedir}/mmm.ini
		chown ${u}:${g} ${statedir}/mmm.ini
		chmod 644 ${statedir}/mmm.ini
	fi
	
	if [ ! -e ${statedir}/recipes.xml ]; then
		cp ${examplesdir}/recipes.xml.sample ${statedir}/recipes.xml
		chown ${u}:${g} ${statedir}/recipes.xml
		chmod 644 ${statedir}/recipes.xml
	fi
	
	;;
esac
