#! /bin/csh
#
# @(#)upgrade_cm	1.5 6/19/90
#
# Upgrade beta version/SVOL of cms .callog to version 2 callog database which
# is resided at $CAL_DIR.  This upgrade script must be run in super user mode.
# This script takes an optional list of user-names.  If user-name is not
# specified, the login name ($LOGNAME) will be the default user.
# 
# Usage: upgrade_cm [user-name ...]
#

set CAL_DIR=/usr/spool/calendar

set ME=`whoami`
if ( $ME != "root" ) then
	echo "Must be run in super-user mode."
	echo "Usage: upgrade_cm [user-name ...]"
	exit 1
endif

if ($#argv == 0) then
	set argv="$argv[0] $LOGNAME"
	/bin/echo -n "Upgrade cm for $argv[*].  Press RETURN to continue..."
	$<
endif

set path=(/bin /etc /usr/bin /usr/etc /usr/bin)
if (! -d $CAL_DIR ) then
	echo "Creating $CAL_DIR..."
	mkdir $CAL_DIR
	chown daemon.daemon $CAL_DIR
	chmod 1777 $CAL_DIR
	chmod g+s $CAL_DIR
endif

foreach user ($argv[*])
	set OLD_CALLOG=~$user/.callog
	set NEW_CALLOG=$CAL_DIR/callog.$user
	if (-f $NEW_CALLOG ) then
		chmod g+rw $NEW_CALLOG
		chgrp daemon $NEW_CALLOG
		echo ""
		echo "User $user has been upgraded.  The request is ignored."
	else
		if ( -f $OLD_CALLOG ) then
			echo ""
			echo "Copying $OLD_CALLOG to $NEW_CALLOG..."
			cp $OLD_CALLOG $NEW_CALLOG
			chown $user.daemon $NEW_CALLOG
			chmod 460 $NEW_CALLOG
			mv $OLD_CALLOG $OLD_CALLOG-
			if ( $status == 0 ) then
				echo "The $OLD_CALLOG is renamed to .callog-"
			else
				echo "Please rename $OLD_CALLOG to .callog- manually."
			endif
		endif
	endif
end

echo ""
echo "Upgrade process is done\!"
