#!/bin/bash
# Info: Script to install OS packages and tools
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-11-2023

USAGE="install-os.sh  will-install-all-no-arguments | os | user | resolution | tools"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

#ORG=$(pwd)

_update_os() {
    echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
    local codename=$(grep VERSION_CODENAME /etc/os-release | cut -f2 -d"=" ) 
    if [ "$codename"  == "bookworm" ] ; then 
      echo "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";" | sudo tee '/etc/apt/apt.conf.d/no-bookworm-firmware.conf'
    fi
    sudo DEBIAN_FRONTEND=noninteractive apt-get update 
    sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y 
    sudo DEBIAN_FRONTEND=noninteractive apt-get -y -qq install sudo curl wget git jq dialog apt-utils gnupg unzip \
      network-manager \
      nfs-common sysstat sshfs \
      netcat-traditional iputils-ping \
      apt-transport-https ca-certificates \
      software-properties-common
    sudo DEBIAN_FRONTEND=noninteractive apt autoremove -y 2>/dev/null
} 

_ssh_keys() {
    local key_file
    if [ -n "$SSH_KEYS" ] && [ -d ".ssh" ]; then 
      for key in $SSH_KEYS
      do
        key_file=$(basename "$key")
        if [ ! -r "$HOME/.ssh/$key_file" ] && [ -r ".ssh/$key_file" ] ; then 
          cp ".ssh/$key_file" ".ssh/$key_file.pub" "$HOME/.ssh"
          if ! grep -q "$(cat ".ssh/$key_file.pub")" "$HOME/.ssh/authorized_keys" ; then
            cat ".ssh/$key_file.pub" >> "$HOME/.ssh/authorized_keys"
          fi
        fi
      done
    fi
}

_create_user() {
    local has_user
    sudo chmod 1777 /tmp
    [ -z "${DEV_USER}" ] && return
    has_user=$(sudo grep ${DEV_USER} /etc/passwd)
    [ -z "$DEV_USER" ] && return 1
    if [ -z "$has_user" ] ; then
    sudo adduser \
      --system \
      --shell "/bin/bash" \
      --gecos "$DEV_USER user" \
      --group \
      --disabled-password \
      --home "$DEV_USER_HOME" \
      "${DEV_USER}"
    else
      echo "User $DEV_USER already exists"
      return 
    fi
    [ ! -d "$DEV_USER_HOME" ] && sudo mkdir -p "$DEV_USER_HOME"
    if [ -z "$(sudo ls "$DEV_USER_HOME"/.profile 2>/dev/null)" ] ; then
      [ -r "$SOURCE_USER_PATH/.profile" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.profile "$DEV_USER_HOME"
    fi
    if [ -z "$(sudo ls "$DEV_USER_HOME"/.bashrc 2>/dev/null)" ] ; then
      [ -r "$SOURCE_USER_PATH/.bashrc" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.bashrc "$DEV_USER_HOME"
    fi
    if [ -z "$(sudo ls "$DEV_USER_HOME"/.bash_aliases 2>/dev/null)" ] ; then
      [ -r "$SOURCE_USER_PATH/.bash_aliases" ] && sudo cp -pvr "$SOURCE_USER_PATH"/.bash_aliases "$DEV_USER_HOME"
    fi
    if [ -z "$(sudo ls "$DEV_USER_HOME"/.ssh 2>/dev/null)" ] && [ -r "$SOURCE_USER_PATH/.ssh" ] ; then
      sudo cp -pvr "$SOURCE_USER_PATH"/.ssh "$DEV_USER_HOME"
    elif [ ! -d "$DEV_USER_HOME/.ssh" ] ; then
      mkdir -p "$DEV_USER_HOME/.ssh" 
    fi
    while IFS= read -r line
    do
      if  ! grep -q "$line" "$DEV_USER_HOME"/.ssh/authorized_keys 2>/dev/null ; then
        echo "$line" | sudo tee -a "$DEV_USER_HOME"/.ssh/authorized_keys >/dev/null
      fi
    done < "$HOME/.ssh/authorized_keys"
    sudo chown -R "$DEV_USER":"$DEV_USER_GROUP" "$DEV_USER_HOME"
    if [ ! -r "/etc/sudoers.d/$DEV_USER" ] ; then
      echo "$DEV_USER  ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers.d/$DEV_USER
    fi
    sudo rm -r "$SOURCE_USER_PATH" 
    # sudo sed -i "$ a AllowUsers $DEV_USER" /etc/ssh/sshd_config
}

_check_resolution() {
  local hostname=""
  hostname=$HOSTNAME
  local ip=""
  ip=$(grep "$hostname" /etc/hosts | grep -v "^#" | awk '{print $1}')
  [ -n "$ip" ] && [ "$ip" == "127.0.1.1" ] && sudo sed -i /^"$ip"/d /etc/hosts 2>/dev/null
  ip=$(grep "$PUB_IP" /etc/hosts | grep -v "^#" | awk '{print $1}')
  [ -z "$ip" ] && echo "$PUB_IP ${hostname}.pub" | sudo tee -a /etc/hosts 2>/dev/null >/dev/null
  ip=$(grep "$PRIV_IP" /etc/hosts | grep -v "^#" | awk '{print $1}')
  [ -z "$ip" ] && echo "$PRIV_IP ${hostname}.priv $hostname" | sudo tee -a /etc/hosts 2>/dev/null >/dev/null
  if [ "$hostname" !=  "$(cat /etc/hostname)" ] ; then
    echo "$hostname" | sudo tee /etc/hostname 2>/dev/null >/dev/null
    sudo hostname "$hostname"
  fi
}

[ -r "./env-os"  ] && . ./env-os

# Update and add packages to installation
[ -z "$1" ] || [ "$1" == "os" ] && _update_os
[ -z "$1" ] || [ "$1" == "ssh_keys" ] && _ssh_keys
[ -z "$1" ] || [ "$1" == "resolution" ] && _check_resolution
[ -z "$1" ] || [ "$1" == "user" ] && _create_user
