#!/bin/sh
#
# Copyright (C) 2017 Dream Property GmbH
#

source librecovery

usage()
{
	echo "Usage: ${0} [-bhqrtv] <dreambox-image.tar.xz>"
	exit ${1}
}

BACKUP_SETTINGS=":"
RESTORE_SETTINGS=":"
SETTINGS="settings.tar.gz"
UPDATE_CACHE_SIGNED=":"

while getopts bchqrtv opt; do
	case "${opt}" in
		b)
			BACKUP_SETTINGS="backup-settings ${SETTINGS}"
			;;
		c)
			UPDATE_CACHE_SIGNED="update_cache_signed"
			;;
		r)
			RESTORE_SETTINGS="restore-settings ${SETTINGS}"
			;;
	esac
	std_opt "${opt}"
done

shift $((${OPTIND} - 1))
[ "$#" -eq 1 ] || usage 1
FILENAME="${1}"

assert_rescue_mode

DEVICE=${ROOT_PARTITION:-${FLASH_DEVICE}}
is_writable_blockdev "${DEVICE}" || is_writable_chardev "${DEVICE}" || abort "Target device '${DEVICE}' is not writable"
! is_empty "${FILENAME}" || abort "No tarball filename given"

case "${FILENAME}" in
	*://*)
		BASE_URI="$(dirname "${FILENAME}")"
		FILENAME="$(basename "${FILENAME}")"
		;;
	*)
		BASE_URI=""
		FILENAME=$(xrealpath "${FILENAME}")
		is_readable_file "${FILENAME}" || abort "Cannot access '${FILENAME}'"
		;;
esac

create_workspace

mount_cache

if [ -n "${BASE_URI}" ]; then
	create_keyring_for_uri "${BASE_URI}/${FILENAME}"
	fetch_signed "${BASE_URI}" "${FILENAME}"
fi

if [ -n "${UBI_DEVICE}" ] && ! is_chardev "${UBI_DEVICE}" && ! attach_ubi_device "${FLASH_DEVICE}"; then
	erase "${FLASH_DEVICE}" || abort "Failed to erase ${FLASH_DEVICE}"
	format_ubi_device "${FLASH_DEVICE}" || abort "Failed to format ${FLASH_DEVICE}"
	attach_ubi_device "${FLASH_DEVICE}" || abort "Failed to attach ${FLASH_DEVICE} to UBI"
fi

[ ! -f "${RECOVERY_CACHE}/settings.tar.gz" ] || cp ${RECOVERY_CACHE}/settings.tar.gz ${SETTINGS}
if blkdev_has_filesystem "${ROOT_PARTITION}" || is_chardev "${ROOT_PARTITION}"; then
	${BACKUP_SETTINGS}
fi
[ "${RESTORE_SETTINGS}" = ":" -o -f "${SETTINGS}" ] || abort "Can't restore settings without prior backup!"

unmount "${ROOT_MOUNTPOINT}" || abort "Failed to unmount root filesystem"

if [ -n "${UBI_DEVICE}" ]; then
	if is_chardev "${ROOT_PARTITION}"; then
		remove_ubi_volume || abort "Failed to remove UBI volume"
	fi

	create_ubi_volume || abort "Failed to create UBI volume"
	udevadm settle
	create_nand_filesystem "${ROOT_PARTITION}" || abort "Failed to create ${FILESYSTEM} filesystem on '${ROOT_PARTITION}'"
else
	if ! is_blockdev "${ROOT_PARTITION}" || ! blkdev_has_filesystem "${DATA_PARTITION}"; then
		if is_beneath_directory "${FILENAME}" "${DATA_MOUNTPOINT}"; then
			xcp "${FILENAME}" .
			FILENAME=`basename "${FILENAME}"`
		fi
		unmount "${DATA_MOUNTPOINT}" || abort "Failed to unmount data filesystem"
		if ! is_blockdev "${ROOT_PARTITION}" || ! is_blockdev "${DATA_PARTITION}"; then
			create_partition_table "${FLASH_DEVICE}" || abort "Failed to partition disk"
		fi
		if ! blkdev_has_filesystem "${DATA_PARTITION}"; then
			create_filesystem "${DATA_NAME}" "${DATA_PARTITION}" || warn "Failed to create ${FILESYSTEM} filesystem '${DATA_NAME}' on '${DATA_PARTITION}'"
		fi
		mount_cache
	fi
	create_filesystem "${ROOT_NAME}" "${ROOT_PARTITION}" || abort "Failed to create ${FILESYSTEM} filesystem '${ROOT_NAME}' on '${ROOT_PARTITION}'"
fi

safe_mount "${ROOT_PARTITION}" "${ROOT_MOUNTPOINT}" || abort "Failed to mount root filesystem"
extract_tarball "${FILENAME}" "${ROOT_MOUNTPOINT}" || abort "Failed to extract root filesystem"
run_postinsts "${ROOT_MOUNTPOINT}" ${POSTINSTS} || abort "Failed to run postinst scripts"
${RESTORE_SETTINGS}
if is_legacy_version && [ -d "${ROOT_MOUNTPOINT}/etc" ]; then
	touch "${ROOT_MOUNTPOINT}/etc/.rescue-update-suggested"
fi
unmount "${ROOT_MOUNTPOINT}" || warn "Failed to unmount root filesystem"

[ "${BACKUP_SETTINGS}" = ":" ] || update_cache "${SETTINGS}"
[ -z "${BASE_URI}" ] || ${UPDATE_CACHE_SIGNED} "${FILENAME}"
unmount_cache

info "Finished successfully. You may reboot now."
