#! /bin/zsh

tools=( 
    netcat-openbsd 
    aria2 
    bat 
    btop 
    bind9-dnsutils 
    debian-goodies 
    gron 
    htop 
    hub 
    icdiff 
    ioping 
    iotop 
    keychain 
    lsof 
    mlocate 
    mtr 
    ncdu 
    net-tools 
    nload 
    nmon 
    oping 
    powerline 
    progress 
    pv 
    ranger 
    reptyr 
    taskwarrior 
    tig 
    tmux 
    tmux-plugin-manager 
    unp 
    jq 
    nethogs 
    duf 
)

GIT_ROOT=$(git rev-parse --show-toplevel)
cd ${GIT_ROOT}

if [[ ${1} == "update" ]]; then
    echo "Updating repos"
    cd ${GIT_ROOT}/private/
    git pull origin main

    cd ${GIT_ROOT}
    git pull origin main
fi

for file in $(ls -1 ./**/deploy.zsh); do
    chmod +x ${file}
    echo "Executing ${file}"
    ${file}
done

echo "Checking zplug installation"
if [ $(dpkg-query -W -f='${Status}' zplug 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
  echo "Installing zplug"
  sudo DEBIAN_FRONTEND=noninteractive apt install zplug -yq
fi

echo "Executing zplug init script"
# zplug - manage plugins
source /usr/share/zplug/init.zsh
zplug "zsh-users/zsh-syntax-highlighting"
zplug "zsh-users/zsh-autosuggestions"
zplug "zsh-users/zsh-completions"
zplug "plugins/per-directory-history", from:oh-my-zsh
zplug "plugins/history-substring-search", from:oh-my-zsh
zplug "plugins/common-aliases", from:oh-my-zsh
zplug "plugins/nmap", from:oh-my-zsh
zplug "plugins/taskwarrior", from:oh-my-zsh
zplug "romkatv/powerlevel10k", use:powerlevel10k.zsh-theme

echo "Installing zplug plugins"
# zplug - install/load new plugins when zsh is started or reloaded
if ! zplug check --verbose; then
    printf "Install? [y/N]: "
    if read -q; then
        echo; zplug install
    fi
fi

echo "Installing tools"
# Install "standard" tools
for i in "${tools[@]}"; do
  if [ $(dpkg-query -W -f='${Status}' ${i} 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
    echo "Installing ${i}"
    sudo DEBIAN_FRONTEND=noninteractive apt install ${i} -yq
  fi
done
