#!/bin/bash

dotfiles=(vimrc
          zshrc
          pbuilderrc
          quiltrc
          dput.cf
          conkyrc
          gitconfig
          pastebinit.xml
          xchat2/xchat.conf xchat2/servlist_.conf xchat2/notify.conf xchat2/chanserv-mod.py
          ssh/config
          mutt/muttrc mutt/mutt.sh mutt/signature mutt/colors mutt/gpg irssi/config
          )
files=(pbuilder/hooks
       pbuilder/scripts
       )
emptydotdirs=()
emptydirs=(pbuilder/result)
repos=(google-talkplugin.list)
reposdeb=()
packages=(geany
          google-talkplugin
          nautilus-dropbox
          filezilla
          conky
          skype
          ubuntu-restricted-exstras
          zram-config
          samba
          gimp
          inkscape
          clementine
          vlc
          calibre
          gdebi
          gparted
          playonlinux
          pass
          pastebinit
          virtualbox linux-headers-generic virtualbox-guest-addition-iso
          terminator
          unity-tweak-tool
          vim
          mutt
          irssi
          flashplugin-installer
          prelink preload
          steam
          cheese
          chromium-browser
          xchat
          zsync
          eatmydata #to use pbuilder quickly
          build-essential
          debhelper dh-make dh-systemd
          git git-buildpackage
          subversion
          ubuntu-dev-tools
          pbuilder
          lintian
          cdbs
          gnupg2 gnupg-agent
          )

personal () {
    sublime () {
        if ( ! dpkg -l sublime-text &> /dev/null ); then
        echo "Installing sublime-text..."
        sudo dpkg -i sublime-text*_`dpkg --print-architecture`.deb
        sudo apt-get -f install
        fi
    }
    prelink () {
        if ( dpkg -l prelink &> /dev/null );then
            NEW=false
            if [ -f /etc/default/prelink ] ; then
                if ( ! grep "PRELINKING=yes" /etc/default/prelink &> /dev/null ); then
                    TEMPFILE=`mktemp`
                    sed s/PRELINKING=unknown/PRELINKING=yes/g /etc/default/prelink > $TEMPFILE
                    sudo cp $TEMPFILE /etc/default/prelink
                    rm $TEMPFILE
                    NEW=true
                fi
            fi
            if [ -f /etc/prelink.conf ] ; then
                if ( ! grep "/usr/bin/skype" /etc/prelink.conf &> /dev/null ); then
                    sudo sh -c 'echo "-b /usr/bin/skype" >> /etc/prelink.conf'
                    NEW=true
                fi
            fi
            if $NEW ; then
                sudo prelink -a &
            fi
        fi
    }
    musiclink () {
        if [ -d $HOME/Music && -e /media/Dati/Music ] ; then
            echo "Creating $HOME/Music symlink"
            rmdir $HOME/Music
            ln -s /media/Dati/Music $HOME/Music
        fi
    }

    sublime
    prelink
    musiclink
}

# This function create files, directories and symlinks, as needed
files () {
    echo "Creating empty dirs..."
    for dir in ${emptydotdirs[@]}; do
        if [ ! -a $HOME/.${dir} ] ; then
            mkdir -pv $HOME/.${dir}
        fi
    done
    for dir in ${emptydirs[@]}; do
        if [ ! -a $HOME/${dir} ] ; then
            mkdir -pv $HOME/${dir}
        fi
    done

    echo "Creating symlinks..."
    for file in ${dotfiles[@]}; do
        # for files
        if [ -f $HOME/settings/${file} ] ; then
            if [ -a $HOME/.${file} ] ; then
                rm $HOME/.${file}
            fi
            if (echo ${file} | grep -q '/' && [ ! -d $HOME/.$(echo ${file} | cut -f 1 -d '/') ]) ; then
                mkdir $HOME/.$(echo ${file} | cut -f 1 -d '/')
            fi
            ln -s $HOME/settings/${file} $HOME/.${file}
        fi
        # for directories
        if [ -d $HOME/settings/${file} ] ; then
            #file=$(echo $file | cut -f 1 -d '/')
            if [ -a $HOME/.${file} ] ; then
                rm -r $HOME/.{file}
            fi
            ln -s $HOME/settings/${file} $HOME/.${file}
        fi
    done
    for file in ${files[@]}; do
        #for files
        if [ -f $HOME/settings/${file} ] ; then
            if [ -a $HOME/${file} ] ; then
                rm $HOME/${file}
            fi
            ln -s $HOME/settings/${file} $HOME/${file}
        fi
        # for directories
        if [ -d $HOME/settings/${file} ] ; then
            if [ -a $HOME/${file} ] ; then
                rm -r $HOME/${file}
            fi
            ln -s $HOME/settings/${file} $HOME/${file}
        fi
    done
}

## add repositories, as needed
#add-repo () {
#    echo "alpha feature! not tested! it can kill you, your mother and your cat!"
#    echo "do you still want to run this script? [y/N]"
#    read reply
#    if [ ${reply} != 'y' !! ${reply} != 'Y' ]
#        then
#            echo "you made the better choice, brother!"
#            exit 2
#    echo "Adding required repositories..."
#    for repo in ${repos[@]}; do
#        tmp=`grep '^deb' ${repo}|tail -n 1|awk {'print $2'}`
#        echo repo: $tmp
#        dir="/etc/apt/sources.list.d/*"
#        if $( grep -v '${tmp}' ${dir} )
#            then
#                echo "Adding \"${repo}\" repository"
#                sudo cp ${repo} /etc/apt/sources.list.d/
#        fi
#    done
#    for repo in ${reposdeb[@]}; do
#        if [ ! $(grep -r ${repo} /etc/apt/sources.list*) ]
#            then
#                echo "Adding \"${repo}\" repository"
#                sudo add-apt-repository --yes ${repo}
#        fi
#    done
#}

## install the required packages, as needed
install () {
    echo "Updating repository information..."
    sudo apt-get -q update
    echo "Installing packages..."
    package=$(echo ${packages[@]} | tr '\n' ' ')
    sudo apt-get install -y ${package}
}

dropboxlinks () {
    echo "Creating link to Dropbox folder"
    DIRShome=(Documents Pictures Public Templates)
    DIRSrootdb=(Develop FCM)

    for dir in ${DIRShome[@]}; do
        if ( [ -d $HOME/${dir} ] || [ ! -e $HOME/${dir} ] ) && [ -d $HOME/Dropbox/home/${dir} ] ; then
            if [ -e $HOME/${dir} ] ; then rmdir $HOME/${dir} ; fi
            ln -s $HOME/Dropbox/home/${dir} $HOME/${dir}
        fi
    done
    for dir in ${DIRSrootdb[@]}; do
        if ( [ -d $HOME/${dir} ] || [ ! -e $HOME/${dir} ] ) && [ -d $HOME/Dropbox/${dir} ] ; then
            if [ -d $HOME/${dir} ] ; then rmdir $HOME/${dir} ; fi
            ln -s $HOME/Dropbox/${dir} $HOME/${dir}
        fi
    done
}

help () {
    cat << EOF
    Valid options are:
     -a                 Add repositories
     -i                 First-installation useful stuff (apt-get update/upgrade)
     -h                 Show this help
EOF
}

while getopts ":ai" opt; do
  case $opt in
    a)
      #ADDREPO=1
      echo "The add-repo function is broken. Going on..."
      ;;
    i)
      echo "Running first-installation stuff..."
      sudo apt-get update
      sudo apt-get dist-upgrade -y
      ;;
    h)
      help
      ;;
    *)
      echo "Unrecognised option: -$OPTARG."
      help
      exit 1
      ;;
  esac
done

files
#if (ADDREPO==1); then add-repo ; fi
install
personal

exit 0
