#!/bin/bash
#
# This script installs firefox bundled with java and flash in a local
# directory on linux for testing. It can be used to test different
# versions of firefox/java/flash on the same host.
#
# It is re-entrant.
#
# To install:
#    $ ./firefox-flash-java.sh
#
# To run:
#    $ ./rtf/firefox.sh
#

# ================================================================
# Useful command.
# ================================================================
function docmd() {
    cmd="$*"
    echo
    echo "# ================================================================"
    echo "# CMD: $cmd"
    echo "# PWD: "`pwd`
    echo "# ================================================================"
    $cmd
    st=$?
    if (( $st )) ; then
	echo "EXIT: Command exited with status $st (FAILED)."
	echo "      CMD: $cmd"
	exit 1
    else
	echo "EXIT: Command exited with status $st (PASSED)."
    fi
}

# ================================================================
# main
# ================================================================
umask 0
medir=$(readlink -f .)
rtfdir="$medir/rtf"
repodir="$medir/repo"

if [ ! -d $repodir ] ; then
    mkdir -p $repodir
fi
if [ ! -d $rtfdir ] ; then
    mkdir -p $rtfdir
fi

# ================================================================
# Firefox
# ================================================================
REPO_FILE="firefox-15.0.1.tar.bz2"
if [ ! -f $repodir/$REPO_FILE ] ; then
    pushd $repodir
    docmd wget http://releases.mozilla.org/pub/mozilla.org/firefox/releases/15.0.1/linux-x86_64/en-US/$REPO_FILE
    ls -lh $REPO_FILE
    popd
fi

if [ ! -f $rtfdir/firefox/firefox ] ; then
    pushd $rtfdir
    docmd tar jxf $repodir/$REPO_FILE
    popd
fi
docmd test -f $rtfdir/firefox/firefox

# ================================================================
# Java
# ================================================================
REPO_FILE="jre-7u7-linux-x64.tar.gz"
if [ ! -f $repodir/j$REPO_FILE ] ; then
    pushd $repodir
    docmd wget javadl.sun.com/webapps/download/AutoDL?BundleId=68236 -O $REPO_FILE
    ls -lh $REPO_FILE
    popd
fi
if [ ! -d $rtfdir/jre1.7.0_07 ] ; then
    pushd $rtfdir
    docmd tar zxf $repodir/$REPO_FILE
    popd
fi
docmd test -f $rtfdir/jre1.7.0_07/bin/java

# ================================================================
# Create firefox/java plugin
# ================================================================
docmd test -f $rtfdir/jre1.7.0_07/lib/amd64/libnpjp2.so
if [ ! -d $rtfdir/firefox/plugins ] ; then
    mkdir -p $rtfdir/firefox/plugins
fi
if [ ! -h $rtfdir/firefox/plugins/libnpjp2.so ] ; then
    ln -s $rtfdir/jre1.7.0_07/lib/amd64/libnpjp2.so $rtfdir/firefox/plugins/
fi
docmd test -h $rtfdir/firefox/plugins/libnpjp2.so
# When firefox starts up java will be recognized.

# ================================================================
# Flash
# ================================================================
REPO_FILE=install_flash_player_11_linux.x86_64.tar.gz
if [ ! -f $repodir/j$REPO_FILE ] ; then
    pushd $repodir
    docmd wget http://fpdownload.macromedia.com/get/flashplayer/pdc/11.2.202.238/$REPO_FILE
    ls -lh $REPO_FILE
    popd
fi
if [ ! -f $rtfdir/firefox/plugins/libflashplayer.so ] ; then
    pushd $rtfdir/firefox/plugins
    docmd tar zxf $repodir/$REPO_FILE
    popd
fi
docmd test -f $rtfdir/firefox/plugins/libflashplayer.so
# When firefox starts up flash will be recognized.

# ================================================================
# Create the firefox.sh script.
# ================================================================
if [ ! -f $rtfdir/firefox.sh ] ; then
    cat >$rtfdir/firefox.sh <<EOF
#!/bin/bash
MYARGS="\$*"
export PATH="$rtfdir/firefox:$rtfdir/jre1.7.0_07/bin:\${PATH}"
export CLASSPATH="$rtfdir/jre1.7.0_07/lib:\${CLASSPATH}"
firefox $MYARGS
EOF
    chmod a+x $rtfdir/firefox.sh
fi
docmd test -f $rtfdir/firefox.sh

# ================================================================
# Tell the user how to run it.
# ================================================================
cat <<EOF

The following packages have been installed in $rtfdir:

  1. firefox 15.0.1
  2. java jre-7u7
  3. flash 11.2.202.238

You can run this version of firefox with this command:

  $ $rtfdir/firefox.sh

EOF


