#!/bin/sh
# Armbian: a snapper config for a btrfs root. With this config, the apt hook
# of the snapper package (/etc/apt/apt.conf.d/80snapper) takes a snapshot
# before and after each package change. armbian-snapshots.service runs this
# at each boot. It does nothing on other root file systems. It keeps a root
# config that already exists.
set -e

[ "$(findmnt -n -o FSTYPE /)" = btrfs ] || exit 0

# snapperd reads the config list only at start. A running snapperd does not
# see a new root config: snapper-boot and the apt hook then fail.
changed=
restart_snapperd() {
	[ -n "$changed" ] || return 0
	# --no-block: at boot, the start of snapperd waits for this service.
	systemctl -q is-active snapperd.service 2> /dev/null &&
		systemctl --no-block try-restart snapperd.service || true
}
trap restart_snapperd EXIT

if [ ! -e /etc/snapper/configs/root ]; then
	# create-config also makes the subvolume /.snapshots.
	snapper --no-dbus -c root create-config /
	# Snapshots for recovery after an update only: no hourly snapshots.
	# Keep 2 to 10. Above 2, cleanup removes the oldest when less than 20%
	# of the file system is free: SD cards are small.
	snapper --no-dbus -c root set-config \
		TIMELINE_CREATE=no \
		NUMBER_MIN_AGE=0 \
		NUMBER_LIMIT=2-10 \
		NUMBER_LIMIT_IMPORTANT=2-5 \
		FREE_LIMIT=0.2
	changed=1
	echo "Snapshots of / are on: a snapshot before and after each package change."
elif [ "$(stat -c %i /.snapshots 2>/dev/null)" != 256 ]; then
	# The config came with a copy of the system (armbian-install) without
	# the snapshots. rsync -x does not go into the subvolume /.snapshots: the
	# copy has an empty directory in its place, or nothing. Inode 256 is the
	# root of a btrfs subvolume.
	if [ -d /.snapshots ] && ! rmdir /.snapshots 2> /dev/null; then
		echo "/.snapshots is a directory with files, not a btrfs subvolume: no snapshots." >&2
		exit 1
	fi
	btrfs subvolume create /.snapshots > /dev/null
	chmod 0750 /.snapshots
	echo "Made the missing subvolume /.snapshots."
fi

# Debian: snapper reads its configs from SNAPPER_CONFIGS in /etc/default/snapper.
f=/etc/default/snapper
[ -e "$f" ] || exit 0
# Accept double quotes, single quotes and a trailing comment.
configs="$(sed -n 's/^SNAPPER_CONFIGS=//p' "$f" | tail -n 1)"
configs="$(printf '%s' "${configs%%#*}" | tr -d "\"'" | tr -s ' \t' '  ' | sed 's/^ //;s/ $//')"
case " $configs " in
*" root "*) ;;
*)
	configs="${configs:+$configs }root"
	changed=1
	if grep -q '^SNAPPER_CONFIGS=' "$f"; then
		sed -i "s/^SNAPPER_CONFIGS=.*/SNAPPER_CONFIGS=\"$configs\"/" "$f"
	else
		printf 'SNAPPER_CONFIGS="%s"\n' "$configs" >> "$f"
	fi
	;;
esac
