#!/bin/bash

# (C) 2009 Markus Bertheau
#
# see http://www.markusbe.com/2009/09/installing-flash-media-server-on-ubuntu-linux/

exit_error()
{
    cd $ORIGINAL_CWD
    rm -rf $TEMPDIR
    echo $*
    exit 1
}

exit_noerror()
{
    cd $ORIGINAL_CWD
    rm -rf $TEMPDIR
}

trap exit_error HUP INT QUIT PIPE TERM
trap exit_noerror EXIT

TEMPDIR=`mktemp -d /tmp/install-fms-ubuntu.XXXXXXXXXX` || exit_error "Couldn't create temporary directory in /tmp"

ORIGINAL_CWD=`pwd`
cd $TEMPDIR

FMS_FILENAME="FlashMediaServer3.5.tar.gz"
FMS_URL="http://download.macromedia.com/pub/flashmediaserver/updates/3_5_3/Linux/$FMS_FILENAME"
PATCH_FILENAME=flash-media-server-3.5.3-ubuntu.patch
PATCH_URL="http://www.markusbe.com/wp-content/uploads/2009/09/$PATCH_FILENAME"
GETLIBS_URL="http://www.markusbe.com/wp-content/uploads/2009/09/getlibs"

DISTRIBUTION=`lsb_release -i | cut -f2`

if [ x"$DISTRIBUTION" != xUbuntu ]; then
    exit_error "Unsupported linux distribution."
fi

UBUNTU_VERSION=`lsb_release -r | cut -f2`
ARCHITECTURE=`uname -m`

case x"$UBUNTU_VERSION" in
    x6.06* | x8.04* | x8.10* | x9.04* | x9.10* | x10.04*)
    ;;
    *)
        exit_error "Ubuntu $UBUNTU_VERSION is not supported."
    ;;
esac

case x"$ARCHITECTURE" in
    "xi686")
        ARCH_BIT="32"
    ;;
    "xx86_64" | "xia64")
        ARCH_BIT="64"
    ;;
    *)
        exit_error "Architecture $ARCH_BIT is not supported."
    ;;
esac

# determine package dependencies
DEPS="patch"

if [ x"$ARCH_BIT" = "x64" ]; then
    DEPS=$DEPS" ia32-libs"
fi

if [ x"$ARCH_BIT" = "x32" ]; then
    DEPS=$DEPS" libexpat1"
    if [    x"${UBUNTU_VERSION:0:4}" = "x6.06" \
         -o x"${UBUNTU_VERSION:0:4}" = "x8.04" \
         -o x"${UBUNTU_VERSION:0:4}" = "x8.10" \
         -o x"${UBUNTU_VERSION:0:4}" = "x9.04" ]; then
         DEPS=$DEPS" libcap1"
    else
        DEPS=$DEPS" libcap2"
    fi

    if [ x"${UBUNTU_VERSION:0:4}" = "x6.06" ]; then
        DEPS=$DEPS" libnspr4"
    else
        DEPS=$DEPS" libnspr4-0d"
    fi
fi

# determine dependencies for which no package exists (and getlibs has to be used)
GETLIBS_DEPS=""

if [ x"$ARCH_BIT" = "x64" -a x"${UBUNTU_VERSION:0:4}" = "x8.10" ]; then
    GETLIBS_DEPS="libuuid1"
fi

if [ x"$ARCH_BIT" = "x64" -a x"${UBUNTU_VERSION:0:4}" = "x8.04" ]; then
    GETLIBS_DEPS="libuuid1 libnspr4-0d"
fi

if [ x"$ARCH_BIT" = "x64" -a x"${UBUNTU_VERSION:0:4}" = "x6.06" ]; then
    GETLIBS_DEPS="libuuid1 libnspr4 libcap1"
fi

if [ x"$GETLIBS_DEPS" = x ]; then
    echo "Ubuntu $UBUNTU_VERSION $ARCH_BIT bit supported."
else
    echo "Ubuntu $UBUNTU_VERSION $ARCH_BIT bit supported, BUT:"
    echo "Files will be installed bypassing the package manager, which means no automatic updates for these files." | fmt -w 75
    read -p "Continue? [Y/n] "
    if [ x"$REPLY" = xn -o x"$REPLY" = xN ]; then
        exit_error
    fi
fi

if [ ! -f "$ORIGINAL_CWD"/$FMS_FILENAME ]; then
    echo "Downloading Flash Media Server..."
    wget $FMS_URL -O "$ORIGINAL_CWD"/$FMS_FILENAME || exit_error "Error downloading Flash Media Server"
else
    echo "Flash Media Server installation package found."
fi
ln -s "$ORIGINAL_CWD"/$FMS_FILENAME $FMS_FILENAME

# Ready to go!

# unpack archive
echo -n "Extracting archive $FMS_FILENAME..."
tar xfz $FMS_FILENAME || exit_error "Error while extracting archive."
echo "done."

# check version
if [ ! -e FMS_3_5_3_r824a ]; then
    FMS_VERSION=`ls -d FMS* | head -1 | sed -e 's/^FMS_\([0-9]\+\)_\([0-9]\+\)_\([0-9]\+\)_r\([0-9a-z]\+\).*$/\1.\2.\3-r\4/'`
    echo "Found Flash Media Server $FMS_VERSION."
    echo "This script has been tested with Flash Media Server 3.5.3-r824a only."
    read -p "Continue anyway? [Y/n] "
    if [ x"$REPLY" = xn -o x"$REPLY" = xN ]; then
        exit 1
    fi
fi

# install dependencies - apt-get

echo "Installing dependencies: $DEPS"
sudo apt-get install $DEPS || exit_error "Error while installing dependencies"

# install dependencies - getlibs

if [ x"$GETLIBS_DEPS" != x ]; then
    wget $GETLIBS_URL || exit_error "Error downloading getlibs"
    sudo bash ./getlibs -p $GETLIBS_DEPS
fi

# library compatibility links

if [ "$ARCH_BIT" = "64" ]; then
    LIBDIR=lib32
elif [ "$ARCH_BIT" = "32" ]; then
    LIBDIR=lib
fi

# expat compatibility link

if [ -e /usr/$LIBDIR/libexpat.so.1 ]; then
    sudo ln -sf libexpat.so.1 /usr/$LIBDIR/libexpat.so.0 || exit_error "Error creating libexpat compatibility link"
elif [ -e /$LIBDIR/libexpat.so.1 ]; then
    sudo ln -sf libexpat.so.1 /$LIBDIR/libexpat.so.0 || exit_error "Error creating libexpat compatibility link"
else
    exit_error "Can't find libexpat.so.0. Please report to author at markusbe.com."
fi

# libcap compatibility link

if [ -e /$LIBDIR/libcap.so.2 -a ! -e /$LIBDIR/libcap.so.1 ]; then
    sudo ln -sf libcap.so.2 /$LIBDIR/libcap.so.1 || exit_error "Error creating libcap compatibility link"
fi

# download patch

wget $PATCH_URL || exit_error "Error downloading patch"

# apply patch

FMS_DIR=`ls -d FMS* | head -1`
(cd $FMS_DIR; patch -p1 < ../$PATCH_FILENAME || exit_error "Error applying patch")

# start installer
(cd $FMS_DIR; sudo ./installFMS && (echo "If this script helped you, maybe it saved you time or money, and you want to"; echo "pay for it, have a look at http://markusbe.com/paying . Thanks :)"))

cd $ORIGINAL_CWD
exit 0
