#! /bin/sh
# bourne shell script
#
# This is the original RTM version that this patch applies to
VERSION="1.0,REV=2003.10.29"

#
# debug message
#
debug_log() {
  if [ $DEBUG != 0 ]; then
    log_msg $*
  fi
}

#
# initialize LOGFILE
#
log_init() {
  /usr/bin/touch $LOGFILE
  /usr/bin/echo "============ LOGFILE started `date` ==========" >> $LOGFILE
}

#
# log a message to the screen and to the LOGFILE
#
log_msg() {
  /usr/bin/echo $*
  /usr/bin/echo $* >> $LOGFILE
}

#
# log a message to the LOGFILE only
#
log_only() {
  /usr/bin/echo $* >> $LOGFILE
}

#
# determine basedir
# - sets the variable basedir
#
get_basedir() {   # start of get_basedir
  PKGS=`/bin/ls -d $PKGDB/SUNWcomic*`
  # did we find the right PKG
  found=0
  for dir in $PKGS
  do
    pkgname=`basename $dir`
    basedir=`pkgparam -R $ROOTDIR $pkgname BASEDIR`
    version=`pkgparam -R $ROOTDIR $pkgname VERSION`
    if [ $version = "$VERSION" ]; then
      found=1
      break
    fi
  done
  if [ $ROOTDIR != "/" ]; then
    basedir=${ROOTDIR}$basedir
  fi
  # what if we do not find the pkg? should not possible...
  if [ $found -eq 0 ]; then
    /usr/bin/echo "Error in postpatch, did not find a SUNWcomic that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# create the patch staging area
#
create_staging_area() {
  log_only "-- creating patch directory $PATCHDIR"
  /usr/bin/mkdir -p $PATCHDIR
}

#
# was configure run previously?
# - sets the variable isConfigured to either 0 or 1
# - requires basedir
get_isConfigured() {
  if [ -r $basedir/config ]; then
    isConfigured=1
  else
    isConfigured=0
  fi
  log_only "-- isConfigured = $isConfigured"
}

#
# read saveState file for configuration variables
# What to do if we do not find the file?
# - given basedir
# - get iMS.UserId, iMS.GroupId, INSTALLER_TYPE, SCHEMA_TYPE, comp list
# - set the variables
#   saveState         shell variable
#   ---------         --------------
#   msg.IsPatch        -> ampath
#   IsAmadmin.User     -> amadmin
#   IsAmadmin.Password -> amadmin_passwd
#   com.iplanet.install.panels.common.ComponentPanel.selectedcomponents
#                      -> compList
read_saveState() {
  # determine saveState file
  # have to use the latest install/Iscli-config_YYYYMM... directory
  CONFIGDIRS=`/bin/ls -dt $basedir/install/Iscli-config_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]`
  for dir in $CONFIGDIRS
  do
    saveState=$dir/saveState
    if [ -f $saveState ]; then
      break
    fi
  done
  log_only "-- saveState file: $saveState"
  set `/usr/bin/grep msg.IsPath $saveState`
  ampath=$3
  log_only "-- IS path: $ampath"
  set `/usr/bin/grep IsAmadmin.User $saveState`
  amadmin=$3
  log_only "-- amadmin user: $amadmin"
  set `/usr/bin/grep IsAmadmin.Password $saveState`
  amadmin_passwd=$3
  log_only "-- amadmin password: xxxxxx"
  set `/usr/bin/grep com.iplanet.install.panels.common.ComponentPanel.selectedcomponents $saveState`
  compList=$3
  log_only "-- compList: $compList"
}

#
# patch_ldif - what to do about ldap changes
#
patch_ldif() {
  log_only "-- patch_ldif being run"
  log_only "/usr/bin/cat > $PATCHDIR/commcli.mod.ldif"
  /usr/bin/cat > $PATCHDIR/commcli.mod.ldif 2>> $LOGFILE << MODS
dn: cn=option_uS, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_uH;cn=mail_option_uE]
commsecOption: cal=[cn=cal_option_uE;cn=cal_option_uB;cn=cal_option_uJ;cn=cal_option_uT]
commdescription: service(s) to be added 
commservletArg: add_services

dn: cn=cal_option_c, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=cal_option_uE, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_uE
commtakesArg: true
commrequired: true
commecho: true
commdescription: user's email address
commservletArg: add_mail

dn: cn=cal_option_uB, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=cal_option_uB, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_uB
commtakesArg: true
commrequired: false
commecho: true
commdescription: user's back end calendar server
commservletArg: add_icsdwphost

dn: cn=cal_option_uJ, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=cal_option_uJ, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_uJ
commtakesArg: true
commrequired: false
commecho: true
commdescription: first day of the week
commservletArg: add_icsfirstday

dn: cn=cal_option_uB, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=cal_option_uB, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_uB
commtakesArg: true
commrequired: false
commecho: true
commdescription: user's back end calendar server
commservletArg: add_icsdwphost

dn: cn=cal_option_uJ, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=cal_option_uJ, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_uJ
commtakesArg: true
commrequired: false
commecho: true
commdescription: first day of the week
commservletArg: add_icsfirstday

dn: cn=option_uM, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=mail_option_uM, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: mail_option_uM
commtakesArg: true
commrequired: false
commecho: true
commdescription: external member of group (repeatable), mail address(es)
commservletArg: add_mgrprfc822mailmember

dn: cn=option_uS, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_o;cn=mail_option_uH;cn=mail_option_r;cn=mail_option_uE;cn=mail_option_uM]
commdescription: service(s) to be added 
commservletArg: add_services

dn: cn=option_uM, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=mail_option_uM, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: mail_option_uM
commtakesArg: true
commrequired: false
commecho: true
commdescription: [action]external-member, action is +,-
commservletArg: CLIModifyArg

dn: cn=option_uS, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_o;cn=mail_option_uH;cn=mail_option_r;cn=mail_option_uE;cn=mail_option_uM]
commdescription: service(s) to be added 
commservletArg: add_services

dn: cn=option_o, cn=servlet1, cn=create, cn=resource, o=commcli,o=comms-config
changetype: delete

dn: cn=option_o, cn=servlet1, cn=create, cn=resource, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_o
commtakesArg: true
commrequired: true
commecho: true
commdescription: owner of the resource's calendar(user id)
commservletArg: owner

MODS
  log_only "/usr/bin/cat > $PATCHDIR/commcli.revert.ldif"
  /usr/bin/cat > $PATCHDIR/commcli.revert.ldif 2>> $LOGFILE << REVERT
## modify this option
##
dn: cn=option_uS, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_uH;cn=mail_option_uE]
commsecOption: cal=[cn=cal_option_c;cn=cal_option_uB;cn=cal_option_uJ;cn=cal_option_uT]
commdescription: service(s) to be added 
commservletArg: add_services

## add this option back
##
dn: cn=cal_option_c, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: cal_option_c
commtakesArg: true
commrequired: false
commecho: true
commdescription: user's default calendar
commservletArg: add_icsCalendar

## remove this option.  It only works with patch
##
dn: cn=cal_option_uE, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
changetype: delete

## The cal_option_uB and cal_option_uJ were wrong in the original
## It makes no sense to revert them back to an error state.
## dn: cn=cal_option_uB, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
## dn: cn=cal_option_uJ, cn=servlet1, cn=create, cn=user, o=commcli,o=comms-config
## dn: cn=cal_option_uB, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
## dn: cn=cal_option_uJ, cn=servlet1, cn=modify, cn=user, o=commcli,o=comms-config
##

## This option was renamed, delete patch replace with rtm.
##
dn: cn=mail_option_uM, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uM, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uM
commtakesArg: true
commrequired: false
commecho: true
commdescription: external member of group (repeatable), mail address(es)
commservletArg: add_mgrprfc822mailmember

## modify this option.  Replace with rtm version
##
dn: cn=option_uS, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=create, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_o;cn=mail_option_uH;cn=mail_option_r;cn=mail_option_uE]
commdescription: service(s) to be added 
commservletArg: add_services

## Revert to rtm dn for this option.
##
dn: cn=mail_option_uM, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uM, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uM
commtakesArg: true
commrequired: false
commecho: true
commdescription: [action]external-member, action=+,-
commservletArg: CLIModifyArg

# revert to rtm values.
#
dn: cn=option_uS, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: delete

dn: cn=option_uS, cn=servlet1, cn=modify, cn=group, o=commcli,o=comms-config
changetype: add
objectclass: top
objectclass: extensibleObject
cn: option_uS
commtakesArg: true
commrequired: false
commecho: true
commsecOption: mail=[cn=mail_option_o;cn=mail_option_uH;cn=mail_option_r;cn=mail_option_uE]
commdescription: service(s) to be added 
commservletArg: add_services

## only the usage message changed, so don't revert.
##
## dn: cn=option_o, cn=servlet1, cn=create, cn=resource, o=commcli,o=comms-config
REVERT
}

#
# update_service - update IS services
#
update_service() {
  log_only "-- update_service being run"
  if [ -f $basedir/lib/services/addcaluserAttributesRequest.xml ]; then
    # have to cd because addcaluserAttributesRequest.xml refers to a
    # file in that directory without a full path
    cd $basedir/lib/services
    log_only "$ampath/bin/amadmin -u $amadmin -w xxxxxx -t $basedir/lib/services/addcaluserAttributesRequest.xml"
    $ampath/bin/amadmin -u $amadmin -w $amadmin_passwd -t $basedir/lib/services/addcaluserAttributesRequest.xml >> $LOGFILE 2>&1
    stat=$?
    log_only "stat=$stat"
    if [ $stat -ne 0 ]; then
      log_msg "--"
      log_msg "-- amadmin -t addcaluserAttributesRequest.xml returned status=$stat"
      log_msg "-- Please see the logfile for details: $LOGFILE"
    fi
  fi
}

#
# main program starts here
#
# Do I need to do stop-msg?
# determine server-root
# create patch staging area (for backout and new files to apply)
# determine ifConfigured
# if configured
#   - read_saveState
#   - patch_ldif
#   - update_service
# endif

get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
/usr/bin/mkdir -p $PATCHDIR
LOGFILE=$PATCHDIR/postpatch_`date +%Y%m%d%H%M%S`.log
log_init
create_staging_area
get_isConfigured
if [ $isConfigured -eq 1 ]; then
  read_saveState
  patch_ldif
  update_service
fi
