#!/bin/bash
# Installation script for "yle-dl"
# Checks bit width and installs related package, 32 or 64 bits 
# Author: ajaaskel(at)forum.ubuntu-fi.org temp001(at)pp.inet.fi
#
# Change log:
# V1.15 2012-05-23  1) New YLE Areena .deb files 2) Uninstall of old version 3) ldconfig
# V1.16 2012-05-24 "apt-get purge" instead of "apt-get remove"
# V1.17 2012-05-25 New version of rtmpdump-yle
# V1.18 2012-06-06 New version of rtmpdump-yle
# V1.19 2012-06-07 Dependency on python-crypto added
# V1.20 2012-06-21 Both new version and new package name "yle-dl", "rtmpdump" re-build to avoid old dependencies  
# V1.21 2012-12-07 yle-dl V2.0.2 
# V1.22 2012-12-07 Remove previous yle-dl packages at current dir 
# V1.23 2013-05-22 yle-dl V2.1.0
# V1.24 2014-04-24 yle-dl V2.2.1
# V1.25 2015-02-27 yle-dl V2.5.1
# V1.26 2015-03-17 yle-dl V2.6.0
# V1.27 2015-03-23 single package for 64 & 32 bits
# V1.28 2015-05-04 yle-dl v2.7.0
# V1.29 2016-11-06 yle-dl v2.12.0
# v2.00 2015-11-07 different package for prior/post Ubuntu 16.04, codebase detection included
#**********************************************************************************************

DL_HTTP="http://www.homelinuxpc.com/download"

#***********************************************

DETECT_CODEBASE()
#For easy comparison, Ubuntu v16.04=1604, etc.
#Will still return the same way _Ubuntu_ codebase when used with Mint or any Ubuntu based distro..   
{
ubuntu_codebase=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//;s/"//g;s/[.]//')
}


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
}

cd ~
GET_SUDO
DETECT_CODEBASE

if [ "$ubuntu_codebase" -ge "1600" ]; then 
echo "Tunnistettu koodipohja $ubuntu_codebase on Ubuntu 16.04 tai uudempi"
NEW_ALL="yle-dl_2.12.0-2_all_16.deb"
else
echo "Tunnistettu koodipohja $ubuntu_codebase on vanhempi, kuin Ubuntu 16.04"
NEW_ALL="yle-dl_2.12.0-2_all.deb"
fi

rm  ./yle-dl_* &>/dev/null
wget -r -O"$NEW_ALL" "$DL_HTTP/$NEW_ALL"
$_SUDO "YLE Areenan latauksen asennus"
sudo apt-get -y purge rtmpdump-yle &>/dev/null
sudo apt-get install gdebi-core
sudo gdebi -n ./$NEW_ALL
sudo ldconfig





