#!/bin/zsh

# Copyright (C) 2023-2024 Dyne.org Foundation
#
# Designed, written and maintained by Denis Roio <jaromil@dyne.org>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  Please refer
# to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, see <https://www.gnu.org/licenses/>.

function mm() {
	>&2 echo "snapshot: $*"
}

let MINIMUM_LEN=8
let MAXIMUM_LEN=16
set -A CHARS a b c d e f g h i j k l m n o p q r s t u v w x y z \
	A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
	0 1 2 3 4 5 6 7 8 9
CNUM="${#CHARS[@]}"
MAXLEN=$(( $MINIMUM_LEN + ( $RANDOM % ( $MAXIMUM_LEN - $MINIMUM_LEN ) ) ))
RANDSTR=""
let POSCNT=0;
while [ 1 -eq 1 ]
do
	if [ $POSCNT -ge $MAXLEN ]
	then
		break;
	fi
	let POSCNT=$POSCNT+1
	RANDSTR="${RANDSTR}${CHARS[${RANDOM}%${CNUM}]}"
done
RANDSTR="${RANDSTR}${CHARS[${RANDOM}%${CNUM}]}"

mm "Snapshot the current persistence.qcow2 install used in Qemu"
[ ${UID} = 0 ] || { mm "Run as root." && exit 1 }

TEMPDIR=/tmp/dyneIV-persist-snap
SNAPFILE=dyneIV-snapshot-`date +'%d%m%y'`-${RANDSTR}.squashfs
COW=persistence.qcow2
COWMNT=/tmp/dyneIV-persist-cowmnt
LOOPMNT=/tmp/dyneIV-persist-loopmnt
SQFSCONF=(-c xz -j 6)

[ -r ${COW} ] || {
   mm "Cannot find ${COW}"
   exit 1 }

mkdir -p $COWMNT
modprobe nbd
qemu-nbd --connect=/dev/nbd0 "${COW}"
mount /dev/nbd0p1 "${COWMNT}"

[ -r ${COWMNT}/dyne/dyne.nst ] || {
	mm "File not found in ${COW}: dyne/dyne.nst"
	umount "${COWMNT}"
	qemu-nbd --disconnect /dev/nbd0
	exit 1 }

mkdir -p ${LOOPMNT}
mount -o loop ${COWMNT}/dyne/dyne.nst ${LOOPMNT}

rm -rf ${TEMPDIR}; mkdir -p ${TEMPDIR}

cp -ra ${LOOPMNT}/home/rw ${TEMPDIR}/home && chown -R 1000:1000 ${TEMPDIR}/home/*
cp -ra ${LOOPMNT}/root/rw ${TEMPDIR}/root && chown -R 0:0 ${TEMPDIR}/root
cp -ra ${LOOPMNT}/etc/rw ${TEMPDIR}/etc
find ${TEMPDIR} -name .cache -exec rm -rf {} +
umount ${LOOPMNT}
umount ${COWMNT}
qemu-nbd --disconnect /dev/nbd0
tar -c ${TEMPDIR} | tar2sqfs -r ${TEMPDIR} -f -q ${SQFSCONF} ${SNAPFILE}
mm "Snapshot created: ${SNAPFILE}"
