#!/bin/sh
##############################################################################
#
#	Usage: 	postinstall
#
#	Copyright (C)2004, Sun Microsystems Inc.
#	All rights reserved.
#
#       pragma ident	"$SunId$ SMI"
##############################################################################

#
# control logging method
#

#log="logger -pdaemon.err"
log=echo

#
# we need to run this script as root
#
userid=`id | cut -f2 -d'(' | cut -f1 -d')'`

if [ ! "$userid" = "root" ]; then
	$log "postinstall: this program needs to run as root"
	exit 1
fi

if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

## Register CCR with sysidconfig
## Is this not an update of the same arch & version?
# it will be run  when create flasharchive
if [ ! "$UPDATE" = "yes" ]; then
    ## not an update of the same arch & version 
    if [ "${PKG_INSTALL_ROOT}" = "" ]; then
      # installing into current running system
      sysidconfig -b "/" -a "/usr/lib/cc-ccr/bin/eraseCCRRepository"
      ## must be done in 'root' pkg because 'user' pkg can't touch 'system' settings
    else
      # installing into alternative root
      REBOOT_DIR=${PKG_INSTALL_ROOT}/etc/rc3.d
      REBOOT_SCRIPT=${REBOOT_DIR}/S99sunwccccrr_b

      if [ ! -d ${REBOOT_DIR} ]; then
	# ???? it is possible REBOOT_SCRIPT/etc/rc3.d does not exist
	mkdir -p ${REBOOT_DIR}
      fi

      cat > $REBOOT_SCRIPT <<-EOF
	#!/bin/sh
	# This script is for SUWNccccrr.
	# when it is executed, root will be /, not ${PKG_INSTALL_ROOT}
	sysidconfig -b "/" -a "/usr/lib/cc-ccr/bin/eraseCCRRepository"
	rm \$0
	EOF

      chmod +x $REBOOT_SCRIPT
    fi
fi

# In the pre-release, property cns.httpproxy.auth was nonprivileged.
# Move it from nonprivileged to privileged.
# By the time we get here, the meta file has been updated
# so that the property is marked privileged,
# but the value is still in the nonpriv file.
# By swapping the values of the -y and -z args in the CLI, we can
# get access to the old value.

ccrcmdroot=/usr/lib/cc-ccr/bin/ccr
ccrcmd=${PKG_INSTALL_ROOT}${ccrcmdroot}

installdirroot=/var/cc-ccr
installdir=${PKG_INSTALL_ROOT}${installdirroot}

meta=${installdir}/ccr.properties
priv=${installdir}/privileged.properties
nonpriv=${installdir}/nonprivileged.properties

metaroot=${installdirroot}/ccr.properties
privroot=${installdirroot}/privileged.properties
nonprivroot=${installdirroot}/nonprivileged.properties

prop=cns.httpproxy.auth

# Do the move only if the property is marked secret.
if /usr/bin/egrep -s -e '^'${prop}'.secret *= *true$' ${meta}; then

  # The ccr cmd is installed with the SUNWccccr package.
  # It may or may not be installed by now.
  # Do nothing if the ccr exe is not installed yet (a fresh install).
  # We are not installing over the pre-release files.
  if [ -x ${ccrcmd} ]; then
    if [ "${PKG_INSTALL_ROOT}" = "" ]; then
      existsinpriv=`$ccrcmd -x ${meta} -y ${priv} -z /dev/null -c ${prop}`
      existsinnonpriv=`$ccrcmd -x ${meta} -y ${nonpriv} -z /dev/null -c ${prop}`
      if [ "${existsinnonpriv}" = "Y" ]; then
        # get value from nonpriv file
        valueinnonpriv=`$ccrcmd -x ${meta} -y ${nonpriv} -z /dev/null -g ${prop}`
        # remove auth from nonpriv file
        $ccrcmd -x ${meta} -y ${nonpriv} -z /dev/null -r ${prop}
        if [ "${existsinpriv}" = "N" ]; then
          # store value into priv file
          $ccrcmd -x ${meta} -y ${priv} -z /dev/null -p ${prop} -v "${valueinnonpriv}"
        fi
      fi
    else
      REBOOT_DIR=${PKG_INSTALL_ROOT}/etc/rc3.d
      REBOOT_SCRIPT=${REBOOT_DIR}/S99sunwccccrr_a

      if [ ! -d ${REBOOT_DIR} ]; then
        # ???? it is possible REBOOT_SCRIPT/etc/rc3.d does not exist
        mkdir -p ${REBOOT_DIR}
      fi

	cat >> $REBOOT_SCRIPT <<-EOF
	#!/bin/sh
	# This script is for SUWNccccrr. It corrects the legacy data for CCR application
	existsinpriv=\`$ccrcmdroot -x ${metaroot} -y ${privroot} -z /dev/null -c ${prop}\`
	existsinnonpriv=\`$ccrcmdroot -x ${metaroot} -y ${nonprivroot} -z /dev/null -c ${prop}\`
	if [ "\${existsinnonpriv}" = "Y" ]; then
		valueinnonpriv=\`$ccrcmdroot -x ${metaroot} -y ${nonprivroot} -z /dev/null -g ${prop}\`
		$ccrcmdroot -x ${metaroot} -y ${nonprivroot} -z /dev/null -r ${prop}
		if [ "\${existsinpriv}" = "N" ]; then
			$ccrcmdroot -x ${metaroot} -y ${privroot} -z /dev/null -p ${prop} -v \${valueinnonpriv}
		fi
	fi
	rm \$0
	EOF

      chmod +x $REBOOT_SCRIPT
    fi
  else
    # This might happen if the SUNWccccr package was explicitly removed.
    if /usr/bin/egrep -s -e '^'${prop}' *=' ${nonpriv}; then
      $log "postinstall: Cannot update values in property files."
      $log "             Must install package SUNWccccr, then re-install this package."
      exit 1
    fi
  fi
fi
   
exit 0
