#!/bin/ksh93

SDIR=$( cd ${ dirname $0; }; printf "$PWD" )
MAC_PREFIX=0:0:2
ZNMASK='/24'

. $SDIR/etc/zinstall.kshlib || exit 1
IPFCONF=$SDIR/files/ipf.conf-www

ZUSER[webadm]=( login=webadm uid=1114 gid=10 gcos='Webmaster'
	profile='auths=solaris.smf.manage.http/*,solaris.smf.value.http/*'
)

# virtuellen zpool pool1 für die zone erstellen
DATADIR[pool1]=(
	pool=${GZPOOL}/${ZNAME}/pool1 z_pool=pool1 zopts='compression=on atime=off'
)
# benötigte ZFSs vom virtuellen zpool
DATADIR[data]=(
	z_pool=pool1/data z_dir=/data zopts='setuid=off'
)
DATADIR[sites]=(
	z_pool=pool1/data/web/sites
	mode=0755 uid=${ZUSER[webadm].uid} gid=${ZUSER[webadm].gid}
)
DATADIR[log]=(
	z_pool=pool1/data/web/log z_dir=/var/log/httpd mode=0755 uid=80 gid=80
)

checkNIC
checkIP
checkDatadirsPre

checkZonePathes
createTempdir
prepareManifest

addPkg install //lnf/primary-admin
addPkg install /web/server/apache-22
# Apache docs muß nicht unbedingt bei jedem - erfordert facet.doc.html=true,
# was auch den Müll für andere packages installiert (ca. 3500 files, 22 MB)
gsed -i -e '/facet.doc.html/ s,false,true,' ${MANIFEST}
addPkg install /web/server/apache-22/documentation

prepareProfiles

# NFS verfügbar machen
PROFILE=${PROFILE_DIR}/nfs_profile.xml
startBundle >${PROFILE}
	print '
	<service version="1" type="service" name="network/nfs/status">
		<instance enabled="true" name="default"/>
	</service>
	<service version="1" type="service" name="network/nfs/nlockmgr">
		<instance enabled="true" name="default"/>
	</service>
	<service version="1" type="service" name="network/nfs/client">
		<instance enabled="true" name="default"/>
	</service>
	<service version="1" type="service" name="system/filesystem/autofs">
		<instance enabled="true" name="default"/>
	</service>
'	>>${PROFILE}
endBundle >>${PROFILE}

createZoneConfig
installZone

bootZone
checkDatadirsPost
customizeRunningZone

# webadm erlauben, sich per ssh einzuloggen
gsed -i -e '/^AllowUsers/ s,$, webadm,' ${ZPATH}/root/etc/ssh/sshd_config
for U in johndoe janedoe ; do
	[[ -r ${SDIR}/files/ssh_keys/authorized_keys2.$U ]] && \
		cat ${SDIR}/files/ssh_keys/authorized_keys2.$U \
			>>${ZPATH}/root/${ZUSER[webadm].home}/.ssh/authorized_keys2
done

# Web dir sharen (wenn's bereits geshared ist, kommt 'nen Fehler, den wir
# ignorieren - vertrauen, daß die derzeitigen Einstellungen ok sind)
zlogin $ZNAME zfs share -o share.desc=\"$ZNAME WebSpace\" \
    -o share.nfs.sec=sys \
    -o share.nfs.rw=@192.168.1.0/30:@192.168.1.64/26: \
	-o share.nfs.root=@192.168.1.0/30 \
	-o share.nfs=on \
    ${DATADIR[sites].z_pool}%${DATADIR[sites].z_pool##*/}

# service http konfigurieren
WEBSITE=${ZNAME}.${ZDOMAIN}
CFDIR="${ZPATH}/root//data/web/sites/conf"

cp ${SDIR}/files/{http-apache22,httpd-setup.ksh} \
	${ZPATH}/root/local/home/admin/etc/

[[ -e ${CFDIR}/httpd.conf ]] && PARAM='' || PARAM='-o'
zlogin $ZNAME /local/home/admin/etc/httpd-setup.ksh -n ${WEBSITE} \
	-s -u ${PARAM} -c -O webadm -G staff -p 80 -P 443

# wenn die *.conf neu erstellt wurden
if [[ -n ${PARAM} ]]; then
	# status/env pages erlauben
	gsed -i -e 's,10.0.0.0/[0-9]*,10.0.0.0/24 192.168.1.0/30 192.168.2.0/24,' \
		${CFDIR}/httpd.conf ${CFDIR}/extra/info.conf \
		${CFDIR}/sites/${WEBSITE} ${CFDIR}/sites/template.conf
	# Aliase fixen
	gsed -i -e "/^ServerAdmin/ s,root.*,webmaster+${ZHNAME}@${ZDOMAIN}," \
		${CFDIR}/httpd.conf
fi
zlogin $ZNAME svcadm enable apache22

rebootZone

# vim: ts=4 sw=4 filetype=sh
