#!/bin/sh
# Based roughly off of Brian White's <bcwhite@pobox.com> netscape4
# installer script.
# -- Robert Woodcock <rcw@oz.net>

function tempcleanup {
	if [ -e "$uvscantmpdir" ]; then
		cd $tmp
#		echo -n - Deleting temp dir $uvscantmpdir...
		rm -rf $uvscantmpdir
		echo
	fi
}	

#
# Avoid unnecessary "postinst" if the next version's "preinst" fails
#
if [ "$0" = "abort-upgrade" ]; then
	exit 0;
fi

# Don't run this if we're not installing
if [ "$1" = "configure" ] && [ "$2" != "" ]; then
	# HOWEVER work around a bug in 3-2 where a reinstall *is* needed
	# because of a bug in its prerm.
	if [ "$2" != "3-2" ]; then
	        exit 0;
	fi
fi

#
# Find the tmp directory
#
tmp=${TMPDIR-/tmp}

#
# If uvscan is already available, then skip rest of setup!
#
if [ -e "/usr/lib/neta/uvscan" ]; then
	echo "- Old uvscan files found -- no need to reinstall them"
	echo "- If you want to force a reinstall, first do: dpkg --purge uvscan"
	exit 0
fi


#
# Unpack the uvscan archive
#
uvscantmpdir=$tmp/uvscan-unpacked.$$
# Double-check that noone is trying to be 3r33t in /tmp
if [ -e "$uvscantmpdir" ]; then
	# Our temp directory already exists - eeeek!
	echo - Temp unpack directory $uvscantmpdir exists\! Exiting...
	exit 1
fi

mkdir $uvscantmpdir
if [ "$?" != "0" ]; then
	echo - Could not create $uvscantmpdir. Exiting...
	exit 1
fi

if [ "`echo $tmp/nlxb*[le].tar`" = "$tmp/nlxb*[le].tar" ]; then
	# No matching files found
	cat << EOF
- Error! No McAfee Virusscan archives have been found in /tmp.
- You can download the 30-day trial version of Virusscan from:
- ftp://ftp.nai.com/pub/antivirus/unix/linux/
- This installer will not run without them. Exiting...
EOF
	# Here is where we should probably offer to download the
        # evaluation version
	tempcleanup
	exit 1
fi

# Doublecheck the filename we wanna use, if there's more than one

if [ "`echo $tmp/nlxb*[le].tar | awk '{print($2)}'`" != "" ]; then
	# We have multiple files to pick from - ask user which one
	echo - Multiple possible Virusscan files found in /tmp.
	i=0;
	for possible in $tmp/nlxb*[le].tar
	do
		i=`expr $i + 1`
		echo - $i\) `ls -l $possible`
	done
	read -p "Please enter the number of the one you wish to use [1-$i]: " choice
	# Check that $choice is valid
	if [ -e "`echo $tmp/nlxb*[le].tar | cut -d' ' -f$choice`" ]; then
		uvscantounpack=`echo $tmp/nlxb*[le].tar | cut -d' ' -f$choice`
		echo - Using $uvscantounpack...
	else
		echo "- Invalid selection!"
		tempcleanup
		exit 1
	fi
else
	# Just one, so use it without asking
	uvscantounpack=`echo $tmp/nlxb*[le].tar`
	echo - Using $uvscantounpack...
fi

# Check file to make sure it's what we want

# Is it owned by us?
if [ -O "$uvscantounpack" ] && [ -G "$uvscantounpack" ]; then
	: # Do nothing, truthfully
else
	read -p "$uvscantounpack is NOT owned by you. Proceed? [y/n]: " proceed
	if [ "$proceed" != "y" ]; then
		echo - Exiting...
		tempcleanup
		exit 1
	fi
fi

# File is owned by us, ass-u-me that it's safe to proceed

cd $uvscantmpdir

tar xOf $uvscantounpack linux-elf.'*'.tar.Z | gzip -dc | tar xf -

if [ "$?" != "0" ]; then
	echo - Unpacking failed\!
	tempcleanup
	exit 1
fi

install -o 0 -g 0 -m 755 -d /usr/lib/neta
install -o 0 -g 0 -m 755 -s uvscan /usr/lib/neta
install -o 0 -g 0 -m 644 clean.dat /usr/lib/neta
install -o 0 -g 0 -m 644 names.dat /usr/lib/neta
install -o 0 -g 0 -m 644 scan.dat /usr/lib/neta
gzip -9 uvscan.1
install -o 0 -g 0 -m 644 uvscan.1.gz /usr/man/man1
install -o 0 -g 0 -m 644 Packing.lst /usr/doc/uvscan
install -o 0 -g 0 -m 644 Readme.1st /usr/doc/uvscan
install -o 0 -g 0 -m 644 Reseller.txt /usr/doc/uvscan
install -o 0 -g 0 -m 644 Whatsnew.txt /usr/doc/uvscan

tempcleanup
