ROLES=base proxy toxcore
SHELLCHECK_OPTS=SC2003,SC2006,SC2010,SC2039,SC2181,SC2046,SC2086,SC2048,SC2162,SC2034,SC2030,SC2166,SC2242,SC2223,SC2319,SC3009,SC3011,SC3030,SC3043,SC3054,SC2009,SC1090,SC2164,SC3044

# FixMe
ANSIBLE_PLUGINS=/usr/local/lib/python3.11/site-packages/ansible-2.9.22-py3.11.egg/ansible/plugins

# Edit this to be one of pentoo or devuan depending on your host platform
# Find the corresponding host in hosts.yml and edit the settings, then
# change this to be that hostname
LOCALHOST=`cat /etc/hostname`

BOX_NBD_BASE_DIR=/a/tmp/GentooImgr
BOX_NBD_BASE_FILE=gentoo.qcow2
BOX_NBD_BASE_QCOW=${BOX_NBD_BASE_DIR}/${BOX_NBD_BASE_FILE}
# set this to the name linux_local_group host in hosts.yml
LOCAL_HOSTS_NAME=pentoo
# set this to the name linux_chroot_group host in hosts.yml
YAML_CHROOT_NAME=linuxGentoo
# set this to the libvirt name of the linux_libvirt_group host in hosts.yml
YAML_BOX_NAME=gentoo1
INST_BOX_NAME=gentoo1

#INST_BOX_DIR=/mnt/o/home/root/vms/virsh
INST_BOX_DIR=${BOX_NBD_BASE_DIR}/create-vm
PWD=/o/var/local/src/play_tox/
NETWORK=default
VERBOSE=2

all: install lint build check run test

lint::
	@sudo xmllint -noout roles/ansible-gentoo_install/templates/etc/libvirt/qemu/gentoo.xml
	@yamllint -c .yamllint.yml -f standard *.yml roles/*/*s/*yml 2>&1| \
		grep -v 'truthy\|indentation' | \
		sed -e '/^$$/d' | tee .yamllint.out | \
		grep -B 2 error | tee .yamllint.err || true
	grep Error .yamllint.out || true

# groddy but works for me
install:: lint 
#	( /usr/local/src ; ansible-galaxy collection install \
#	  file:///usr/local/src/community.general )
	[ -e $(ANSIBLE_PLUGINS)/connection/libvirt_qemu.py ] \
		|| ln -s ${PWD}/lib/plugins/libvirt_qemu.py \
		$(ANSIBLE_PLUGINS)/connection/q || true
	@[ -f ${BOX_NBD_BASE_QCOW} ] || { \
          echo ERROR: not created BOX_NBD_DEV="${BOX_NBD_DEV}" - use ; \
          echo qemu-img create -f qcow2 "${BOX_NBD_BASE_QCOW}" 20G ; \
			exit 2 ; }
	@( ps ax | grep -v grep | \
		grep "qemu-nbd.*/dev/nbd.*${BOX_NBD_BASE_QCOW}" ) || { \
	        echo ERROR: not mounted BOX_NBD_DEV="${BOX_NBD_DEV}" - use ; \
	        echo qemu-nbd -n -f qcow2 -c /dev/nbd1 ${BOX_NBD_BASE_QCOW} ;\
		exit 1 ; }

build::
	sudo $(MAKE) -$(MAKEFLAGS) build_base
	sudo $(MAKE) -$(MAKEFLAGS) build_overlay

build_base:: install
	echo INFO: $@ "${BOX_NBD_BASE_QCOW}"
	echo INFO running the toxcore role will build ${BOX_NBD_BASE_QCOW}
	sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml \
		-l ${LOCALHOST} -c local --verbose ${VERBOSE} \
		$(ROLES) > .$@-${LOCALHOST} 2>&1
	[ -f ${BOX_NBD_BASE_QCOW} ]

build_overlay::
	@virsh list | grep "${INST_BOX_NAME}.*running" && \
		virsh destroy ${INST_BOX_NAME} ; true
#	@virsh list | grep "${INST_BOX_NAME}.*running" && exit 1
	@virsh list --all | grep ${INST_BOX_NAME} && \
		virsh undefine ${INST_BOX_NAME} && \
		rm -f \
		 ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml \
		 ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ; true
# /a/tmp/GentooImgr/create-vm/xml/gentoo1.xml 
#	! virsh list --all | grep "${INST_BOX_NAME}" && exit 2
	[ ! -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ] || { \
		echo WARN delete this file to continue; \
		echo rm -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ; \
		exit 3 ; }
	[ ! -f ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml ] || { \
		echo WARN delete this file to continue ; \
		echo rm -f ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml ; \
		exit 4 ; }
	PLAY_ANSIBLE_SRC=${PWD} bash bin/toxcore_build_overlay_qcow.bash
	[ -f ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml ]
	xmllint -noout ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml

check::
	grep -n 'shell: *$$' roles/*/tasks/*.yml && { echo ERROR: "shell: in .yml" ; false ; } || true
	grep -n '^[a-z ]*: {{' roles/*/tasks/*.yml && { echo WARN: "{{ in .yml" ; false ; } || true
	@bash .pylint.sh ; cat .pylint.err ; true
	@shellcheck -s bash -W 0 -x -a -e ${SHELLCHECK_OPTS} bin/*sh || true
	$(MAKE) -$(MAKEFLAGS) check_base
	@[ -d /mnt/gentoo/lost+found ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_chroot
	@[ -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_overlay

check_localhost::
	sudo sh ansible_local.bash --diff -i hosts.yml -l ${LOCALHOST} \
		--check -c local --verbose ${VERBOSE} \
		$(ROLES) > .$@-$@-${LOCALHOST} 2>&1

check_base::
	ls ${BOX_NBD_BASE_QCOW}
	ls ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img
	ls ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml
	ps axf | grep 'qemu-system-x86_64 -name guest='${INST_BOX_NAME} ; \
		true

check_chroot::
	[ -d /mnt/gentoo/lost+found ] || exit 0
	ansible -c chroot -l ${YAML_CHROOT_NAME} -i hosts.yml \
		-m setup -vvv ${YAML_CHROOT_NAME}
	sudo sh ansible_local.bash --diff -i hosts.yml -l ${YAML_CHROOT_NAME} \
		--check -c chroot --verbose ${VERBOSE} \
		$(ROLES) > .$@-${YAML_CHROOT_NAME}-${LOCALHOST} 2>&1

check_overlay::
	sudo /var/local/sbin/hostvms_libvirt_test_ga.bash ${INST_BOX_NAME} ls /
# domain-*-gentoo/org.qemu.guest_agent.0 || true
	sudo find /var/lib/libvirt/qemu/channel/target/	| \
		grep org.qemu.guest_agent.0
	sudo find /var/lib/libvirt/qemu/channel/target/ -type s | \
		grep ${INST_BOX_NAME}
	ansible -c libvirt_qemu -l ${YAML_BOX_NAME} -i hosts.yml \
		-m setup -vvv ${YAML_BOX_NAME}
	sudo virsh list | grep -q ${INST_BOX_NAME} || exit 0
	sudo sh ansible_local.bash --diff -i hosts.yml -l ${INST_BOX_NAME} \
		--check -c libvirt_qemu --verbose ${VERBOSE} \
		$(ROLES) > .$@-${INST_BOX_NAME}-${LOCALHOST} 2>&1

# Edit hosts.yml and customize this target if you are on a Debianish
devuan::
	sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml \
		-l devuan -c local --verbose ${VERBOSE} $(ROLES) \
		> .$@-${LOCALHOST} 2>&1

# Edit hosts.yml and customize this target if you are on a Gentoo
pentoo::
	sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml \
		-l pentoo -c local --verbose ${VERBOSE} $(ROLES) \
		> .$@-${LOCALHOST} 2>&1

run::
	@[ ! -f ${BOX_NBD_BASE_QCOW} ] && \
		$(MAKE) -$(MAKEFLAGS) $@_local
	@[ -d /mnt/gentoo/lost+found ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_chroot
	@[ ! -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_libvirt

run_local:: lint
	A=`grep nbd /proc/partitions | wc -l`
	@[ $${A} -eq 0 ] && echo WARN looks like theres no nbd mount && \
		exit 0
	@[ -f ${BOX_NBD_BASE_QCOW} ] && \
		echo WARN  ${BOX_NBD_BASE_QCOW} exists - delete or rename \
		&& exit 0
	BOX_NBD_DEV=$( /usr/local/bin/ansible_get_inventory.bash BOX_NBD_DEV  $BOX)
	A=`grep $BOX_NBD_DEV /proc/partitions | wc -l`
	@[ $${A} -eq 0 ] && echo WARN looks like theres no $BOX_NBD_DEV nbd mount && \
		exit 0
	sudo sh ansible_local.bash --diff -i hosts.yml -l ${LOCALHOST} \
		-c local --verbose ${VERBOSE} $(ROLES) \
		> .$@-$@-${LOCALHOST} 2>&1

run_chroot::
	[ -d /mnt/gentoo/lost+found ] || exit 0
	sudo sh ansible_local.bash --diff -i hosts.yml -l ${YAML_CHROOT_NAME} \
		 -c chroot --verbose ${VERBOSE} $(ROLES) \
		> .$@-${YAML_CHROOT_NAME}-${LOCALHOST} 2>&1

run_libvirt::
	[ -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ]
	@virsh net-list | grep "${NETWORK}.*active" || \
		sudo virsh net-start "${NETWORK}"
	@virsh list | grep ${INST_BOX_NAME} && \
	  virsh define ${INST_BOX_DIR}/xml/${INST_BOX_NAME}.xml
	@virsh list | grep "${INST_BOX_NAME}.*running" || \
	  virsh start ${INST_BOX_NAME}
	sh ansible_local.bash --diff -i hosts.yml -l ${INST_BOX_NAME} \
		-c libvirt_qemu --verbose ${VERBOSE} $(ROLES) \
		> .run-${INST_BOX_NAME}-${LOCALHOST} 2>&1

# hourly is quick tests, weekly is medium tests, monthly is long tests
weekly:: test
test:: 
	@[ -d /mnt/gentoo/lost+found ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_local
	@[ -f ${INST_BOX_DIR}/images/${INST_BOX_NAME}.img ] && \
		sudo $(MAKE) -$(MAKEFLAGS) $@_libvert

test_local::
	bash .pyanal.sh &
	sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml -l ${LOCALHOST} \
		-c local \
		--verbose ${VERBOSE} -t weekly \
		$(ROLES) > .$@-${LOCALHOST}  2>&1

test_libvirt::
#	bash .pyanal.sh &
# check if ${INST_BOX_NAME} is running
	! sudo virsh list | grep -q ${INST_BOX_NAME} && exit 0
	sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml \
		-l ${INST_BOX_NAME} -c libvirt_qemu \
		--verbose ${VERBOSE} -t weekly \
		$(ROLES) > .$@-${LOCALHOST}  2>&1

veryclean:: clean
	rm -f .run* .check*

clean::
	find . -name \*~ -delete
	rm roles/*/vars/*.txt