#!/bin/bash
# FOSSology fo-installdeps script
# Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
#
# This script helps you install build and runtime dependencies on a system.
# It is NOT indented to replace package dependencies, it's just a tool to
# make testing the "upstream" build and install process. If you determine
# this script isn't installing something you think it should, consult
# the packaging metadata for the system in question as that is the
# canonical location for such info, then fix it there first and also
# update this file and the INSTALL document.

## Options parsing and setup
# parse options
OPTS=`getopt -o rbeah --long runtime,buildtime,everything,agent,help -n 'fo-installdeps' -- "$@"`

if [ $? != 0 ]; then
   echo "ERROR: Bad option specified."
   OPTS="--help"
fi

eval set -- "$OPTS"

# if no options then do everything
if [ "$OPTS" = " --" ]; then
   EVERYTHING=1
fi

while true; do
   case "$1" in
      -r|--runtime) RUNTIME=1; shift;;
      -b|--buildtime) BUILDTIME=1; shift;;
      -e|--everything) EVERYTHING=1; shift;;
      -a|--agent) AGENT=1; shift;;
      -h|--help)
         echo "Usage: fo-installdeps [options]";
	 echo "  -r or --runtime    : install runtime dependencies"
	 echo "  -b or --buildtime  : install buildtime dependencies"
	 echo "  -e or --everything : install all dependencies (default)"
	 echo "  -a or --agent      : only install agent runtime dependencies (implies -r)"
	 echo "  -h or --help       : this help text"
	 exit;;
      --) shift; break;;
      *) echo "ERROR: option $1 not recognised"; exit 1;;
   esac
done

# This must run as root.
if [ `id -u` != "0" ] ; then
   echo "ERROR: fo-installdeps must run as root."
   echo "Aborting."
   exit 1
fi

if [ $EVERYTHING ]; then
   echo "*** Installing both runtime and buildtime dependencies ***"
   RUNTIME=1
   BUILDTIME=1
fi

if [ $AGENT ]; then
   # agent implies runtime
   RUNTIME=1
fi

# figure out what distro we're on
DISTRO=`lsb_release -is`
CODENAME=`lsb_release -cs`

if [ "$?" != "0" ]; then
   echo "ERROR: this program requires the lsb_release command. On Debian based"
   echo "  systems this is probably in the lsb-release package, on"
   echo "  Fedora/RedHat systems it is probably the redhat-lsb package."
   exit 1
fi

########################################################################

if [ $BUILDTIME ]; then
   echo "*** Installing $DISTRO buildtime dependencies ***";
   case "$DISTRO" in
      Debian|Ubuntu)
         apt-get install \
            libmagic-dev libxml2-dev libextractor-dev \
            build-essential libtext-template-perl subversion
         case "$CODENAME" in
            etch|dapper) apt-get install postgresql-server-dev-8.1;;
            lenny|hardy) apt-get install postgresql-server-dev-8.3;;
            sid) apt-get install postgresql-server-dev-8.3;;
            *) echo "ERROR: Unknown or Unsupported $DISTRO release"; exit 1;;
         esac
         ;;
      Fedora)
         # F8=Werwolf F9=Sulphur F10=Cambridge dev=Rawhide
         yum groupinstall "Development Tools"
         yum install \
            perl-Text-Template subversion \
            postgresql-devel file-devel \
            libxml2 libextractor-devel
         ;;
      Mandriva)
         # 2008.1 tested
         urpmi \
            postgresql-devel \
            libextractor-devel libmagic-devel libxml2 \
            perl-Text-Template subversion gcc make perl
         ;;
      RedHatEnterprise*|CentOS)
         # 4=Nahant* 5=Tikanga*
         yum install \
            postgresql-devel \
            file libxml2 \
            perl-Text-Template subversion
         ;;
      *) echo "ERROR: distro not recognised, please fix and send a patch"; exit 1;;
   esac
fi

########################################################################

if [ $RUNTIME ]; then
   echo "*** Installing $DISTRO runtime dependencies ***";
   case "$DISTRO" in
      Debian|Ubuntu)
         [ $AGENT ] || apt-get install apache2 libapache2-mod-php5
         apt-get install \
            mail-transport-agent \
            php5 php5-pgsql php-pear \
            libmagic1 libxml2 libextractor1c2a \
            binutils bzip2 cabextract cpio sleuthkit mkisofs xpdf-utils \
               rpm tar upx-ucl unrar unzip
         case "$CODENAME" in
            dapper) [ $AGENT ] || apt-get install postgresql-8.1;;
            etch)
               [ $AGENT ] || apt-get install postgresql-8.1
               apt-get install libextractor-plugins;;
            lenny|hardy)
               [ $AGENT ] || apt-get install postgresql-8.3
               apt-get install libextractor-plugins;;
            sid)
               [ $AGENT ] || apt-get install postgresql-8.3
               apt-get install libextractor-plugins;;
            *) echo "ERROR: Unknown or Unsupported $DISTRO release"; exit 1;;
         esac
         ;;
      Mandriva)
         [ $AGENT ] || urpmi postgresql-server httpd
            urpmi php php-pear php-pgsql postgresql \
               libextractor libextractor-plugins libxml2 \
               cabextract genisoimage upx sleuthkit binutils bzip2 cpio \
               mkisofs poppler-utils rpm tar unzip gzip
         echo "NOTE: unrar is available in PLF, please install it via urpmi if"
	 echo "   the repository is configured or from http://plf.zarb.org/packages.php"
         ;;
      Fedora)
         [ $AGENT ] || yum install postgresql-server httpd
         yum install \
            postgresql \
            php php-pear php-pgsql \
            smtpdaemon \
            file-libs libxml2 libextractor libextractor-plugins \
            binutils bzip2 cabextract cpio genisoimage poppler-utils \
               rpm tar upx unzip gzip

         # enable, possible init, and start postgresql
         /sbin/chkconfig postgresql on
         if [ ! -f /var/lib/pgsql/data/PG_VERSION ]; then
            /sbin/service postgresql initdb
         fi
         /sbin/service postgresql start

         # F8=Werwolf F9=Sulphur F10=Cambridge dev=Rawhide
         case "$CODENAME" in
            Werwolf|Sulphur)
               echo "NOTE: sleuthkit and unrar is not available in Fedora release $CODENAME,";
               echo "   please install from upstream sources.";;
            Cambridge|Rawhide)
               yum install sleuthkit
               echo "NOTE: unrar is not available in Fedora release $CODENAME,"
               echo "   please install from upstream sources."
	       ;;
            *) echo "ERROR: Unknown or Unsupported Fedora release"; exit 1;;
         esac
         ;;
      RedHatEnterprise*|CentOS)
         # 4=Nahant* 5=Tikanga*
         [ $AGENT ] || yum install postgresql-server httpd
         yum install \
            postgresql \
            php php-pear php-pgsql \
            smtpdaemon \
            file libxml2 \
            binutils bzip2 cpio mkisofs poppler-utils rpm tar unzip gzip
         echo "NOTE: libextractor, cabextract, sleuthkit, upx, and unrar are not"
         echo "    available in RHEL please install from upstream sources.";;
      *) echo "ERROR: distro not recognised, please fix and send a patch"; exit 1;;
   esac
fi

########################################################################
