all:
	echo OK

check-deps-path:
	which setcap
	which usermod

remove-debian-sources:
	rm -vf /etc/apt/sources.list.d/debian.sources

install-il-debian-sources:
	cp -vf il-sources.list /etc/apt/sources.list.d/il-sources.list

install-iptables-tools:
	apt install iptables iptstate conntrack iptables-persistent netfilter-persistent -y

install: install-gns3

install-docker:
	bash install-docker.sh

install-gns3-deps:
	apt update
	apt install -y build-essential jq rsync net-tools lsb-release
	apt install -y bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu-system qemu-kvm bridge-utils libvirt-daemon-system
	apt install -y python-dev-is-python3 libelf-dev libpcap-dev python3-pyqt5 xterm cmake
	apt install -y python3-pyqt5.QtSvg python3-pyqt5.qtwebsockets
	DEBIAN_FRONTEND=noninteractive apt install -y bash-completion libguestfs-tools bsd-mailx

install-gns3: install-gns3-deps check-deps-path install-busybox-static install-pip upgrade-pip3 install-docker create-gns3-user build-gns3-server build-gns3-gui build-gns3-vpcs build-gns3-dynamips build-gns3-ubridge
	ln -s /usr/bin/qemu-system-x86_64 /usr/bin/qemu-kvm; true
	chown gns3:gns3 /home/gns3/ -R
	systemctl daemon-reload
	systemctl enable gns3.service
	systemctl start gns3.service

build-gns3-server:
	git clone --depth 1 https://github.com/GNS3/gns3-server.git; true
	cd gns3-server && git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' && git pull origin && git checkout tags/v2.2.49
	python3 -m pip install --upgrade pip --break-system-packages
	cd gns3-server && pip3 install -r requirements.txt --break-system-packages && python3 setup.py install
	ln -s /usr/bin/qemu-system-x86_64 /usr/bin/qemu-kvm;true
	cd gns3-server/init && cp gns3.service.systemd /etc/systemd/system/gns3.service
	chown root:root /etc/systemd/system/gns3.service
	sed -i -e "s@^ExecStart=.*@ExecStart=/usr/local/bin/gns3server@g" /etc/systemd/system/gns3.service
	mkdir -p /home/gns3/.config/GNS3/2.2/
	cp -vf gns3_server.conf /home/gns3/.config/GNS3/2.2/gns3_server.conf
	chown gns3:gns3 /home/gns3/ -R
	systemctl daemon-reload

build-gns3-gui:
	git clone --depth 1 https://github.com/GNS3/gns3-gui.git; true
	python3 -m pip install --upgrade pip --break-system-packages
	cd gns3-gui && pip3 install -r requirements.txt --break-system-packages && python3 setup.py install

build-gns3-vpcs:
	git clone --depth 1 https://github.com/GNS3/vpcs.git; true
	cd vpcs/src && ./mk.sh && cp -v vpcs /usr/local/bin/vpc

build-gns3-dynamips:
	git clone --depth 1 https://github.com/GNS3/dynamips.git; true
	cd dynamips && mkdir build && cd build && cmake .. && make && make install

build-gns3-ubridge:
	git clone --depth 1 https://github.com/GNS3/ubridge.git; true
	cd ubridge && make && make install

upgrade-gns3-server: 
	git clone --depth 1 https://github.com/GNS3/gns3-server.git; true
	cd gns3-server && git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' && git pull origin && git checkout tags/v2.2.49
	python3 -m pip install --upgrade pip --break-system-packages
	systemctl stop gns3.service
	cd gns3-server && pip3 install -r requirements.txt --break-system-packages && python3 setup.py install
	systemctl start gns3.service

create-gns3-user:
	useradd gns3;true
	mkdir -p /home/gns3
	rsync -av /etc/skel/ /home/gns3/
	usermod -a -G qemu gns3;true
	usermod -a -G kvm gns3;true
	usermod -a -G libvirt gns3;true
	usermod -a -G docker gns3;true

cleanup-gns3-sources:
	rm -fr gns3-server
	rm -fr dynamips
	rm -fr gns3-gui
	rm -fr ubridge
	rm -fr vpcs

install-pip:
	apt install -y python3-pip

upgrade-pip3:
	python3 -m pip install --upgrade pip --break-system-packages

upgrade-python-deps:
	pip3 install --upgrade requests --break-system-packages
	pip3 install --upgrade chardet --break-system-packages

install-busybox-static:
	apt install -y busybox-static

get-busy-box-static:
	bash get-busy-box-static.sh

disable-firewalld:
	systemctl disable --now firewalld


fix-cloudinit-netplan-permissions:
	chmod 600 /etc/netplan/50-cloud-init.yaml

install-default-iptables-persistent-rules:
	cp -vf rules.v4 /etc/iptables/rules.v4
	cp -vf rules.v6 /etc/iptables/rules.v6

install-debain-hv-scripts:
	mkdir -p /usr/libexec/hypervkvpd
	cp -vf hv_get_dns_info.sh /usr/libexec/hypervkvpd/hv_get_dns_info
	chmod +x /usr/libexec/hypervkvpd/hv_get_dns_info
	cp -vf hv_get_dhcp_info.sh /usr/libexec/hypervkvpd/hv_get_dhcp_info
	chmod +x /usr/libexec/hypervkvpd/hv_get_dhcp_info
