#!/bin/bash

######################################################################
# constants

# Directory in which we will find Firefox packages.
# Subdirectories are expected for each version.
PKGDIR=/pub/org/Mozilla/Firefox

# Directory to unpack (install) Firefox to.
# Subdirectories will be created for each version.
INSTBASEDIR=/all/opt/firefox

######################################################################
# functions

# ----------------------------------------------------------------------
function die () {
# abort program with message

echo "$0: $*" > /dev/stderr
exit 1

}

######################################################################
# main

# Make sure package base directory exists.
[ -d "$PKGDIR" ] || die "Bad package directory: $PKGDIR"
cd "$PKGDIR" || die "chdir failed: $PKGDIR"

# Make sure install base directory exists.
[ -d "$INSTBASEDIR" ] || die "Bad install directory: $INSTBASEDIR"

# Get architecture of the machine we are running on.
ARCH="${ARCH:-$(uname -m)}"

# We check the machine architecture for literal string "64".
# If present, we set BITSKLUDGE to "64" to match.
# If not present, we leave BITSKLUDGE unset (empty).
# We use this later to find "linux" or "linux64".
BITSKLUDGE=
echo $ARCH | fgrep -q 64 && BITSKLUDGE=64

# Firefox "OS".  This really means OS+architecture.
FFSYSTEM="${FFSYSTEM:-linux${BITSKLUDGE}}"

# Find the most recent version subdirectory, using mtime.
# That directory name becomes FFVERSION.
FFVERSION=$(ls -1t | head -n 1)

# Now that we know the version...
# Redefine vars to include the version subdir.
PKGDIR="$PKGDIR/$FFVERSION/$FFSYSTEM"
INSTVERDIR="$INSTBASEDIR/$FFVERSION"

# We can define the package file now, too.
PKGFILE="$PKGDIR/firefox-$FFVERSION.tar.bz2"

# Make sure package file actually exists, as a file.
[ -f "$PKGFILE" ] || die "Bad or missing package: $PKGFILE"

# If install target already exists, exit silently.
[ -e "$INSTVERDIR" ] && exit

# Make install dir for this version.
mkdir "$INSTVERDIR" || die "mkdir trouble"

# Unpack the package to the versioned install dir.
# Strip the first path component, which is always a "firefox".
# (The distribution package has everything under a single "firefox" folder.)
tar --extract --bzip2 --strip-components=1 -C "$INSTVERDIR" -f "$PKGFILE" || die "extract trouble"

# Update "current" symlink
# We create a "new" symlink first, and then move/rename that to "current".
# Any existing "current" symlink will be replaced with that move.
# This ensures "current" either gets updated, or remains untouched.
# The -T switch to mv(1) forces the target to be considered a non-directory.
# Otherwise we end up with a symlink named "current/new".
ln -sf "$FFVERSION" "$INSTBASEDIR/new" || die "symlink create troulbe"
mv -Tf "$INSTBASEDIR/new" "$INSTBASEDIR/current" || die "symlink move/rename trouble"

# Report what we did.
# When run from cron, this means we get reports on updates (only).
echo Updated Firefox to version "$FFVERSION" 

######################################################################
# END OF FILE
######################################################################

