#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/kick-av-update,v 1.4 2002/05/24 13:01:38 bwhittak Exp $
#
# (C) Copyright Network Associates Inc. 2001
#
if [ -f $NETAWSS/.profile.vars ]; then
    . $NETAWSS/.profile.vars
else
    . /var/NAIENV/.profile.vars
fi
[[ "$PATH" == "$wsPATH":* || -z "$wsPATH" ]] || PATH=$wsPATH:$PATH

shopt -s extglob

# Start av-update co-operatively.  If two copies are started at the same time
# (for example, by cron) then one av-update will be run, combining the
# functions of engine and DAT update from one server.  Assumes that the desired
# source site, path, user, password are the same.

# default values
get_eng=false
get_dat=false
unset eng_zip dat_zip
unset proxy pport puser ppass

# Process options
while [[ $# -gt 0 ]]; do
    case $1 in
    ('-engpath') eng_dir="$2"; shift;;
    ('-datpath') dat_dir="$2"; shift;;
    ('-eng') get_eng=true ;;
    ('-dat') get_dat=true ;;
    ('-src') server="$2"; shift;;
    ('-srcpath') srcpath="$2"; shift;;
    ('-user') user="$2"; shift;;
    ('-pass') pass="$2"; shift;;
    ('-proxy') proxy="$2"; shift;;
    ('-pport') pport="$2"; shift;;
    ('-puser') puser="$2"; shift;;
    ('-ppass') ppass="$2"; shift;;
    (*) echo "$0 bad option: $1" >&2;;
    esac
    shift
done

# if neither -eng nor -dat specified then assume both
$get_eng || $get_dat || get_eng=true get_dat=true

# say we started
$get_eng && echo "_STARTED_" >$XMLCONFDIR/eng_update_status
$get_dat && echo "_STARTED_" >$XMLCONFDIR/dat_update_status

# rendezvous directory
cdir=/tmp/kick-av-update.d

# post our options in the common directory
(
    until mkdir -p "$cdir"; cd "$cdir"; do
	sleep 1
    done
    $get_eng && echo -n "-eng ${eng_dir:+-engpath '$eng_dir' }" > opt.eng
    $get_dat && echo -n "-dat ${dat_dir:+-datpath '$dat_dir' }" > opt.dat
)
# wait for someone else to do the same
sleep 30
# grap the directory and run with all the options...
if mv "$cdir" "$cdir.$$"; then
    opts=$(cat "$cdir.$$"/opt.*)
    rm -rf "$cdir.$$"
    eval 'exec av-update \
	${server:+-src "$server"} \
	${srcpath:+-srcpath "$srcpath"} \
	${user:+-user "$user"} \
	${pass:+-pass "$pass"} \
	${proxy:+-proxy "$proxy"} \
	${pport:+-pport "$pport"} \
	${puser:+-puser "$puser"} \
	${ppass:+-ppass "$ppass"} \
	' $opts
else
    # ... or someone else must have got there first
    exit 0
fi
