#!/bin/bash 

# Window To The Stars installer for Linux Ubuntu, Mint etc.
#
# version 0.01 13/06/2014
# http://www.ast.cam.ac.uk/~rgi/window.html
#
# version 0.02 10/11/2014
# add Arch linux support (special thanks to Saeed Sarpas)
#
# version 0.03 20/04/2015
# move to Cambridge

# first install the packages we know we will need

# check for apt-get or yum or zypper or pacman

if [ -n "$(command -v apt-get)" ]; then

    echo "Using apt-get for install (debian/ubuntu/mint-like system)"

    # on ubuntu-like machines, everything is just there... so simple!
    sudo apt-get install gnuplot gfortran perl cpanminus cmake wget ghostscript libav-tools libgtk2-perl libcairo-perl libglib-perl liblocal-lib-perl pkg-config

elif [ -n "$(command -v yum)" ]; then

    echo "Using yum (fedora-like system)"

    # fedora : requires rpmfusion for ffmpeg 

    # first install rpmfusion packages
    sudo yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm

    # then get packages
    sudo yum install gnuplot gcc-gfortran perl cpanminus cmake wget ghostscript ffmpeg perl-Gtk3 perl-Cairo perl-Glib perl-local-lib pkgconfig

elif [ -n "$(command -v zypper)" ]; then

    # open suse : tested on 13.1 but should work on earlier versions

    # get release data : I hope one of these two works!
    source /etc/os-release 2>/dev/null
    source /etc/SuSE-release 2>/dev/null

    # VERSION_ID is defined in 13.1+ ... otherwise use VERSION
    if [ -z $VERSION_ID ] ; then
	VERSION_ID=$VERSION
    fi

    echo "Using zypper (opensuse-like system $PRETTY_NAME; $VERSION_ID)"

    # basic tools
    sudo zypper install curl cmake wget perl make

    # we need cpanminus, but there is no package, so install manually
    sudo curl -L http://cpanmin.us | perl - --sudo App::cpanminus

    # now the guts
    sudo zypper install gnuplot gcc-fortran ghostscript perl-Gtk3 perl-Cairo perl-Glib perl-local-lib pkg-config
    
    # try to install ffmpeg from packman repos
    sudo zypper addrepo -f -n packman-multimedia http://packman.inode.at/suse/openSUSE_$VERSION_ID/Multimedia/ packman-multimedia
    
    sudo zypper addrepo -f -n packman-essentials http://packman.inode.at/suse/openSUSE_$VERSION_ID/Essentials/ packman-essentials

    sudo zypper install ffmpeg
    
elif [ -n "$(command -v pacman)" ]; then

    echo "Using pacman (arch linux)"

    sudo pacman -S gnuplot gcc-fortran perl cpanminus cmake wget ghostscript gtk3-perl cairo-perl glib-perl perl-local-lib pkg-config

    if [ -z "$(pacman -Q libav)" ]; then
      sudo pacman -S ffmpeg
    fi

else 

    echo "Not sure what to use: yum or apt-get or zypper or pacman? Perhaps you're running a very obscure linux... or a mac (ha) in which case you will have to hack this script to make it work :)"
    exit

fi


# check for error

if [ "$?" != "0" ]; then
    # oops there was a problem
    echo "There was an error installing packages : please check the error message, try to fix it and report to Rob if there's something he should do about it. http://www.ast.cam.ac.uk/~rgi/window.html"
    exit
fi
  
# get the WTTS installer script
wget -O installer http://www.ast.cam.ac.uk/~rgi/code/wtts/installer

if [ "$?" != "0" ]; then
    echo "Failed to download the Window To The Stars installer from wget http://www.ast.cam.ac.uk/~rgi/code/wtts/installer : check your internet connection and check that the www.ast.cam.ac.uk website is alive"
    exit
fi

# make it executable
chmod +x installer

if [ "$?" != "0" ]; then
    echo "Failed to make the installer script executable. If it downloaded, this is odd and I don't know what to do."
    exit
fi

# Now we need a manager for the perl modules.
# If perlbrew is available, use it, otherwise try local::lib 
if command -v perlbrew >/dev/null; then 
    echo "Use perlbrew as Perl module manager"
else
    echo "Use local::lib as Perl module manager"

    # install local::lib for perl
    cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
    export PERL5LIB=~/perl5/lib/perl5:$PERL5LIB  
fi
echo "PERL5LIB is now $PERL5LIB"
    
# run it for a local install
./installer local
if [ "$?" != "0" ]; then
    echo "Installer script failed in some way. Please see error messages above for clues"
    exit
fi

# put ~/bin into the bashrc PATH
echo "export PATH=\$PATH:~/bin" >> ~/.bashrc
echo "export PERL5LIB=~/perl5/lib/perl5:\$PERL5LIB" >> ~/.bashrc

if [ "$?" != "0" ]; then
    echo "Failed to put ~/bin into your .bashrc's path. Perhaps you are using a different shell? In any case, you need ~/bin in your path to run the wtts command automatically. $?"
    exit
fi

# resource bashrc to force ~/bin
source ~/.bashrc

# export PATH and PERL5LIB
export PATH
export PERL5LIB

