#!/bin/bash


#
# Installing Gnash dependencies.
#
#
# Gnash at runtime needs
# Curl optional package
# OpenSSL optional package
# media-video/ffmpeg
# media-image/giflib
# boost::thread
# boost::date_time
#
#
# Gnash at compile-time needs
# sed
# gcc4
# libtool
# autotools
# Curl optional package
# OpenSSL optional package
# media-video/ffmpeg
# media-image/giflib
# boost-all
# agg >= 2.5 (the version in libbe is 2.4, cannot be used and proper care should be taken to link to -lagg before -lbe)
#
#
# Checking Gnash from repository requires
# Python >= 2.4
# dev-util/bzr
#
#
# Building the depreciated SDL gui additionally requires
# SDL (gcc4 build)  http://students.mimuw.edu.pl/~ap262965/haiku-gnash/SDL-1.2.13-r1a1-x86-gcc4-2009-11-13.zip
# iconv_header (or the LibIconv optional package)   http://students.mimuw.edu.pl/~ap262965/haiku-gnash/iconv-091128.h
#




function ask() {
    case `alert --idea "Install $1?" "Install" "Already Installed" "Bail"` in
        "Install")
            wget "$2"
            bn=`basename "$2"`
            unzip -o -d /boot "$bn"
            ;;
        "Already Installed")
            ;;
        *)
            alert --info "Aborted installing Gnash dependencies." Dismiss
            exit
            ;;
        esac
}

function ask_bzr() {
    case `alert --idea "Install (unstable) bazaar 2.1.0? Bazaar is needed to checkout the Gnash sources." "Install" "Already Installed" "Bail"` in
      "Install")
          if test ! -e bzr-2.1.0.tar.gz ; then
              wget http://launchpad.net/bzr/2.1/2.1.0/+download/bzr-2.1.0.tar.gz ;
              tar xf bzr-2.1.0.tar.gz ;
          fi ;
          pushd bzr-2.1.0 ;
          if test ! -e bzr.old ; then
              mv bzr bzr.old ;
              sed bzr.old > bzr -e 's|/usr/bin/env python|/boot/common/bin/python|' ;
              chmod +x bzr ;
          fi ;
          if test ! -e setup.py.old ; then
              mv setup.py setup.py.old ;
              sed setup.py.old > setup.py -e 's|/usr/bin/env python|/boot/common/bin/python|' ;
              chmod +x setup.py ;
          fi ;
          ./setup.py install ;
          popd ;
          true
          ;;
      "Already Installed")
          true
          ;;
      *)
          echo "NOTE: aborting Gnash installation" ;
          false
          ;;
    esac ;
}

function ask_opt() {
    case `alert --idea "Install $1?" "Install" "Already Installed" "Bail"` in
        "Install")
            installoptionalpackage -a "$1"
            ;;
        "Already Installed")
            ;;
        *)
            alert --info "Aborted installing Gnash dependencies." Dismiss
            exit
            ;;
        esac
}

if [ $# -gt 1 ] || ( [ $# -eq 1 ] && [ x$1 != 'x--compile-time' ] )
then
    alert --info "Usage: $0 [--compile-time]"
    exit 1
fi

if [ $# -eq 1 ]
then
    COMPILE_TIME=yes
else
    COMPILE_TIME=no
fi

DIRNAME="gnash-dependencies"

if [ -e "$DIRNAME" ]
then
    echo "Warning: directory \"$DIRNAME\"/ already present"
fi

mkdir "$DIRNAME"
pushd "$DIRNAME"

ask_opt Sed


ask Ffmpeg-gcc4 http://students.mimuw.edu.pl/~ap262965/haiku-gnash/ffmpeg-gcc4-haiku-2009-09-19.zip
ask giflib http://www.fileden.com/files/2008/8/23/2062382/packages/giflib-4.1.6-gcc2-2008-12-31.zip

if [ $COMPILE_TIME == yes ]
then
    #ask Boost-all http://www.haiku-ports.de/packages/dev-libs/boost/boost-1.38.0-gcc4-haiku-2009-09-13.zip
    ask Boost-all http://students.mimuw.edu.pl/~ap262965/haiku-gnash/boost-1.38.0-gcc4-haiku-2009-09-13.zip
    ask agg-2.5-gcc4 http://students.mimuw.edu.pl/~ap262965/haiku-gnash/agg-2.5-gcc4-haiku-2009-09-13.zip
    ask_bzr
else
    ask Boost::thread http://students.mimuw.edu.pl/~ap262965/haiku-gnash/libboost_thread-1.38.0-gcc4-2009-11-14.zip
    ask Boost::date_time http://students.mimuw.edu.pl/~ap262965/haiku-gnash/libboost_date_time-1.38.0-gcc4-2009-11-14.zip
fi

ask_opt Curl
ask_opt OpenSSL
