#!/bin/sh
# Download latest OpenVAS stable version, compile and install
# (C) Vlatko Kosturjak. GPL v2
# Authors: Vlatko Kosturjak, Hartmut Goebel
# 
# Some examples:
# SUDOCMD="sudo" sh $0 
# OVNOCLI="yes" sh $0
# OPENVASPATH="/opt/openvas-3" sh $0
# OVSKIPLATEST="yes" sh $0
# OVSKIPRM="yes" sh $0
# OVSKIPBUILD="yes" sh $0
# OVSKIPRM="yes" OVSKIPBUILD="yes" sh $0

if [ -z "$OPENVASPATH" ]; then
	OPENVASPREFIX="`date +%F`"
	OPENVASPATH="/opt/openvas-$OPENVASPREFIX"
	echo "[i] Using default installation path: $OPENVASPATH"
else
	echo "[i] Using specified installation path: $OPENVASPATH"
fi

OPENVASTMP="/tmp/openvas-$$/"
OPENVASDL="/tmp/openvas-dl-$$/"

# Determine latest version
if [ -z "$OVSKIPLATEST" ]; then
	echo "[i] Determing latest OpenVAS version"
	TARLINKS=$(lynx --dump --listonly http://www.openvas.org \
	| grep -i 'wald.intevation.org/.*/.*openvas-.*-3.*tar.gz' \
	| sed -e 's#^.*http://#http://#ig')
	OPENVASLIB=$(echo "$TARLINKS" | grep -i 'openvas-libraries-3.*tar.gz' | tail -1)	
	if [ -z "$OPENVASLIB" ]; then
		echo "[w] Could not determine latest version of openvas-libraries"
	else
		echo "[i] Using: $OPENVASLIB"
	fi
	OPENVASS=$(echo "$TARLINKS" | grep -i 'openvas-scanner-3.*tar.gz' | tail -1)
	if [ -z "$OPENVASS" ]; then
		echo "[w] Could not determine latest version of openvas-scanner"
	else
		echo "[i] Using: $OPENVASS"
	fi
	OPENVASC=$(echo "$TARLINKS" | grep -i 'openvas-client-3.*tar.gz' | tail -1)
	if [ -z "$OPENVASC" ]; then
		echo "[w] Could not determine latest version of openvas-client"
	else
		echo "[i] Using: $OPENVASC"
	fi
else
	echo "[i] Being stupid about latest version, as asked. Using hard coded links."
fi

if [ -z "$OPENVASLIB" ]; then
	OPENVASLIB="http://wald.intevation.org/frs/download.php/717/openvas-libraries-3.0.4.tar.gz"
fi
if [ -z "$OPENVASS" ]; then
	OPENVASS="http://wald.intevation.org/frs/download.php/696/openvas-scanner-3.0.1.tar.gz"
fi
if [ -z "$OPENVASC" ]; then
	OPENVASC="http://wald.intevation.org/frs/download.php/685/openvas-client-3.0.0.tar.gz"
fi

echo "[i] Downloading OpenVAS to $OPENVASDL"

if [ -z "$OVSKIPBUILD" ]; then
    rm -rf $OPENVASDL 
    mkdir -p $OPENVASDL
    wget $OPENVASLIB -O $OPENVASDL/openvas-libraries.tar.gz
    wget $OPENVASS -O $OPENVASDL/openvas-scanner.tar.gz
    if [ -z "$OVNOCLI"]; then
	wget $OPENVASC -O $OPENVASDL/openvas-client.tar.gz
    else
	echo "[i] Skipping OpenVAS client as instructed"
    fi

    echo "[i] Extracting OpenVAS to $OPENVASTMP"
    rm -f $OPENVASTMP 
    mkdir -p $OPENVASTMP
    cd $OPENVASTMP
    tar xzf $OPENVASDL/openvas-libraries.tar.gz
    tar xzf $OPENVASDL/openvas-scanner.tar.gz
    if [ -z "$OVNOCLI"]; then
	tar xzf $OPENVASDL/openvas-client.tar.gz
    fi

    echo "[i] Installing OpenVAS to $OPENVASPATH"

    # E.g. OpenVAS-Client configure needs to figure out the
    # openvas-library version, therefore set path and ld_library_path

    export LD_LIBRARY_PATH=$OPENVASPATH/lib:$LD_LIBRARY_PATH
    export PATH=$OPENVASPATH/bin:$PATH

    for i in openvas-libraries openvas-scanner ; do
	echo "[i] Installing OpenVAS module: $i"
	cd $i* && ./configure --prefix=$OPENVASPATH && make && $SUDOCMD make install && cd ..
    done

    if [ -z "$OVNOCLI"]; then
	echo "[i] Installing OpenVAS client"
	cd openvas-client* && ./configure --prefix=$OPENVASPATH && make && $SUDOCMD make install && cd ..
    fi

    echo "[i] Finished installation of OpenVAS to $OPENVASPATH"
fi # end of if OVSKIPBUILD

if [ -z "$OVSKIPRM" ]; then
	echo "[i] Removing downloaded files and temporary directories"
	rm -rf $OPENVASDL
	rm -rf $OPENVASTMP
fi

