#!/bin/bash
#

TERMINAL=$(tty)

clear

# Temporary colors
NORMAL=''
GREEN=''
RED=''
ITALIC=''
BOLD=''
if tty -s; then
  NORMAL="$(tput sgr0)"
  GREEN=$(tput setaf 2)
  RED="$(tput setaf 1)"
  BOLD=$(tput bold)
  ITALIC=$(tput sitm)
fi

userdir="$1"

if [ ! $userdir ]; then
  userdir="$PWD"
fi


#########################################################################################################
# EXTRAS
#########################################################################################################
# .vimrc

cat <<EOT >> $userdir/.vimrc
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!

:syntax enable


if exists('+selectmode')
        set slm=mouse,key
endif
if exists('+mousemodel')
        set mousemodel=popup
endif
if exists('+keymodel')
        set keymodel=startsel
endif
if exists('+selection')
        set selection=inclusive
endif

EOT

chown -h $newuser: $userdir/.vimrc


#########################################################################################################
# .screenrc

cat <<EOT >> $userdir/.screenrc
hardstatus alwayslastline "%{= r}(%H) %-w%{= c}%n %t%{-}%+w %-10=%<%l %-5=%{= g}%c"
termcapinfo xterm 'hs:ts'

EOT

chown -h $newuser: $userdir/.screenrc


#########################################################################################################
# .xinitrc
# turns off monitor VESA powersaving features
# https://askubuntu.com/questions/47311/how-do-i-disable-my-system-from-going-to-sleep

cat <<EOT >> $userdir/.xinitrc
setterm -blank 0 -powersave off -powerdown 0
xset s off

EOT

chown -h $newuser: $userdir/.xinitrc


# disable screen blanking and locking
# https://superuser.com/questions/644804/disable-screensaver-screen-blank-via-command-line

cat <<EOT >> $userdir/.xset
#!/bin/sh
export DISPLAY=:0.0
xset s off
xset s noblank
xset -dpms

EOT

chown -h $newuser: $userdir/.xset
chmod 0700 $userdir/.xset


#########################################################################################################
# OPTIONAL System services
#########################################################################################################

#########################################################################################################
# WoL
# https://photostructure.com/coding/wake-on-lan/


#########################################################################################################
# .
#########################################################################################################

#########################################################################################################
# .


#########################################################################################################
# OPTIONAL Filesystems & tools
#########################################################################################################

#########################################################################################################
# .

if [ "$fs" ]; then

	if [ "$fs_exfat" ]; then
		apt -y install xfsprogs
		modprobe -v xfs
	fi

	if [ "$fs_exfat" ]; then
		apt -y install exfat-fuse exfatprogs
	fi

fi

if [ "$fs_tools" ]; then

	if [ "$fs_tools_libvirt" ]; then
		apt -y install libguestfs-xfs
	fi

	if [ "$fs_tools_monitoring" ]; then
		apt -y install gdu	#- Pretty fast disk usage analyzer
		apt -y install ncdu	#- ncurses disk usage viewer
	fi

fi

#########################################################################################################
# OPTIONAL Network tools
#########################################################################################################

#########################################################################################################
# Network tools & programs Packages

if [ "$net_tools" ]; then
	apt -y install ifupdown2
	apt -y install net-tools
	apt -y install ethtool
	apt -y install bridge-utils
	apt -y install iperf iperf3

	# Wireless networking tools
	apt -y install wavemon
fi

#########################################################################################################
# Network tools & programs Packages

if [ "$wireguard" ]; then
	apt -y install wireguard wireguard-tools
fi

#########################################################################################################
# OPTIONAL EXTRAS
#########################################################################################################

#########################################################################################################
# Install Webmin
# Option to install ConfigServer Firewall with webmin plugin

if [ "$webmin" ]; then

	# Check user permission
	if [ "$(id -u)" -ne 0 ]; then
		echo "${RED}Error:${NORMAL} The \`extra\` script must be run as root!" >&2
		exit 1
	fi

	# Install Webmin dependencies
	apt -y install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl libauthen-libwrap-perl apt-show-versions

	# Install Webmin
	curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
	sh setup-repos.sh
	apt-get -y install --install-recommends webmin

	echo -e "\\n\\n"
	echo "Do you wish to install ConfigServer Firewall"
	select yn in "Yes" "No"; do
		case \$yn in
			Yes )
				# ConfigServer
				# https://www.configserver.com/cp/csf.html

				apt -y install libwww-perl liblwp-protocol-https-perl libgd-graph-perl firewalld postfix

				cd $userdir
				wget -O csf.tgz https://download.configserver.com/csf.tgz
				tar -xzf csf.tgz ; cd csf
				sh install.sh

				cd $userdir ; rm -rf csf csf.tgz

				/usr/share/webmin/install-module.pl /usr/local/csf/csfwebmin.tgz

				sed -i "s/TESTING = \"1\"/TESTING = \"0\"/g" /etc/csf/csf.conf
				sed -i "s/RESTRICT_SYSLOG = \"0\"/RESTRICT_SYSLOG = \"3\"/g" /etc/csf/csf.conf
				sed -i "s/TCP_IN = \"20,21,22,25,53,853,80,110,143,443,465,587,993,995\"/TCP_IN = \"20,21,22,25,53,853,80,110,143,443,465,587,993,995,10000\"/g" /etc/csf/csf.conf
				sed -i "s/TCP_OUT = \"20,21,22,25,53,853,80,110,113,443,587,993,995\"/TCP_OUT = \"20,21,22,25,53,853,80,110,113,443,587,993,995,10000\"/g" /etc/csf/csf.conf
				#sed -i "s/UDP_IN = \"20,21,53\"/UDP_IN = \"20,21,53,10000\"/g" /etc/csf/csf.conf
				#sed -i "s/UDP_OUT = \"20,21,53,113,123\"/UDP_OUT = \"20,21,53,67,113,123,10000\"/g" /etc/csf/csf.conf

				if [ "csf_IPv6" ]; then

					apt -y install libio-socket-inet6-perl

					sed -i "s/IPV6 = \"0\"/IPV6 = \"1\"/g" /etc/csf/csf.conf
					sed -i "s/TCP6_IN = \"20,21,22,25,53,853,80,110,143,443,465,587,993,995\"/TCP6_IN = \"20,21,22,25,53,853,80,110,143,443,465,587,993,995,10000\"/g" /etc/csf/csf.conf
					sed -i "s/TCP6_OUT = \"20,21,22,25,53,853,80,110,113,443,587,993,995\"/TCP6_OUT = \"20,21,22,25,53,853,80,110,113,443,587,993,995,10000\"/g" /etc/csf/csf.conf
				fi

				# If CSF is run on a router and run the firewall only on the wan interface
				if [ "csf_router" ]; then
					sed -i "s/ETH_DEVICE = \"\"/ETH_DEVICE = \"eth0\"/g" /etc/csf/csf.conf

					if [ "csf_IPv6" ]; then
						sed -i "s/ETH6_DEVICE = \"\"/ETH6_DEVICE = \"eth0\"/g" /etc/csf/csf.conf
					fi

					sed -i "s/ETH_DEVICE_SKIP = \"\"/ETH_DEVICE_SKIP = \"eth1\"/g" /etc/csf/csf.conf
				fi

				sed -i "s/DENY_IP_LIMIT = \"200\"/DENY_IP_LIMIT = \"500\"/g" /etc/csf/csf.conf

				if [ "csf_IPSET" ]; then

					apt -y install ipset
					sed -i "s/LF_IPSET = \"0\"/LF_IPSET = \"1\"/g" /etc/csf/csf.conf
				fi

				sed -i "s/LF_PERMBLOCK_COUNT = \"4\"/LF_PERMBLOCK_COUNT = \"3\"/g" /etc/csf/csf.conf
				sed -i "s/LF_NETBLOCK = \"0\"/LF_NETBLOCK = \"1\"/g" /etc/csf/csf.conf

				systemctl start csf
				systemctl start lfd

				systemctl restart networking

				break
				;;
			No ) exit
				;;
		esac
	done
fi


# sed -i "s/???/!!!/g" /etc/csf/csf.conf

#########################################################################################################
# NGINX

if [ "$nginx" ]; then

cat <<EOT >> /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/debian/ buster nginx
deb-src http://nginx.org/packages/debian/ buster nginx

EOT

curl http://nginx.org/keys/nginx_signing.key | apt-key add -

apt update

apt -y install nginx nginx-module-image-filter nginx-module-njs nginx-module-perl nginx-module-xslt python-certbot-nginx libgd-tools


mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled


FILES=/etc/nginx/sites-available/*.conf
for f in $FILES
do
  echo "Processing $f file..."
  ln -s /etc/nginx/sites-available/$f /etc/nginx/sites-enabled/$f
  sleep 0.5
done


fi

#########################################################################################################
# FreeNGINX

if [ "$freenginx" ]; then

	#FreeNGINX

fi


#########################################################################################################
# QEMU-KVM
# https://reintech.io/blog/installing-using-kvm-virtualization-debian-12
# https://tqdev.com/2023-install-kvm-from-the-cli-on-debian-12
# https://linuxconfig.org/setting-up-virtual-machines-with-qemu-kvm-and-virt-manager-on-debian-ubuntu
# https://blog.programster.org/install-debian-12-kvm-server

if

	apt update
	apt -y install cpu-checker
	kvm-ok

fi

if

	apt update
	apt -y install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst libvirt-daemon
	apt -y install virt-manager

	# libvirt-clients-qemu libvirt-login-shell libvirt-daemon-driver-storage-gluster libvirt-daemon-driver-storage-iscsi-direct libvirt-daemon-driver-storage-rbd libvirt-daemon-driver-storage-zfs

	usermod -a -G libvirt $newuser

	sed -i "s/#uri_default/uri_default/g" /etc/libvirt/libvirt.conf

fi


#########################################################################################################
# Network monitoring tools
# 
#  Icinga. Icinga is an open source, free network monitoring software that you can use to assess performance across networks, Cloud services, and data centers. ...
#  NinjaOne. ...
#  Paessler PRTG Network Monitor. ...
#  OpManager. ...
#  Datadog. ...
#  Nagios Core. ...
#  Catchpoint.


#########################################################################################################
# Numerous Window Managers and Desktop Environments are available
#########################################################################################################
# i3wm
# https://github.com/DarkStarSword/junk/tree/master/config/home/.i3

if [ "$desktop_i3wm" ]; then

	apt update
	apt -y install xinit i3 i3-wm i3blocks i3lock i3lock-fancy i3status i3pystatus suckless-tools stterm

fi

#########################################################################################################
# Xfce

if [ "$desktop_xfce" ]; then

	#Xfce

fi

#########################################################################################################
# MATE
# https://dwaves.de/2024/03/02/gnu-linux-debian-12-how-to-install-mate-desktop/

if [ "$desktop_mate" ]; then

	apt update

	# This will install the base packages required for a minimal MATE desktop
	apt -y install mate-desktop-environment-core

	# This will install the complete MATE desktop
	apt -y install mate-desktop-environment

	# This will install the complete MATE desktop including a few extras
	apt -y install mate-desktop-environment-extras

	if [ "$mate-desktop-environment-core" ]; then

		#

	fi

	if [ "$mate-desktop-environment" ]; then

	apt -y install network-manager network-manager-gnome \
		network-manager-fortisslvpn network-manager-fortisslvpn-gnome \
		network-manager-l2tp network-manager-l2tp-gnome \
		network-manager-openconnect network-manager-openconnect-gnome \
		network-manager-openvpn network-manager-openvpn-gnome \
		network-manager-pptp network-manager-pptp-gnome \
		network-manager-ssh network-manager-ssh-gnome \
		network-manager-vpnc network-manager-vpnc-gnome

	fi

	if [ "$mate-desktop-environment-extras" ]; then

		#

	fi

fi

#########################################################################################################
# LXDE

if [ "$desktop_lxde" ]; then

	#LXDE

fi

#########################################################################################################
# LXQt

if [ "$desktop_lxqt" ]; then

	#LXQt

fi


#########################################################################################################
# Display manager
###
# cross-desktop display manager LightDM

if [ "$display_manager_lightdm" ]; then
	apt -y install lightdm
fi



#########################################################################################################
# Conky
# https://github.com/tonyho/i3wm-conky-config
# https://github.com/tonyho/i3wm-conky-config/blob/master/config
# https://www.reddit.com/r/i3wm/comments/4x8tif/i3_and_conky_in_the_background/
# https://forum.manjaro.org/t/configure-conky-through-manjaro-i3/109546/4

apt -y install conky-all


#########################################################################################################
# Programs

apt -y install terminator remmina xscreensaver xscreensaver-gl firefox-esr


#########################################################################################################
# VSCodium
# https://ipv6.rs/tutorial/Debian_Latest/VSCodium/


#########################################################################################################
# NoMachine remote desktop
# https://www.nomachine.com/

if [ "$nomachine_i386" ]; then

	wget https://www.nomachine.com/free/linux/32/deb -O nomachine_i386.deb
	dpkg -i nomachine_i386.deb
	rm nomachine_i386.deb

fi

if [ "$nomachine_amd64" ]; then

	wget https://www.nomachine.com/free/linux/64/deb -O nomachine_amd64.deb
	dpkg -i nomachine_amd64.deb
	rm nomachine_amd64.deb

fi

#########################################################################################################
# 

#########################################################################################################
# 

#########################################################################################################
#

#########################################################################################################
#


# Press a key to reboot
#read -s -n 1 -p "Press any key to reboot"
#reboot


