#!/bin/bash
# Installation script for "Oracle Java JRE"
# Checks bit width and installs related package, 32 or 64 bits 
# Author: ajaaskel(at)forum.ubuntu-fi.org temp001(at)pp.inet.fi
#
# v1.0  2012-12-06 JRE 7u9 
# v1.01 2012-12-18 JRE 7u10
# v1.02 2012-12-20 Debian "update-alternatives": plugin, javaws, jexec
# v1.03 2013-01-14 JRE 7u11
# v1.04 2013-01-14 "priority" variable
# v1.05 2013-02-06 JRE 7u13
# v1.06 2013-03-15 JRE 7u17
# v1.07 2013-04-18 JRE 7u21
# v1.08 2013-04-18 Remove old files at /tmp before downloading
# v1.09 2013-06-19 JRE 7u25, backup of /usr/lib/jvm --> .old
# v1.10 2013-10-30 JRE 7u45
# v1.11 2013-12-01 download path for 64 bits fixed
# v1.12 2014-01-19 JRE 7u51
# v1.13 2014-06-22 JRE 7u60
########################################################################

NEW_32="AutoDL?BundleId=90214"
NEW_64="AutoDL?BundleId=90216"
priority=10060
DL_HTTP="http://javadl.sun.com/webapps/download"

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

GET_BITS()
#Entry: none, return: BIT_AMOUNT
{
BITS=`uname -m` 
case $BITS in
i686 )
  BIT_AMOUNT='32 bits';;
x86_64 )
  BIT_AMOUNT='64 bits';;
* )
  BIT_AMOUNT='Unknown';;
esac
}

GET_DESKTOP()
#Detect desktop
{
if [ -n "$(pgrep gnome-session)" ] ; then
  DESKTOP="gnome"
elif [ -n "$(pgrep ksmserver)" ] ; then
  DESKTOP="kde"
elif [ -n "$(pgrep xfce-msc-manage)" ] ; then
  DESKTOP="xfce"
else
DESKTOP="unknown"
fi
}

GET_SUDO()
#Which graphical sudo to use ?
{
GET_DESKTOP
if [ "$DESKTOP" = "gnome" ] ; then
_SUDO="gksudo"
elif [ "$DESKTOP" = "kde" ] ; then
_SUDO="kdesudo"
else
_SUDO="sudo"
fi
}

java_inst()
{
dest_dir="/usr/lib/jvm"
bak_dir="/usr/lib/jvm.old"
sudo mkdir -p $bak_dir; sudo cp -aR "$dest_dir/"* "$bak_dir"  #Just for quick fall back
sudo apt-get -y purge icedtea*
dir_name_1="${gz_name/\.tar*/}"    # jre-7u9-linux-i586
sudo mkdir -p "$dest_dir/$dir_name_1"  
sudo tar -zxvf /tmp/"$gz_name" -C "$dest_dir/$dir_name_1" &>/tmp/tar.log
a=$(tar --exclude="*/*" -tf "/tmp/$gz_name")  # jre1.7.0_09/
dir_name_2=${a/\/}   # jre1.7.0_09
j_inst_dir="$dest_dir/$dir_name_1/$dir_name_2"  # Oracle: "Java installation directory"
java_bin="$j_inst_dir/bin/java"
echo
echo "Java installation directory=$j_inst_dir"
sudo update-alternatives --install /usr/bin/java java "$java_bin" "$priority"
sudo ldconfig
java -version
echo
}

plugin_inst() 
# Uses variables defined by "java_inst"
{
plugins_dir="/usr/lib/mozilla/plugins"
plugin_pathfile=$(find "$j_inst_dir" -name libnpjp2.so)
if [ -z "$plugin_pathfile" ] ; then
echo "Error: Plugin binary file not found." 
exit 1
fi
echo "Java plugin binary=$plugin_pathfile"
echo "Java plugin link directory=$plugins_dir"
sudo update-alternatives --install "$plugins_dir/libnpjp2.so" mozilla-javaplugin "$plugin_pathfile" "$priority"
echo
}

jexec_inst()
{
jexec_dir=$j_inst_dir/lib
sudo update-alternatives --install /usr/bin/jexec jexec "$jexec_dir/jexec" "$priority"
echo "jexec=$jexec_dir/jexec" 
echo
}

javaws_inst()
{
javaws_dir=$j_inst_dir/bin
sudo update-alternatives --install /usr/bin/javaws javaws "$javaws_dir/javaws" "$priority"
echo "javaws=$javaws_dir/javaws" 
echo
}


cd ~
GET_BITS
GET_SUDO
$_SUDO "Oracle Java JRE asennus"
sudo rm /tmp/jre* /tmp/tar.log
echo "Wait patiently for downloading --- Odota kärsivällisesti latausta"
if [ "$BIT_AMOUNT" = "32 bits" ] ; then
    echo "32 bits o/s detected."
    response=$(wget --spider "$DL_HTTP/$NEW_32" 2>&1)
    gz_name=$(echo $response | tr '&' '\n' | grep -i "File=" | head -n1 | sed 's/.*=//g')
    wget -r -O/tmp/$gz_name "$DL_HTTP/$NEW_32"
elif [ "$BIT_AMOUNT" = "64 bits" ] ; then
    echo "64 bits o/s detected."
    response=$(wget --spider "$DL_HTTP/$NEW_64" 2>&1)
    gz_name=$(echo $response | tr '&' '\n' | grep -i "File=" | head -n1 | sed 's/.*=//g')
    wget -r -O/tmp/$gz_name "$DL_HTTP/$NEW_64"
else
    echo "*** Error: Cannot detect bit width --- application not compatible with this o/s. ***"
    exit 1
fi
if [ "$?" != "0" ]; then
	echo "*** Downloading failed ***"
	exit 1
fi
java_inst
plugin_inst
jexec_inst
javaws_inst


