#!/bin/bash
# Author Felix Wolfsteller, heavily based on work by
# (C) Vlatko Kosturjak. GPL v2

# sh build-all-svn-3-all.sh /home/myprefix
#
#    will attempt to install libraries, scanner, client, manager and
#    administrator to /home/myprefix.
#    The stdout output of configure/cmake and make will be redirected to e.g.
#    openvas-libraries/make-openvas-libraries.log.
#    Call this script in the parent folder of openvas-libraries,
#    openvas-scanner etc (e.g. in a svn working copy from trunk).
#    This script will not abort gracefully if something went wrong.

OPENVASPREFIX=`date +%F`
# Adjust prefix to your needs in script:
OPENVASPATH=/opt/openvas-svn-$OPENVASPREFIX
# Or pass prefix as argument to script
if [ $# = 1 ] ; then
   OPENVASPATH=$1
fi

FAILED=0

echo "o 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

# Autoconf based
build_ac_module()
{
  cd $1 && ./configure --prefix=$OPENVASPATH > ../configure-$i.log && make > ../make-$i.log && return 0
  return $1
}

# Cmake based
build_cm_module()
{
  cd $1 && cmake -DCMAKE_INSTALL_PREFIX=$OPENVASPATH . > ../configure-$i.log && make > ../make-$1.log && return 0
  return $1
}

# Both: make install
make_install_module()
{
  make install > ../make-install-$i.log && cd .. && return 0
  return $1
}

# (Mainly) autoconf based modules
for i in openvas-libraries openvas-scanner openvas-client
do
	echo "o Building OpenVAS module: $i"
	if build_ac_module $i ; then
	  echo "  - succes"
	else
	  echo "  - failed"
	  FAILED=1
	  break
	fi

	echo "o Installing OpenVAS module: $i"
	if make_install_module $i ; then
	  echo "  - succes"
	else
	  echo "  - failed"
	  FAILED=1
	  break
	fi
done

[ FAILED = 1 ] && {
	echo "o Failed." && exit 1
}

# CMake based modules
for i in openvas-manager openvas-administrator
do
	echo "o Building OpenVAS module: $i"
	if build_cm_module $i ; then
	  echo "  - succes"
	else
	  echo "  - failed"
	  FAILED=1
	  break
	fi

	echo "o Installing OpenVAS module: $i"
	if make_install_module $i ; then
	  echo "  - succes"
	else
	  echo "  - failed"
	  FAILED=1
	  break
	fi
done

[ FAILED = 1 ] && {
	echo "o Failed." && exit 1
}

echo "o Finished installation of OpenVAS to $OPENVASPATH"
exit 0
