#!/usr/bin/env bash
# 
# SCRIPT NAME:
# install
#
# AUTHOR:
# Severin Kaderli <severin@kaderli.dev>
#
# DESCRIPTION:
# This is the main installation script for my dotfiles. It setups the symlinks
# to the neeeded files, creates new directories, enables systemd services,
# installs pacman packages and gives out the correct permissions to files.
#
# USAGE:
# ./install
. ./system/.config/custom/env
. ./system/.local/bin/utils

###############################################################################
# Configuration variables                                                     #
###############################################################################
HOST="$(hostname)"

# Directories which should be created
DIRECTORIES=(
    ".local/log"
    "Build"
    "Downloads"
    "Keys"
    "Music"
    "Pictures"
    "Projects"
    "Tasks"
    "Videos"
)

# Files which should be symlinked in the home folder
LINKED_FILES_HOME=(
    ".config/autokey"
    ".config/bat"
    ".config/cmus"
    ".config/compton"
    ".config/cron"
    ".config/custom"
    ".config/dconf"
    ".config/dunst"
    ".config/git"
    ".config/gtk-2.0"
    ".config/gtk-3.0"
    ".config/httpie"
    ".config/i3"
    ".config/mpv"
    ".config/newsboat"
    ".config/npm"
    ".config/polybar"
    ".config/python"
    ".config/redshift"
    ".config/sqlite3"
    ".config/streamlink"
    ".config/task"
    ".config/termite"
    ".config/Trolltech.conf"
    ".config/user-dirs.dirs"
    ".config/vim"
    ".config/vue"
    ".config/wget"
    ".config/X11"
    ".config/zathura"
    ".config/zsh"
    ".local/bin"
    ".local/share/applications/vim.desktop"
    ".local/share/gnupg/gpg-agent.conf"
)

# Symlinks which will be created
declare -A LINKED_FILES
LINKED_FILES=(
    ["${HOME}/Music"]="${HOME}/Documents/Media/Music"
    ["${HOME}/Pictures"]="${HOME}/Documents/Media/Pictures"
)

# Permissions to set for folders and files
declare -A PERMISSIONS
PERMISSIONS=(
    ["${XDG_BIN_HOME}"]="744"
)

# Groups the user should be added to
ADD_GROUPS=(
    "bumblebee"
)

# Array of systemd services which should be enabled
SYSTEMD_SERVICES=(
    "cronie"
    "NetworkManager"
    "org.cups.cupsd"
    "suspend"
    "xboxdrv"
)

###############################################################################
# Install package manager and packages                                        #
###############################################################################
print_section "Installing requirements"
sudo pacman -S git base-devel --noconfirm --needed

print_section "Installing yay"
is_yay_installed=$(command -v yay)

# Install yay if not already installed
if [ -z "${is_yay_installed}" ]; then
    git clone https://aur.archlinux.org/yay.git
    (
    cd yay || exit
    makepkg -si --noconfirm --skippgpcheck
    )
    rm -rf yay
else
    print_log "yay is already installed"
fi


print_section "Installing packages"
package_count=$(< "${PACKAGES_DIR}/packages.list" wc -l)
print_log "Installing ${package_count} packages"
yay -S --noconfirm --needed $(cat "${PACKAGES_DIR}/packages.list")


###############################################################################
# Create directories and symlinks                                             #
###############################################################################
print_section "Creating directories"
for dir in "${DIRECTORIES[@]}"
do
    create_directory "${dir}"
done

print_section "Creating symlinks"
for file in "${LINKED_FILES_HOME[@]}";
do
    create_link "${file}"
done

for file in "${!LINKED_FILES[@]}"; do
    print_log "Linking ${YELLOW}${file}${RESET} -> ${YELLOW}${LINKED_FILES[${file}]}${RESET}"
    rm -rf "${file}"
    ln -fs "${LINKED_FILES[${file}]}" "${file}"
done

print_section "Give permissions"
for permission in "${!PERMISSIONS[@]}"; do
    set_permission "${USER}" "${PERMISSIONS[${permission}]}" "${permission}"
done

print_section "Add user to groups"
for group in "${ADD_GROUPS[@]}"
do
    add_to_group "${USER}" "${group}"
done

print_section "Copying systemd services"
for file in "${SYSTEM_DIR}/etc/systemd/system/"*
do
    print_log "Copy ${YELLOW}$(basename "${file}")${RESET} to ${YELLOW}/etc/systemd/system${RESET}"
    sudo install -m 644 "${file}" "/etc/systemd/system"
done

print_section "Enabling systemd services"
for service in "${SYSTEMD_SERVICES[@]}"
do
    enable_service "${service}"
done

print_section "Copy custom udev rules"
for file in "${SYSTEM_DIR}/etc/udev/rules.d/"*.rules
do
    copy_udev_rule "${file}"
done
sudo udevadm control --reload-rules

print_section "Copy sudoers content"
for file in "${SYSTEM_DIR}/etc/sudoers.d/"*
do
    copy_sudoers_content "${file}"
done

print_section "Set lockscreen image"
echo "${DOTFILES}/assets/lockscreen.${HOST}.jpg"
betterlockscreen -u "${DOTFILES}/assets/lockscreen.${HOST}.jpg"

print_section "Create issue file"
{
echo '\e{red}';
< "/etc/hostname" tr '[:lower:]' '[:upper:]' | figlet -f big | sed "s/\\\\/\\\\\\\/g";
echo -e "\\\r (\\\l)";
echo '\e{reset}';
} >> "/tmp/issue"
sudo mv "/tmp/issue" "/etc/issue"
print_log "Issue file created"