#!/bin/sh

# Shell script maintain realms for SCC.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004-2005 OpenEyeT Professional Services.
# Copyright (C) 2005-2009 QNH.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Contact information: http://scc.qnh-infrastructure.nl

# SCC-release:	1.7.5
# file-version:	$Revision: 1.51 $


ProgName=${0##*/};			export ProgName

set -u

SCC_BIN=/opt/scc-srv/bin
SCC_TOP=/var/opt/scc-srv
export SCC_BIN SCC_TOP

export SCC_DATA=${SCC_TOP}/data
export SCC_TMP=${SCC_TOP}/tmp
export SCC_CONF=${SCC_TOP}/conf
export SCC_WWW=${SCC_DATA}/www

export TMPDIR=${SCC_TMP}
export TMP=${SCC_TMP}

export SHELL=/bin/sh
export LANG=C

PATH=/sbin:/usr/sbin:/usr/bin:/bin:${SCC_BIN};		export PATH

# Use the following utilities for SunOS.
if [ -x /usr/ccs/bin/what ]
then
	alias what=/usr/ccs/bin/what
fi
if [ -x /usr/xpg4/bin/awk ]
then
	alias awk=/usr/xpg4/bin/awk
fi
if [ -x /usr/xpg4/bin/grep ]
then
	alias grep=/usr/xpg4/bin/grep
fi

SYNTAX="Syntax error, use: ${ProgName} [ -a <syslist> | -d <syslist> ] [ -c | -r ] [ -q ]  <realm>"

create=0
remove=0
add_sys=0
del_sys=0
quick=0
sys_list=""
while [ $# -gt 1 ]
do
	if [ $# -eq 1 ]
	then
		# All options, except the ones in the following list, should have an argument.
		if [ "x${1}" != "x-c" -a "x${1}" != "x-r" -a "x${1}" != "x-q" ]
		then
			echo "${ProgName}: Syntax error, missing argument for option: $1" 
			exit 2
		fi
	fi

	case "${1}" in
	-a)	sys_list="${2}";
		add_sys=1;
		shift 2;;

	-c)	create=1
		shift 1;;

	-d)	sys_list="${2}";
		del_sys=1;
		shift 2;;

	-r)	remove=1
		shift 1;;

	-q)	quick=1
		shift 1;;

	*)	echo "${SYNTAX}" >&2
		exit 1;;
	esac
done

if [ ${del_sys} -eq 0 -a ${add_sys} -eq 0 -a ${create} -eq 0 -a ${remove} -eq 0 ]
then
	echo "${SYNTAX}" >&2
	exit 1
fi

if [ ${del_sys} -eq 1 -a ${add_sys} -eq 1 ]
then
	echo "${SYNTAX}" >&2
	exit 1
fi

if [ $# -ne 1 ]
then
	echo "${SYNTAX}" >&2
	exit 1
fi
realm="${1%%/*}"		# Only one level deep!

cd ${SCC_WWW}

if [ ${remove} -eq 1 ]
then
	if [ ! -d "${realm}" ]
	then
		echo "${ProgName}: attempt to remove non-existent realm '${realm}'"
		exit 2
	else
		rm -rf "${realm}"

		# Try to remove the realm from the main index.html.
		# Use ed to preserve ownership and permissions.
		cat <<-_X_ | ed -s index.html 2>/dev/null
			/^[ 	]*<LI><A HREF="${realm}\/index.html">.*<\/A><\/LI>[ 	]*$/d
			w
			q
		_X_
		exit 0
	fi
fi

if [ ! -d "custom" ]
then
	echo "${ProgName}: example directory 'custom' does not exist"
	exit 2
fi

if [ ${create} -eq 1 ]
then
	if [ -d All ]
	then
		example=All
	else
		example=custom
	fi
else
	example="${realm}"
fi
set dummy $(ls -ld "${example}")
case "${2}" in
?r??r??r??)	new_mode=0444; umask 222;;
?r??r??-??)	new_mode=0440; umask 227;;
?r??-??-??)	new_mode=0400; umask 277;;
?r??-??r??)	new_mode=0404; umask 272;;
*)		new_mode=0400; umask 077;;
esac
new_owner_group="${4}:${5}"

if [ ${create} -eq 1 ]
then
	if [ -d "${realm}" ]
	then
		echo "${ProgName}: realm directory '${realm}' already exists"
		exit 2
	fi
	mkdir -p "${realm}"/custom
	mkdir -p "${realm}"/cgi-bin

	if [ -f All/.htaccess ]
	then
		cp -p All/.htaccess "${realm}"/.htaccess
	fi
	cp -p ${SCC_BIN}/scc-wrapper.cgi "${realm}"/cgi-bin
	ln -s ../../custom/scc-logo.png "${realm}"/custom/scc-logo.png
	ln -s ../../custom/scc-realm.conf "${realm}"/custom/scc-realm.conf
	if [ -f custom/style.css ]
	then
		ln -s ../../custom/style.css "${realm}"/custom/style.css
	fi
	if [ -f custom/scc-auto-realm.conf ]
	then
		cp custom/scc-auto-realm.conf "${realm}"/custom/
	fi
	chown -R "${new_owner_group}" "${realm}"

	awk '/<!-- SCC_REALM: Keep this marker to indicate scc-realm where to add new realms -->/	{
			if ( r != "All" )	# Already present in index.html
			{
				printf( "<LI><A HREF=\"%s/index.html\">%s index</A></LI>\n", r, r );
			}
		}
		{
			print;
		}' r="${realm}" index.html >new_index.html

	# The main-index can have other ownership than the realm-files.
	set dummy $(ls -ld index.html)
	case "${2}" in
	?r??r??r??)	index_mode=0444;;
	?r??r??-??)	index_mode=0440;;
	?r??-??-??)	index_mode=0400;;
	?r??-??r??)	index_mode=0404;;
	*)		index_mode=0400;;
	esac
	index_owner_group="${4}:${5}"
	mv -f new_index.html index.html
	chmod ${index_mode} index.html
	chown "${index_owner_group}" index.html
fi

if [ "${sys_list}" ]
then
	if [ ! -d "${realm}" ]
	then
		echo "${ProgName}: realm '${realm}' does not exist, use the -c option" >&2
		exit 2
	fi

	cd "${realm}"
	for sys in $(echo ${sys_list} | tr -s "," " " )
	do
		if [ ${add_sys} -eq 1 ]
		then
			for suffix in cur html log log.html
			do
				if [ ! -f ../All/scc.${sys}.${suffix} ]
				then
					echo "${ProgName}: file All/scc.${sys}.${suffix} not found" >&2
				else
					ln -s ../All/scc.${sys}.${suffix} .
					chown "${new_owner_group}" ../All/scc.${sys}.${suffix}
				fi
			done
		else
			for suffix in cur html log log.html smt.html
			do
				rm -f scc.${sys}.${suffix} 2>/dev/null
			done
			# Cleanup the possibly saved snapshots and logfiles.
			rm -f scc.${sys}.cur.* scc.${sys}.log.*
		fi
	done

	if [ ${quick} -eq 0 ]
	then
		${SCC_BIN}/scc-update "${realm}"
	fi
fi

exit 0
