#!/bin/bash
#
# Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved
#
# patchscript for security components Windows



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

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


common=`cd ${common};pwd`

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

msys2dos () {
idir=$1
       echo $idir | sed -e "s|^/\(.\)/|\1:/|"
} # msys2dos

# Get component versions
get_nspr_mod () {
    from_file=$1
    search_string=$2
    value=`grep ^${search_string} ${from_file} | 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 replaces the #xxx# values in the README
  # tr removes the Windows ^M (end of line) and ^Z (end of file)

  cat ${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" \
   | tr -d '\15\32' \
   > ${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`
reference_build=`get_info REFERENCE_BUILD`
jss3_reference_build=`get_info JSS3_REFERENCE_BUILD`

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

##############################################################
# Verifying the set of tools needed for the Windows installer
##############################################################
echo "========================================"
echo "Verifying the list of tools needed for the Windows installer"
# ANT
echo "ANT:"
which ant
if [ ! $? -eq 0 ] ;
then
	echo "ANT is required"
	echo "PATH=$PATH"
	exit 1
fi
# InstallShield
echo "IsSABld.exe (StandAlone InstallShield 11.5):"
which IsSABld.exe
if [ ! $? -eq 0 ] ;
then
	echo "IsSABld.exe is required"
	echo "PATH=$PATH"
	exit 1
fi
# UUIDGEN.exe (from MSVC)
echo "UUIDGEN.exe:"
which UUIDGEN.exe
if [ ! $? -eq 0 ] ;
then
	echo "UUIDGEN.exe is required (from MSVC)"
	echo "PATH=$PATH"
	exit 1
fi
# make
echo "MAKE:"
MAKE="gmake.exe"
which $MAKE
if [ ! $? -eq 0 ] ;
then
       MAKE="make.exe"
       which $MAKE
       if [ ! $? -eq 0 ] ;
       then
               echo "MAKE not found (tried gmake.exe and make.exe)"
               exit 1
       fi
fi
echo "MAKE=${MAKE}"
# JAVA_HOME
if [ -z "$JAVA_HOME" ] ;
then
	echo "JAVA_HOME must be set"
	exit 1
fi
# ANT_HOME
if [ -z "$ANT_HOME" ] ;
then
	echo "ANT_HOME must be set"
	exit 1
fi

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...."
  
###############################
# Adding copyright file
###############################
  echo "Copying copyright file from ${common} to ${thispatch}"
  cp ${common}/copyright ${thispatch}
  if [ ! $? -eq 0 ] ;
  then
	echo "error cp copyright"
	exit 1
  fi
  echo "========================================"

###############################
# Create the installer patch
################################
# See documentation of this process at
# http://multiplatform.sfbay.sun.com/Wiki.jsp?page=JES5WindowsPatch_With_ISMAutoUpdate
################################

OBJDIR=WINNT5.0_OPT.OBJ
MOZDIST=${security_base_dir}/mozilla/dist/${OBJDIR}/pkgarchive

installer_build_dir=${base_dir}/${patch_build_dir}/installer
rm -rf ${installer_build_dir}
mkdir -p ${installer_build_dir}
installer_jes5_dir=${installer_build_dir}/src/installer/JES5

# Get patch build workspace from CVS -----------------------------------------
cd ${installer_build_dir}
# Check out the JES5 ISM,Installer related source and patch build scripts.
CVSROOT=:pserver:svbld@jescvs.us.oracle.com:/sw/jeswindows 
cvs co src/installer/JES5
if [ ! $? -eq 0 ] ;
then
	echo "Unable to check out src/installer/JES5"
	echo "CVSROOT=$CVSROOT"
	exit 1
fi
# Check out ListJavaESPatches.exe utility
cvs co src/installer/utilities/ListJesPatches/ListJavaESPatches.exe
if [ ! $? -eq 0 ] ;
then
	echo "Unable to check out src/installer/utilities/ListJesPatches/ListJavaESPatches.exe"
	echo "CVSROOT=$CVSROOT"
	exit 1
fi

# Check out the MSI_auto-sync tool source
CVSROOT=:pserver:svbld@jescvs.us.oracle.com:/sw/jes 
cvs co internal/gatekeeping/Windows
if [ ! $? -eq 0 ] ;
then
	echo "Unable to check out internal/gatekeeping/Windows"
	echo "CVSROOT=$CVSROOT"
	exit 1
fi

# Copy the reference zip files -------------------------------------------------
zip_ref_dir=${installer_build_dir}/zip_ref
mkdir -p ${zip_ref_dir}
cp ${reference_build}/*.zip ${zip_ref_dir}
if [ ! $? -eq 0 ] ;
then
	echo "Unable to copy reference zip files from ${reference_build}"
	exit 1
fi
cp ${jss3_reference_build}/*.zip ${zip_ref_dir}
if [ ! $? -eq 0 ] ;
then
	echo "Unable to copy JSS3 reference zip files from ${jss3_reference_build}"
	exit 1
fi

# Copy the new zip files -------------------------------------------------------
zip_new_dir=${installer_build_dir}/zip_new
mkdir -p ${zip_new_dir}
cp ${MOZDIST}/*.zip ${zip_new_dir}
if [ ! $? -eq 0 ] ;
then
	echo "Unable to copy new zip files from ${MOZDIST}"
	exit 1
fi
# We keep the same JSS3 version here.
cp ${jss3_reference_build}/*.zip ${zip_ref_dir}
if [ ! $? -eq 0 ] ;
then
	echo "Unable to copy JSS3 reference zip files from ${jss3_reference_build}"
	exit 1
fi

patch_script_dir=${installer_build_dir}/src/installer/JES5/Patch/Patch_Scripts
# Modify PatchBuildProperties.mk ----------------------------------------------
old_file=${patch_script_dir}/PatchBuildProperties.mk_old
new_file=${patch_script_dir}/PatchBuildProperties.mk

installer_jes5_dir_dos=$(msys2dos $installer_jes5_dir)
patch_script_dir_dos=$(msys2dos ${patch_script_dir})
zip_ref_dir_dos=$(msys2dos ${zip_ref_dir})
zip_new_dir_dos=$(msys2dos ${zip_new_dir})

# Save original file
mv ${new_file} ${old_file}
# Replace values
cat ${old_file} | sed \
  -e "s|<SUNW PRODUCT NAME>|NSS_NSPR_JSS|g" \
  -e "s|<PRODUCT NAME>|NSS_NSPR_JSS|g" \
  -e "s|<PRODUCT VERSION>|${security_version}|g" \
  -e "s|<SHORT NAME OF THE PRODUCT>.*|NSS_NSPR_JSS|g" \
  -e "s|<PATCH ID>|${thispatchnum}|g" \
  -e "s|<PATH TO PATCH_SCRIPTS>|${patch_script_dir_dos}|g" \
  -e "s|<PATH TO REFERENCE BINARIES OF THE PRODUCT>|${zip_ref_dir_dos}|g" \
  -e "s|<PATH TO TARGET BINARIES OF THE PRODUCT>|${zip_new_dir_dos}|g" \
  -e "s|<PATH TO REFERENCE ISM>|${installer_jes5_dir_dos}|g" \
  -e "s|<FEATURE_NAME>|security|g" \
  -e "s|^SUBFEATURES := None|SUBFEATURES := JSS,NSPR,NSPRD,NSS,NSSD,NSSU|g" \
  -e "s|<PATCH_REVISION>|${patch_rev}|g" \
> ${new_file}

echo "PatchBuildProperties.mk modified with current patch values"
echo "-------------------------------------------------------------------------"
cat ${new_file}
echo "-------------------------------------------------------------------------"

# Modify UpdatePCP.pl ----------------------------------------------------------
old_file=${patch_script_dir}/UpdatePCP.pl_old
new_file=${patch_script_dir}/UpdatePCP.pl
# Save original file
mv ${new_file} ${old_file}
# Replace values
cat ${old_file} | sed \
  -e "s/REINSTALLMODE=o/REINSTALLMODE=e/g" \
  -e "s/<GUID_OUT>/\`uuidgen.exe\`/" \
> ${new_file}
echo "UpdatePCP.pl updated with REINSTALLMODE=e"

# Modify BUGLIST.template ------------------------------------------------------
old_file=${patch_script_dir}/BUGLIST.template_old
new_file=${patch_script_dir}/BUGLIST.template
# Save original file
mv ${new_file} ${old_file}
# Replace values
cat ${old_file} | sed \
  -e "s/<BUGID>/${thispatchnum}/g" \
> ${new_file}
echo "BUGLIST.template update with Patch ID ${thispatchnum}"

# Patch ${patch_script_dir}/Makefile -------------------------------------------
# remove "; \" at the end of a target (needed for the MSYS shell).
makefile_new="${patch_script_dir}/Makefile"
makefile_orig="${patch_script_dir}/Makefile.orig"
mv $makefile_new $makefile_orig
perl -e 'undef $/;while (<>) {s/; \\.^$/\n/msg;print;}' <$makefile_orig >$makefile_new

# Run installer build ----------------------------------------------------------
cd ${patch_script_dir}
${MAKE} createWinPatch
if [ ! $? -eq 0 ] ;
then
	echo "${MAKE} createWinPatch failed"
	exit 1
fi

# Copy exe and uninstall batch to patch location -------------------------------
echo "Copying patch EXE:"
ls ${installer_jes5_dir}/${thispatchnum}/${thispatchnum}.exe
cp ${installer_jes5_dir}/${thispatchnum}/${thispatchnum}.exe ${thispatch}
if [ ! $? -eq 0 ] ;
then
	echo "Error cp patch EXE"
	exit 1
fi

echo "Copying patch Uninstall BAT:"
ls ${installer_jes5_dir}/${thispatchnum}/Uninstall_${thispatchnum}.bat
cp ${installer_jes5_dir}/${thispatchnum}/Uninstall_${thispatchnum}.bat ${thispatch}
if [ ! $? -eq 0 ] ;
then
	echo "error cp patch Uninstall BAT"
	exit 1
fi

echo "Copying ListJavaESPatches.exe"
ls ${installer_jes5_dir}/${thispatchnum}/ListJavaESPatches.exe
cp ${installer_jes5_dir}/${thispatchnum}/ListJavaESPatches.exe ${thispatch}
if [ ! $? -eq 0 ] ;
then
	echo "error cp ListJavaESPatches.exe"
	exit 1
fi


# Remove installer build workspace to save space
echo "Saving modified files"
cp ${patch_script_dir}/PatchBuildProperties.mk ${base_dir}/${patch_build_dir}
cp ${patch_script_dir}/UpdatePCP.pl ${base_dir}/${patch_build_dir}
cp ${patch_script_dir}/BUGLIST.template ${base_dir}/${patch_build_dir}
echo "Removing installer workspace"
cd ${base_dir}
rm -rf ${installer_build_dir}

# -------------------------------------------
echo "----------done creating Windows patches --------------"
