#!/bin/sh


# ask for right script call
while true; do
    read -p "Usage: ./proxmox-setup.sh <internalIP for vmbr0 10.10.10.X> <hostnameWithoutZAP> <shortNodeName> <internal IP for dedicated interface 10.11.52.X> <dedicated interface>. Used it like this?" yn
    case $yn in
        [Yy]* ) break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

sed -i '/none swap sw/c\#/dev/pve/swap none swap sw 0 0' /etc/fstab;

# check if already updated and rebooted
while true; do
    read -p "Already changed repo, updated and rebooted?" yn
    case $yn in
        [Yy]* ) break;;
        [Nn]* ) apt -y update; apt -y upgrade; echo "deb http://download.proxmox.com/debian/pve buster pve-no-subscription" > /etc/apt/sources.list.d/pve-enterprise.list; 
		apt update; 
		apt -y upgrade;
		sed '/bridge_fd/q' /etc/network/interfaces > /etc/network/interfaces.new;


		echo "auto vmbr0:0
iface vmbr0:0 inet static
        address $1
        netmask 255.255.255.0
		
auto $5
iface $5 inet static
		address $4
		netmask 255.255.255.0
		" >> /etc/network/interfaces.new;

		cat /etc/network/interfaces.new > /etc/network/interfaces;

		ifup vmbr0:0;
		ifup $5;
		/etc/init.d/networking restart;
		echo "############### did ifup vmbr0:0, did ifup $5 ###############";

		IP=$(grep `hostname` /etc/hosts | awk '{ print $1 }');
		echo "your external node IP is $IP. If not, pleace stop the script (CTL + C)";
		sleep 10;
		sed -i "s/${IP}/${4}/g" /etc/hosts;
		sed -i '/swap/d' /etc/fstab; 
		#reboot;
		exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo "I will ask for bondbabe009-76090-bl01 password, please provide";
sleep 5;
pvecm add 10.11.52.1;

# ask for reboot after pvecm add
while true; do
    read -p "pvecm add done, should I reboot now?" yn
    case $yn in
        [Yy]* ) reboot;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo "###############did pvecm add, ###############";
apt -y install certbot sudo vnstat screen openjfx;
mkdir /opt/jdk/; cd /opt/jdk/; wget http://185.249.196.89/jdk-8u211-linux-x64.tar.gz; tar xfvz jdk-8u211-linux-x64.tar.gz; mv jdk*/ .; rm *tar*; update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_211/bin/java 100; update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_211/bin/javac 100;
echo "did apt -y install certbot sudo vnstat openjdk-8-jre-headless screen openjfx, sleeping 5"; sleep 5;

echo "
Match Address 95.156.227.249
    PermitRootLogin yes
Match Address 10.10.10.*
    PermitRootLogin yes
Match Address 10.10.20.*
    PermitRootLogin yes"	>> /etc/ssh/sshd_config;
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf;
echo fs.inotify.max_user_instances=10240000 | sudo tee -a /etc/sysctl.conf;
echo fs.inotify.max_queued_events=104857600 | sudo tee -a /etc/sysctl.conf;
sysctl -p;
echo "will start with partition stuff, sleeping 5"; sleep 5;
lvremove -y pve data;
lvcreate -n lxcstor01 -l 100%FREE pve;
mkdir /mnt/$3-lxcstor01-localLV;
mkfs.ext4 /dev/mapper/pve-lxcstor01;
mount /dev/mapper/pve-lxcstor01 /mnt/$3-lxcstor01-localLV;
echo "done with partition stuff, sleeping 5"; sleep 5;
echo 'UUID="' > fstabtemp; blkid /dev/mapper/pve-lxcstor01 | awk '{ print $2 }' | grep -o '[a-z]*[0-9]*[-]*' >> fstabtemp; echo '"' >> fstabtemp;  echo "   /mnt/$3-lxcstor01-localLV/ ext4 defaults,errors=continue,rw,noatime,nodiratime,async,barrier=1 0 0" >> fstabtemp;
tr -d "\n\r" < fstabtemp > fstabtemp-formatted;
cat fstabtemp-formatted >> /etc/fstab;
rm fstabtemp*;
echo "generated fstab, sleeping 5"; sleep 5;
mv /home/imageserver /home/imageserver2;
ln -s /mnt/pve/imageserver/ /home/imageserver;
dpkg -l | grep qemu-server | grep "6.3" > /dev/null && wget http://185.223.30.65/qemu-060321.tar && tar xfvz qemu-060321.tar && ./qemu-install.sh && rm qemu-060321.tar
systemctl restart pveproxy pvedaemon;
rm /usr/bin/check_guest_traffic.sh; rm /lib/systemd/system/check_guest_traffic.service; rm /etc/systemd/system/check_guest_traffic.service; cd /usr/bin/; wget http://134.255.232.30/check_guest_traffic.sh; chmod +x check_guest_traffic.sh; cd /lib/systemd/system/; wget http://134.255.232.30/check_guest_traffic.service; cd /etc/systemd/system/; wget http://134.255.232.30/check_guest_traffic.service; chmod 644 /etc/systemd/system/check_guest_traffic.service; systemctl start check_guest_traffic; systemctl enable check_guest_traffic; systemctl restart check_guest_traffic; sleep 5; systemctl status check_guest_traffic;
sed -i '/corosync/d' /etc/crontab; echo "* * * * * root /root/corosync.sh >/dev/null 2>&1" >> /etc/crontab; cd /root; rm corosync.sh; wget http://134.255.232.30/corosync.sh; chmod +x corosync.sh;
sed -i '/ExecStart/c\ExecStart=/usr/bin/lxcfs -l /var/lib/lxcfs/' /lib/systemd/system/lxcfs.service;
systemctl stop pvesr.timer;
systemctl disable pvesr.timer;
echo "please set a root passwd and safe it in 1passwd";
passwd root;
echo "installed and configured dhcp server, sleeping 5"; sleep 5;
echo "done! This is the storage path, please add into proxmox GUI:"
cat /etc/fstab  | grep $3 | awk '{print $2}'
echo "This is your external IP, please add into firewall settings in proxmox GUI:"
cat /etc/network/interfaces | grep address | grep -v $1 | grep -v $4 | awk '{print $2}';
echo "Bitte das SSL Zertifikat in der Proxmox GUI bestellen!";
