#!/bin/ksh
#
#ident "@(#)calcreate	1.2 99/04/21   SMI" 
#
# Script to create a calendar.
# If run by root user, -u option may be used to set the owner of
# the calendar to a particular user.
#

thiscmd=`basename $0`
USAGE="Usage: "$thiscmd" [-u owner] calendarname"

SDTCMADMIN=/usr/dt/bin/sdtcm_admin

CALCHOWN=/opt/SUNWical/bin/calchown

while getopts ":u:" opt ; do
    case $opt in
	u)	
		owner=$OPTARG;;

	\?)	
		print -u2 $0':' bad option
		print -u2 $USAGE 
		exit 1;;
    esac
done

shift $(($OPTIND - 1))

newcal=$1

if [[ -z $newcal ]]; then
  print -u2 $USAGE
  exit 1
fi

$SDTCMADMIN -a -c $newcal

status=$?
if [[ $status != 0 ]]; then
  # sdtcm_admin failed. We're silent since it's error message should point
  # to the problem
  exit $status
fi

if [[ -n $owner ]]; then

  $CALCHOWN $owner $newcal
  status=$?

  if [[ $status != 0 ]]; then

    # chown failed (Probably due to permission problems)
    # delete the calendar that we just created.
    # Since we use the -f option the user won't be prompted.

    print -u2 $thiscmd':' failed to set owner of $newcal to $owner, $newcal not created.

    $SDTCMADMIN -d -f -c $newcal
  fi
fi

exit $status

