#!/bin/bash
#

TERMINAL=$(tty)

clear

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


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

#########################################################################################################
# WEBMIN
# Option to install ConfigServer Firewall with webmin plugin

if [ "$webmin" ]; then

cat <<EOT >> $userdir/bin/webmin
#!/bin/bash

# When install Webmin and dependencies
# http://www.webmin.com/

sudo apt -y install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl libauthen-libwrap-perl apt-show-versions python
sudo apt -y install 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

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

        cd $userdir ; rm -rf csf csf.tgz

        sudo apt -y install libwww-perl liblwp-protocol-https-perl libgd-graph-perl

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

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

        sudo systemctl start csf
        sudo systemctl start lfd

        sudo systemctl restart networking

        break
        ;;

        No ) exit
        ;;
    esac
done

EOT

chown -h $newuser: $userdir/bin/webmin
chmod 0700 $userdir/bin/webmin

fi


#########################################################################################################
# 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


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


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


