#!/bin/bash
#
# Install script for Debian unstable from deian stable
#
# adapt source.list for unstable
#mv /etc/apt/sources.list /etc/apt/sources.list.old
#cp files/sources.list /etc/apt/sources.list
source lib/messages.sh
source lib/download.sh

#init default variables
source conf/init.sh

#Variables override
for i in $(ls conf/*.conf)
do
    source $i
done

erase_display_char () {
    let "i=0"
    while [  $i -lt $1 ]; do
        message "\b \b"
        let "i=$i + 1"
    done
}

install () {
    nchar=0
    message "\nInstall $1 : "
    LC_ALL=C apt-get install --no-install-recommends -y $1 2>/dev/null | while read x; do
	display=""
	pkg_name=""
        case $x in
            Get*)
                pkg_name=$(echo $x | awk '{ printf $5 }')
                display="downloading"
            ;;
            Unpack*)
                pkg_name=$(echo $x | awk '{ printf $2 }')
                display="Unpacking"
            ;;
            *already*newest*)
                pkg_name=$(echo $x | awk '{ printf $1 }')
                display="Already Installed"
            ;;
        esac
	if [ ! -z "$display" ]
        then
            display="$display $pkg_name"
            erase_display_char $nchar
            message "$display" "info"
            nchar=${#display}
        fi 
    done
    message " done" "ok"

}

process_package_file (){
    for p in `cat $1 | grep  '^[^#].*'`
    do
        install $p
    done
}

create_fusioninv_conf () {
    # Process Config file
    message "\nProcess Fusion inventory configuration file : "
    #remove comments
    sed -i '/^#/ d' ${FI_CONF_FILE}
    sed -i '/^$/ d' ${FI_CONF_FILE}

    # put server
    sed -i "1iserver = ${FI_SERVER}" ${FI_CONF_FILE}

    # put username and password for proxy (htaccess)
    sed -Ei "s/^(user\ =).*/\1 ${FI_PROXY_USER}/g" ${FI_CONF_FILE}
    sed -Ei "s/^(password\ =).*/\1 ${FI_PROXY_PASS}/g" ${FI_CONF_FILE}
    #sed -Ei "s/^(tag\ =).*/\1 ${centre}/g" ${FI_CONF_FILE}
    message "done" "ok"
}

configure_drm(){
    if [ -z "$(grep -o drm $INITRAMFS_CONF_FILE)" ] 
    then
        cat "drm" >> $INITRAMFS_CONF_FILE
    else
        message "DRM already done " "warn"
    fi
    if [ -z "$(grep -o $1 $INITRAMFS_CONF_FILE)" ]
    then
        cat "$1 modeset=1" >> $INITRAMFS_CONF_FILE
    else
        message "$1 already done " "warn"
    fi
}

configure_grub (){
    sed -E "s/^GRUB_CMDLINE_LINUX_DEFAULT=\"(.*)\"/GRUB_CMDLINE_LINUX_DEFAULT=\"\1 spash\"/g" ${GRUB_DEFAULT_CONF_FILE} >/dev/null
    update-grub2 2>/dev/null
}

configure_plymouth (){
    local module=$(lsmod | grep -o "i915\|radeon" | head -1) 
    if [ ! "$module" == "" ]
    then
        message "\nConfigure drm modules for $module : "
        configure_drm $module
        message "done" "ok"
        message "\nConfigure grub command line : "
        local splash_enable=$(sed -rn 's/GRUB_CMDLINE_LINUX_DEFAULT=\"(.*)\"/\1/p' ${GRUB_DEFAULT_CONF_FILE} | grep -o "splash" )
        [[ -z $splash_enable ]] && configure_grub || message "Grub splash kernel option exist " "warn"
        message "done" "ok"
        message "\nSelect plymouth theme : "
        plymouth-set-default-theme -R spinner &>/dev/null
        message "done" "ok"
        message "\n" 
    else
        message "\nNo suitable kernel module found for plymouth\n" "warn"
    fi
}

message "\nAdd Debian stretch backport and Sid Repository :"
cp ./files/apt/*.list /etc/apt/sources.list.d/
cp ./files/apt/20-stable /etc/apt/preferences.d/
cp ./files/apt/60-* /etc/apt/preferences.d/
cp ./files/apt/80-* /etc/apt/preferences.d/
message "done\n" "ok"


apt-get update &>/dev/null
process_package_file files/packages.list

message "Activate unattended upgrades : "
cp ./files/apt/20auto-upgrades /etc/apt/apt.conf.d/
cp ./files/apt/50unattended-upgrades /etc/apt/apt.conf.d/
message "done" "ok"

message "\ninstall custom background images : "
mkdir -p /usr/share/backgrounds/acaqb/
cp -R ./files/backgrounds/*  /usr/share/backgrounds/acaqb/
message "done\n" "ok"

configure_plymouth

# Login manager config
message "\nConfiguring lightdm : "
cp files/lightdm.conf /etc/lightdm/ &> /dev/null
cp files/lightdm-gtk-greeter.conf /etc/lightdm/ 
message "OK" "ok"

message "\nCreate users default parameters : "
cp -R files/skel/ /etc/ &> /dev/null
message "OK" "ok"

#remove interface file until NM use network
rm -rf /etc/network/interfaces

#add elementary theme
#mkdir -p /tmp/themes/
#cd /tmp/themes/

#download master.zip https://github.com/shimmerproject/elementary-xfce/archive/
#unzip master.zip > /dev/null
#mv elementary-xfce-master/* /usr/share/icons/
#rm -rf *
