#!/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved
#
# patchscript for security components HP-UX



date=`date +"%b\/%d\/%Y"`
Timestamp=$(/bin/date +%Y.%m.%d.%H.%M)

#
# Commands
#
CP="/bin/cp"
LS="/bin/ls"
CAT="/bin/cat"
PWD="/bin/pwd"
SED="/bin/sed"
CHMOD="/bin/chmod"
MKDIR="/bin/mkdir"
AWK="/bin/awk"
GREP="/bin/grep"
RM="/bin/rm"

base_dir=`${PWD}`
security_base_dir="$base_dir/../.."

dest=${base_dir}/dist/patches

while getopts rp:b:f option
do
        case $option in
		p)      common=$OPTARG;;
        esac
done

if [ "$common" = "" ] ;
then
    echo "Usage $0 -p <patch_dir>"
    exit 1
fi

if [ ! -d $common ] ;
then
	echo "No patch description at $common"
	exit 1
fi


if [ ! "${JSS3_LOCATION}" = "" ] ;
then
    echo "Using JSS3 from ${JSS3_LOCATION}"
fi

common=`cd ${common};pwd`

echo "Using directory ${common} to build patch."

# Get component versions
get_nspr_mod () {
    from_file=$1
    search_string=$2
    value=`grep ^${search_string} ${from_file} | /usr/bin/sed -e 's: *::g' -e 's|^.*=||g'`

    echo ${value}
}
get_nspr_version () {
    if [ -f  ${security_base_dir}/nspr/config/module.df ] ;
    then
      # NSPR 4.1
      search_file=${security_base_dir}/nspr/config/module.df
      nspr_mod_version=`get_nspr_mod ${search_file} MOD_VERSION`
      nspr_mod_minor=`get_nspr_mod ${search_file} MOD_MINOR`
      nspr_mod_patch=`get_nspr_mod ${search_file} MOD_PATCH`
      value=${nspr_mod_version}.${nspr_mod_minor}.${nspr_mod_patch}
    else
      # NSPR 4.4
	  value=`grep PR_VERSION ${security_base_dir}/nspr/pr/include/prinit.h | sed -e 's/"$//' -e 's/.*"//' -e 's/ .*//'`
    fi
    echo $value
}

nspr_version=`get_nspr_version`
nss_version=`grep NSS_VERSION ${security_base_dir}/nss/lib/nss/nss.h | head -1 | sed -e 's/[^"]*"//' -e 's/".*//' -e 's/ .*//' | cut -d. -f1-3`
jss_version=`grep JSS_VERSION ${security_base_dir}/jss/org/mozilla/jss/util/jssver.h | sed -e 's/"$//' -e 's/.*"//' -e 's/ .*//'`
jss_major=`echo $jss_version | cut -d . -f 1`

echo "NSPR $nspr_version"
echo "NSS $nss_version"
echo "JSS $jss_version"

build_readme () {
  echo "from template:"
  ls -l ${common}/patch.README

  ${SED} \
  	  -e "s/#PATCHID#/${thispatchnum}/g" \
      -e "s/#PATCHID_BASE#/${patch_num}/g" \
      -e "s/#PATCH_REV#/${patch_rev}/g" \
      -e "s/#DATE#/${date}/g" \
      -e "s/#SECURITY_VERSION#/${security_version}/g" \
      -e "s/#NSPR_VERSION#/${nspr_version}/g" \
      -e "s/#NSS_VERSION#/${nss_version}/g" \
      -e "s/#JSS_VERSION#/${jss_version}/g" \
      -e "s/#DEPOT_NAME#/${thispatchnum}/g" \
	  -e "/^#/d" \
      ${common}/patch.README > ${thispatch}/README.${thispatchnum}
  
  err=$?
  if [ ! $err -eq 0 ] ;
  then
    echo "Unable to create README file (error $err)"
    exit 1
  fi
}

# get variable from patch.infos file
INFO_SEP==

get_info () {
    info_param=$1
    value=`${GREP} ^$info_param$INFO_SEP ${common}/patch.infos | ${AWK} -F$INFO_SEP '{ print $2 }'`
    echo $value
}

# parse info files for this patch
patch_rev=`get_info REV`
patch_num=`get_info PATCH_NUM`
security_version=`get_info SECURITY_VERSION`
psf_template=`get_info PSF_TEMPLATE`

echo "========================================"
echo "patch_rev=$patch_rev"
echo "patch_num=${patch_num}"
echo "========================================"

  # Build dir
  patch_build_dir=build/${patch_num}
  rm -rf ${patch_build_dir}
  mkdir -p ${patch_build_dir}

  thispatchnum=${patch_num}-${patch_rev}
  thispatch=${dest}/${thispatchnum}

echo "thispatch=$thispatch"

###########################
# Delete the old stuff
###########################
  if [ -d ${thispatch} ]; then
     echo "removing any previously built version of this patch"
     rm -rf ${thispatch}
  fi

###################################
# Create the patch output directory
##################################
  if [ ! -d ${thispatch} ]; then
     ${MKDIR} -p ${thispatch}
  fi

###############################
# Create README.<patchnum> file
###############################

  echo "======building readme...."
  build_readme
  echo "======done building readme...."

###############################
# Create the depot
################################
OBJDIR32=HP-UXB.11.11_OPT.OBJ
OBJDIR64=HP-UXB.11.11_64_OPT.OBJ
MOZDIST=${security_base_dir}/dist

# NSPR
NSPR_LIB32_DIR=${MOZDIST}/${OBJDIR32}/lib
NSPR_LIB64_DIR=${MOZDIST}/${OBJDIR64}/lib
NSPR_INC_DIR=${MOZDIST}/${OBJDIR32}/include
NSPR_LOC=/opt/sun/private

# NSS
NSS_LIB32_DIR=${MOZDIST}/${OBJDIR32}/lib
NSS_LIB64_DIR=${MOZDIST}/${OBJDIR64}/lib
NSS_BIN32_DIR=${MOZDIST}/${OBJDIR32}/bin
NSS_BIN64_DIR=${MOZDIST}/${OBJDIR64}/bin
NSS_INC_DIR=${MOZDIST}/public/nss
NSS_LOC=/opt/sun/private

# JSS
JSS_LIB32_DIR=${MOZDIST}/${OBJDIR32}/lib
JSS_LIB64_DIR=${MOZDIST}/${OBJDIR64}/lib
JSS_JAR_DIR=${MOZDIST}/release/no-policy/classes
JSS_LOC=/opt/sun/private

# PSF files from templates
psf_file=${thispatchnum}.psf
echo "Create psf From ${common}/${psf_template} to ${patch_build_dir}/${psf_file}"
cat ${common}/${psf_template} | sed \
  -e "s|#NUMBER#|${Timestamp}|g" \
  -e "s|#VERSION#|${VERSION}|g" \
  -e "s|#PATCHID_BASE#|${patch_num}|g" \
  -e "s|#PATCH_REV#|${patch_rev}|g" \
  -e "s|#PATCHID#|${thispatchnum}|g" \
  -e "s|#NSPR_VERSION#|${nspr_version}|g" \
  -e "s|#NSS_VERSION#|${nss_version}|g" \
  -e "s|#JSS_VERSION#|${jss_version}|g" \
  -e "s|#NSPR_LIB32_DIR#|${NSPR_LIB32_DIR}|g" \
  -e "s|#NSPR_LIB64_DIR#|${NSPR_LIB64_DIR}|g" \
  -e "s|#NSPR_BIN32_DIR#|${NSPR_BIN32_DIR}|g" \
  -e "s|#NSPR_BIN64_DIR#|${NSPR_BIN64_DIR}|g" \
  -e "s|#NSPR_INC_DIR#|${NSPR_INC_DIR}|g" \
  -e "s|#NSPR_LOC#|${NSPR_LOC}|g" \
  -e "s|#NSS_LIB32_DIR#|${NSS_LIB32_DIR}|g" \
  -e "s|#NSS_LIB64_DIR#|${NSS_LIB64_DIR}|g" \
  -e "s|#NSS_BIN32_DIR#|${NSS_BIN32_DIR}|g" \
  -e "s|#NSS_BIN64_DIR#|${NSS_BIN64_DIR}|g" \
  -e "s|#NSS_INC_DIR#|${NSS_INC_DIR}|g" \
  -e "s|#NSS_LOC#|${NSS_LOC}|g" \
  -e "s|#JSS_LIB32_DIR#|${JSS_LIB32_DIR}|g" \
  -e "s|#JSS_LIB64_DIR#|${JSS_LIB64_DIR}|g" \
  -e "s|#JSS_JAR_DIR#|${JSS_JAR_DIR}|g" \
  -e "s|#JSS_LOC#|${JSS_LOC}|g" \
> ${patch_build_dir}/${psf_file}

  err=$?
  if [ ! $err -eq 0 ] ;
  then
    echo "Unable to create psf file (error $err)"
    exit 1
  fi

# Create the depot for the patch
echo "Create depot directory with ${patch_build_dir}/${psf_file} to ${thispatch}"
/usr/sbin/swpackage \
  -x enforce_dsa=false \
  -x follow_symlinks=true \
  -x compression_type=gzip \
  -x compress_cmd=/usr/contrib/bin/gzip \
  -x uncompress_files=false \
  -x compress_files=true  \
  -x reinstall_files=true \
  -x package_in_place=false \
  -x target_type=directory  \
  -x write_remote_files=true \
  -x run_as_superuser=false \
  -s ${patch_build_dir}/${psf_file} @ ${thispatch}

  err=$?
  if [ ! $err -eq 0 ] ;
  then
    echo "Unable to create depot (error $err)"
    exit 1
  fi



  echo "----------done creating HP-UX patches --------------"
