#! /bin/sh

PATH=/bin:/usr/sbin:/usr/bin:/usr/sbin

EXAMPLESDIR="/usr/local/share/examples/ja-samba"
SAMBA_CONFIG="smb.conf"
SAMBA_CONFDIR="/usr/local/etc"
SAMBA_SPOOL="/var/spool/samba"
SAMBA_LOGDIR="/var/log/samba"
SAMBA_LOCKDIR="/var/db/samba"
SAMBA_PRIVATEDIR="/usr/local/etc/samba"
SAMBA_OWNER=root
SAMBA_GROUP=wheel
INSTALL_DATA="install -c -m 0444"
MKDIR="mkdir -p"
CHMOD="chmod"
CHOWN="chown"

post-install() {
	${MKDIR} ${SAMBA_SPOOL} && ${CHMOD} 1777 ${SAMBA_SPOOL}
	${MKDIR} ${SAMBA_LOGDIR} && ${CHMOD} 0755 ${SAMBA_LOGDIR}
	${MKDIR} ${SAMBA_LOCKDIR} && ${CHMOD} 0755 ${SAMBA_LOCKDIR}
	${MKDIR} ${SAMBA_PRIVATEDIR} && ${CHMOD} 0700 ${SAMBA_PRIVATEDIR}
	if [ $(id -u) -eq 0 ]; then
		INSTALL_DATA="${INSTALL_DATA} -o ${SAMBA_OWNER} -g ${SAMBA_GROUP}"
		${CHOWN} ${SAMBA_OWNER}:${SAMBA_GROUP} ${SAMBA_SPOOL}
		${CHOWN} ${SAMBA_OWNER}:${SAMBA_GROUP} ${SAMBA_LOGDIR}
		${CHOWN} ${SAMBA_OWNER}:${SAMBA_GROUP} ${SAMBA_LOCKDIR}
		${CHOWN} ${SAMBA_OWNER}:${SAMBA_GROUP} ${SAMBA_PRIVATEDIR}
	fi
	${INSTALL_DATA} ${EXAMPLESDIR}/${SAMBA_CONFIG}.sample ${SAMBA_CONFDIR}
	if [ ! -f ${SAMBA_CONFDIR}/${SAMBA_CONFIG} ]; then
	    ${INSTALL_DATA} ${EXAMPLESDIR}/${SAMBA_CONFIG}.sample ${SAMBA_CONFDIR}/${SAMBA_CONFIG}
	fi
}

case $2 in
	POST-INSTALL)
		post-install
		;;
esac
