#!@TERMUX_PREFIX@/bin/bash
##
## Termux script for restoring $PREFIX (package installation dir).
## For backup script, see "termux-backup".
##
## Why it doesn't implement support for restoring $HOME:
##
##  * It can't selectively restore files due to tar option
##    --recursive-unlink, that will erase file tree before extracting.
##
##  * It has to maintain support of piped input. Therefore we can't
##    allow optional backup of home by termux-backup. Here is why:
##    with piped input it is not possible to check contents of backup
##    file (you can't seek piped content twice). We must be sure that
##    all directories selected for restore are present inside archive
##    and this is possible only with hardcoded configuration
##    (e.g. backup only $PREFIX). Otherwise, restore procedure may
##    fail or even worse, erase all directories (potentially user
##    data) that are not present in backup.
##
## Considering the mentioned 2 major issues, should be obvious that
## we have literally 2 choices: either backup/restore only $PREFIX
## or backup/restore both $HOME and $PREFIX unconditionally, to
## ensure consistency of data passed to termux-restore.
##
## However the current choice is to stick to backing up only $PREFIX,
## to leave responsibility of managing files in $HOME to user (as said
## above, termux-restore script ERASES DATA before restore).
##
## On side note, why data should be erased before restore:
##
##  * Perform a complete rollback to state exactly as in archive.
##
##  * Get rid of files that are not tracked by package manager.
##

set -e -u

export PREFIX=@TERMUX_PREFIX@

msg() {
	echo "$*" >&2
}

show_usage() {
	msg
	msg "Usage: termux-restore [input file]"
	msg
	msg "Script for restoring Termux installation directory (\$PREFIX)"
	msg "from the given TAR archive."
	msg
	msg "It is expected that backup file was made by 'termux-backup'."
	msg "Restoring procedure will erase all files in \$PREFIX directory"
	msg "that are not present in the given backup file. Be careful."
	msg
	msg "Backup contents may be supplied via stdin by specifying input"
	msg "file as '-'. Note that piped TAR archive must be uncompressed."
	msg
}

if [ "$(id -u)" = "0" ]; then
	msg "This script should not be used as root."
	exit 1
fi

if [ $# -lt 1 ]; then
	msg
	msg "[!] Input file path is not specified."
	show_usage
	exit 1
fi

if [ $# -gt 1 ]; then
	shift 1
	msg
	msg "[!] Got extra arguments: $*"
	show_usage
	exit 1
fi

case "$1" in
	-\?|-h|--help|--usage) show_usage; exit 0;;
	*) BACKUP_FILE_PATH=$1;;
esac

if [ "$BACKUP_FILE_PATH" != "-" ] && [ ! -e "$BACKUP_FILE_PATH" ]; then
	msg
	msg "[!] File '$BACKUP_FILE_PATH' does not exist."
	msg
	exit 1
else
	if [ "$BACKUP_FILE_PATH" != "-" ] && [ ! -f "$BACKUP_FILE_PATH" ]; then
		msg
		msg "[!] Path '$BACKUP_FILE_PATH' is not a regular file."
		msg
		exit 1
	fi
fi

# Ensure that prefix doesn't contain read-only files.
msg "Fixing read-write access to files where necessary..."
find "@TERMUX_PREFIX@" -type d -print0 | xargs -0 -r chmod u+rwx
find "@TERMUX_PREFIX@" -type f -print0 | xargs -0 -r chmod u+rw

# --recursive-unlink is added intentionally to delete all orphan/extra files
# in $PREFIX. It must be restored to a clean state as in backup tarball.
msg "Erasing current \$PREFIX and restoring one from archive..."
tar -x -C "@TERMUX_BASE_DIR@" -f "$BACKUP_FILE_PATH" \
	--recursive-unlink --preserve-permissions ./usr
