#!/bin/bash
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.

me=${0##*/} mydir=${0%$me}

shopt -s extglob
shopt -s nullglob
shopt -s dotglob

GetBase()
{	# get the base directory if needed
    [[ -d $NETAWSS ]] && return
    [[ -f /var/NAIENV/.profile.vars ]] && . /var/NAIENV/.profile.vars
    [[ -d ${NETAWSS:=/opt/NETAwss} ]] || exit
}

# so where are we to put this installer?
if [[ $# -gt 0 ]]; then
    dest=$1
    shift
fi
if [[ -z $dest ]]; then
    GetBase && dest=$NETAWSS/ui/www/apps/WebShieldClientUnix.sh
fi

# where do the jars come from?
if [[ $# -le 0 ]]; then
    GetBase && set -- $NETAWSS/ui/www
fi

# set up args for tar content
jarlist=$(find "$@" -type f -name '*.jar' -print)
unset args
for f in ${mydir}setup ${mydir}ws_launch_browser $jarlist; do
    t=${f##*/} h=${f%$t}
    args="$args${h:+ -C $h} $t"
done

set -e	# bail out on error from here on

# make the tar
tmp=/tmp/$me.$$.tar
trap "rm -f $tmp" EXIT	# remove it on exit for any reason
tar -cf $tmp $args

# build the installer
ibase=${mydir}installer.sh
c=$(wc -c <$tmp) k=$(cksum <$tmp) s=$( (echo ''; cat $ibase) | wc -l)
rm -f "$dest"
[[ -n ${dest%/*} ]] && mkdir -p ${dest%/*}
sed -e "s/^\(skiplines=\).*/\1${s//[^0-9]}/" \
    -e "s/^\(checksize=\).*/\1'$c'/" \
    -e "s/^\(checkcksum=\).*/\1'$k'/" \
    $ibase >"$dest"
cat $tmp >>"$dest"

