#!/bin/bash
. ./lib

options=()
options+=("networkmanager" "Network Management" on)
options+=("openssh" "Server SSH" on)
options+=("cronie" "Cron tasks server" on)
options+=("xdg-user-dirs" "Create user directories" on)
options+=("numlockon" "numlock on on tty" on)
options+=("haveged" "Antropy generator" on)
if (lscpu | grep Intel) then
  options+=("intel-ucode" "Intel microcode" on)
else
  options+=("intel-ucode" "Intel microcode" off)
fi
if (lscpu | grep AMD) then
  options+=("amd-ucode" "AMD microcode" on)
else
  options+=("amd-ucode" "AMD microcode" off)
fi
options+=("net-snmp" "SNMP Server" off)
options+=("samba" "Server SMB" off)
if (dmesg |grep Bluetooth) then
  options+=("bluez" "" on)
  options+=("bluez-libs" "" on)
else
  options+=("bluez" "" off)
  options+=("bluez-libs" "" off)
fi
options+=("syslog-ng" "" off)
options+=("pkgstats" "" off)
options+=("ntp" "" off)
#options+=("rsyslog" "" off)
#options+=("systemd-readahead" "(AUR) boot improvement" off)

sel=$(whiptail --backtitle "$apptitle" --title "Services Install" --checklist "" --cancel-button "Back" 0 0 0 \
  "${options[@]}" \
  3>&1 1>&2 2>&3)
if [ ! "$?" = "0" ]; then
  exit 1
fi

for itm in $sel; do
  case $itm in
    '"numlockon"') ;;
    '"systemd-readahead"') aurpkg="$aurpkg $(echo $itm | sed 's/"//g')";;
    *) pkg="$pkg $(echo $itm | sed 's/"//g')";;
  esac
done


instpkg "$pkg" "$aurpkg"

for itm in $sel; do
  case $itm in
    '"networkmanager"')
      systemctl disable dhcpcd
      if (svcenable NetworkManager) then
        svcdisable dhcpcd
      fi
    ;;
    '"openssh"')
      svcenable sshd "SSH Server"
    ;;
    '"cronie"')
      svcenable cronie
    ;;
    '"bluez"')
      svcenable bluetooth "Bluetooth Service"
    ;;
    '"ntp"')
      svcenable ntpd "NTP Server"
    ;;
    '"numlockon"') script install/system/services/numlockon;;
    '"haveged"')
			svcenable haveged "Haveged"
    ;;
    '"syslog-ng"')
      #echo "ForwardToSyslog=yes" >> /etc/systemd/journald.conf
      systemctl enable syslog-ng@default
    ;;
    '"rsyslog"')
      systemctl enable rsyslog
    ;;
    '"net-snmp"')
      if (confirm "Create and edit /etc/snmp/snmpd.conf ?") then
        mkdir /etc/snmp > /dev/null
        echo "# SNMP Server Config" > /etc/snmp/snmpd.conf
        echo "# ------------------" >> /etc/snmp/snmpd.conf
        echo "" >> /etc/snmp/snmpd.conf
        echo "# SNMP 1 and 2" >> /etc/snmp/snmpd.conf
        echo "#rocommunity read_only_user" >> /etc/snmp/snmpd.conf
        echo "#rwcommunity read_write_user" >> /etc/snmp/snmpd.conf
        echo "" >> /etc/snmp/snmpd.conf
        echo "# SNMP 3" >> /etc/snmp/snmpd.conf
        echo "#rouser read_only_user" >> /etc/snmp/snmpd.conf
        editfile /etc/snmp/snmpd.conf
      fi
      if (confirm "Create and edit /var/net-snmp/snmpd.conf (for SNMPv3) ?") then
        mkdir -p /var/net-snmp/ > /dev/null
        echo "# Note : Clear text password will be encrypted when you restart snmpd." > /var/net-snmp/snmpd.conf
        echo "" >> /var/net-snmp/snmpd.conf
        echo "#createUser read_only_user SHA password1 AES password2" >> /var/net-snmp/snmpd.conf
        editfile /var/net-snmp/snmpd.conf
      fi
      svcenable snmpd "SNMP Server"
    ;;
    '"systemd-readahead"')
      svcenable systemd-readahead
    ;;
  esac
done

exit 0