#!/bin/bash

#==============================================================================
# Configuration
#==============================================================================

LAMP=false
RPMREBUILD=false
NVIDIA=false
NX=false  # NX broken these days; VNC a right pain re users; X2Go excellent
BBB=false  # BigBlueButton

#==============================================================================
# Constants, functions
#==============================================================================

# . /etc/lsb-release # sets DISTRIB_* environment variables, including DISTRIB_CODENAME
DISTRIB_CODENAME=`lsb_release -sc` # ... another way

PACKAGES=""

addkey()
{
    echo "addkey: $1"
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $@
}

addurlkey()
{
    echo "addurlkey: $1"
    wget -q $1 -O- | sudo apt-key add -
}

addrepo()
{
    echo "addrepo: \"$1\""
    # sudo add-apt-repository --remove --yes "$1"
    sudo add-apt-repository --yes "$1"
}

add()
{
    PACKAGES="$PACKAGES $@"
}

#==============================================================================
# Certificates
#==============================================================================

# sudo apt-get install --reinstall ca-certificates

#==============================================================================
# Repositories
#==============================================================================

# Inkscape development
addkey B9A06DE3
sudo add-apt-repository -s "deb http://ppa.launchpad.net/inkscape.dev/trunk/ubuntu ${DISTRIB_CODENAME} main"
# -s adds the "deb-src" source repository as well
# see: http://askubuntu.com/questions/160511/why-does-add-apt-repository-fail-to-add-source-repositories

# Komodo Edit
addrepo ppa:mystic-mirage/komodo-edit

# LibreOffice
addrepo ppa:libreoffice/ppa

# node.js
addrepo ppa:chris-lea/node.js

# Pipelight
addrepo ppa:pipelight/stable

# R
addkey E084DAB9
addrepo "deb http://cran.ma.imperial.ac.uk/bin/linux/ubuntu ${DISTRIB_CODENAME}/"

# Skype
addrepo "deb http://archive.canonical.com/ ${DISTRIB_CODENAME} partner"

# Spotify - see https://www.spotify.com/uk/download/previews/ and http://www.ubuntuupdates.org/ppa/spotify
addkey 94558F59
SPOTIFY_REPOSITORY_LINE="deb http://repository.spotify.com stable non-free"
SPOTIFY_FILE=/etc/apt/sources.list.d/spotify.list
grep "$SPOTIFY_REPOSITORY_LINE" $SPOTIFY_FILE || sudo sh -c "echo '$SPOTIFY_REPOSITORY_LINE' >> $SPOTIFY_FILE"

# VirtualBox
addurlkey http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
addrepo "deb http://download.virtualbox.org/virtualbox/debian ${DISTRIB_CODENAME} contrib"

# Wine
addrepo ppa:ubuntu-wine/ppa

# X2Go
addrepo ppa:x2go/stable

# NX
if $NX; then
    addrepo ppa:freenx-team
    sudo sed -i "s/${DISTRIB_CODENAME}/precise/g" /etc/apt/sources.list.d/freenx-team-ppa-${DISTRIB_CODENAME}.list
fi

# BBB
if $BBB; then
    # BBB needs a dedicated machine with port 80 not being used for anything else...
    # https://code.google.com/p/bigbluebutton/wiki/InstallationUbuntu#Before_You_Install
    # https://code.google.com/p/bigbluebutton/wiki/FAQ#What_are_the_minimum_hardware_requirements_for_the_BigBlueButton
    # https://groups.google.com/forum/#!topic/bigbluebutton-users/a1l9s54cE2g
    # https://code.google.com/p/bigbluebutton/wiki/InstallationUbuntu
    # http://www.bigbluebutton.org/

    # addurlkey http://ubuntu.bigbluebutton.org/bigbluebutton.asc
    # addrepo "deb http://ubuntu.bigbluebutton.org/lucid_dev_081/ bigbluebutton-lucid main"
    # addrepo "deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse"
fi

#==============================================================================
# Packages
#==============================================================================

# Hardware:
add grub-pc
add unetbootin
add mtpfs mtp-tools powertop # mtp* = mount Android phones via USB
add mdadm # RAID
# add linux-firmware # Intel Wi-Fi drivers
# add hplip-gui # HP printers
add apcupsd # APC UPS
add lm-sensors # CPU temperature, etc. "sensors" command

# System:
add ppa-purge

# Network:
add alpine autossh cups-pdf dos2unix ethtool gftp icedtea-7-plugin \
    initramfs-tools \
    libcrypt-ssleay-perl libdvdread4 libio-socket-ssl-perl libnet-ssleay-perl \
    libssl-dev \
    mail-notification nmap ntp openjdk-7-jre putty-tools proxytunnel \
    rsnapshot sendemail \
    smartmontools ssh sshfs testdisk thunderbird unison x2goclient xinetd
add krdc
# VPNs:
add network-manager-vpnc openconnect vpnc

# Thunderbird add-ons:
add xul-ext-lightning
# MANUAL: Nostalgy, Provider for Google Calendar, Google Contacts

# GNOME desktop:
add human-theme ubuntu-restricted-extras ubuntu-desktop

# XFCE:
add xubuntu-desktop # XFCE, LightDM

add origami # Spare CPU
add virtualbox # prior to Ubuntu 14.04, installed version 4.2; now installs 4.3
add wine

# Programming
add astyle build-essential cmake cppcheck libboost-dev meld
add perl-doc uncrustify subversion tidy
#add python3-pyside pyside-tools

# Science
add octave octave-doc octave-htmldoc octave-statistics octave-plot octave-symbolic
add r-recommended
add maxima wxmaxima xmaxima

# R extras
add libblas-dev libcairo2-dev libdbd-mysql libjpeg62 liblapack-dev liblzma-dev libmysqlclient-dev libxt-dev
add r-cran-rcpp
add unixodbc-dev

# Debian package development
add alien autoconf automake autotools-dev debhelper dh-make devscripts fakeroot file gdebi gfortran git gnupg lintian patch patchutils pbuilder perl python rpm quilt xutils-dev

# Graphics/office
add dia feh gthumb gscan2pdf jhead libreoffice lyx msttcorefonts okular openjdk-7-jdk pdfjam pdftk
# add inkscape
add inkscape-trunk # development version
add librsvg2-bin # for rsvg-convert
add guvcview # photos from webcams, etc.

# Database
add libmyodbc mdbtools mdbtools-gmdb mysql-client mysql-workbench python-mysqldb python-pyodbc unixodbc

# Media
add audacity
add pipelight-multi

# Music
add lilypond frescobaldi

# NodeJS
add python-software-properties python g++ make nodejs

# Komodo
add komodo-edit komodo-edit-globalmenu

# Others
add spotify-client
add skype
add x2goserver

# OPTIONAL
if $NVIDIA; then
    add nvidia-current nvidia-settings # proprietary NVidia drivers (if disabled, you'll get Nouveau)
    # add bumblebee-nvidia # Optimus for Linux/NVidia
fi

# LAMP
if $LAMP; then
    add apache2 libapache2-mod-perl2 libapache2-mod-wsgi
    add geoip-bin libgeo-ip-perl
    add libpcre3-dev
    add mysql-server
    add gitweb
    add libapache2-svn websvn
    add apache2-utils  # for htpasswd
    # ... apache2-src outdated (Ubuntu 14.04)
fi

# NX
if $NX; then
    add freenx-server
fi

# BBB
if $BBB; then
    # add python-software-properties
    # add libreoffice-common libreoffice
    # add ruby
    # add gem
    # add bigbluebutton
fi

#==============================================================================
# Done adding
#==============================================================================

#echo $PACKAGES
#exit 0

#==============================================================================
# Install
#==============================================================================

sudo apt-get update
sudo apt-get install $PACKAGES

#==============================================================================
# Apache extra
#==============================================================================
if $LAMP; then
    a2enmod cgi dav dav_fs dav_svn headers include proxy proxy_connect ssl userdir
fi

#==============================================================================
# Other extra
#==============================================================================
# rpmrebuild
if $RPMREBUILD; then
    RPMREBUILDFILE=rpmrebuild-2.11-1.noarch.rpm
    wget http://downloads.sourceforge.net/project/rpmrebuild/rpmrebuild/2.11/$RPMREBUILDFILE
    sudo alien --to-deb $RPMREBUILDFILE
    sudo dpkg --install rpmrebuild*.deb
fi

#if $NX; then
    # WARNING: NX 4 is different in the way it handles desktops (cf. NX 3)
    # https://www.nomachine.com/download/download&id=6
    #NXFILE=nomachine_4.2.21_1_amd64.deb
    #wget http://web12.nomachine.com/download/4.2/Linux/$NXFILE
    #sudo dpkg --install $NXFILE
    # ... package is "nomachine"
#fi

echo "========================================================================"
echo "To enable Silverlight:"
echo "    sudo pipelight-plugin --enable silverlight"
echo "Test at:"
echo "    http://bubblemark.com/silverlight2.html"
echo "========================================================================"
