#!/bin/sh -x

cd /
#chmod -R u+w bigsis
#rm -rf bigsis
#cp -R //f/NT/bigsis bigsis
cd bigsis

[ -d uxmon ] || (echo "where are we?"; exit 1)
[ -d uxmon ] && chmod -R u+w .

echo "/cgi/bswebadmin.exe\\" > www/skins/default/ADMIN_PATH.inc
echo "/cgi/bswebalarm.exe\\" > www/skins/default/ALARM_PATH.inc
echo "/cgi/bsgraph.exe\\" > www/skins/default/GRAPH_PATH.inc
echo "/cgi/bshistory.exe\\" > www/skins/default/HISTORY_PATH.inc
rm www/skins/default/cache
perl bin/compile_skin default

echo "use modules;" >> bin/common.pm

PERL5LIB=".:`pwd`/bin:`pwd`/uxmon"
PERL5LIB=".;c:\\bigsis\\bin;c:\\bigsis\\uxmon"
export PERL5LIB

modules="Sys::Syslog Sys::Hostname Win32::EventLog Win32::Process Socket FileHandle"

case $OS in
    *indows*)
	modules="$modules BS_win32"
	PERL5LIB=`echo $PERL5LIB| sed 's/:/;/'`
	for d in `echo $PATH| sed 's/:/ /g'`; do
	    if [ -f $d/perlcrt.dll ]; then
		cp $d/perlcrt.dll bin
	    fi
	done
	;;
    *)
	modules="$modules POSIX BS_unix"
	;;
esac

base=""

to_exe() {
    binary=$1
    if [ -f $binary.pl ]; then
	mv $binary.pl $binary
    fi
    shift
    echo "1;" > bin/modules.pm
    for module in $modules $*; do
	perl -Ibin -Iuxmon -c -e "use $module" > /dev/null  && {
	    echo "use $module; 1;" >> bin/modules.pm
	}
    done
    echo "1;" >> bin/modules.pm
    echo "1;" >> bin/modules.pm
    dir=`dirname $binary`
    file=`basename $binary`
    echo "compiling $filename"
    (cd $dir; perl2exe -tiny $file $files)
}



mkdir bin/Monitor
cp uxmon/Monitor/bb.pm bin/Monitor

to_exe bin/bbd display_map Monitor::bb
to_exe bin/install32

monmodules=""
for monmodule in `cd bin; echo Statusmon/*`; do
    monmodule=`echo $monmodule | sed 's#/#::#' | sed 's/.pm$//'`
    monmodules="$monmodules $monmodule"
done
to_exe bin/bsmon $monmodules

to_exe bin/notify Cwd Getopt::Long
to_exe bin/log_mail Net::SMTP

to_exe bin/bsadmin Monitor::bb
to_exe bin/bshistory bscgi
to_exe bin/bswebalarm bscgi Monitor::bb
to_exe bin/bsgraph bscgi Monitor::bb
to_exe bin/bswebadmin bscgi Monitor::bb

to_exe bin/bb_start32
to_exe bin/bb_stop32

monitors=`(cd uxmon; echo Monitor/*) | sed 's/\//::/g' | sed 's/\.pm//g'`
cfg=`(cd uxmon/Config; echo ./*)`
files=`(cd uxmon; echo /bigsis/uxmon/uxmon-rules.pl ./Monitor/* $cfg)`
(cd uxmon; mv Config cfg)
for f in $cfg; do
    cp uxmon/cfg/$f uxmon
done
to_exe uxmon/uxmon $monitors
(cd uxmon; mv cfg Config)
for f in $cfg; do
    rm -f uxmon/$f
done

mv uxmon/uxmon.exe bin || exit
mv *.dll bin
mv uxmon/*.dll bin

rm -rf uxmon
for f in bin/*; do
    case $f in
        *.exe|*.dll)
		;;
	*)
		rm -rf $f
		;;
    esac
done
