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

source librecovery

FILENAME=${RECOVERY_CACHE}/backup.tar.gz

usage()
{
	echo "Usage: ${0} [-hqtv] [<dreambox-image.tar.gz>]"
	echo "       Default: ${FILENAME}"
	exit ${1}
}

xgetopts $@

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

assert_rescue_mode
create_workspace

if [ -n "${UBI_DEVICE}" ] && ! is_chardev "${UBI_DEVICE}"; then
	attach_ubi_device "${FLASH_DEVICE}" || abort "Failed to attach ${FLASH_DEVICE} to UBI"
fi
safe_mount "${ROOT_PARTITION}" "${ROOT_MOUNTPOINT}" -o ro || abort "Failed to mount root filesystem"
if is_beneath_directory "${FILENAME}" "${DATA_MOUNTPOINT}"; then
	safe_mount "${DATA_PARTITION}" "${DATA_MOUNTPOINT}" || abort "Failed to mount data filesystem"
fi
create_tarball "${ROOT_MOUNTPOINT}" "${FILENAME}" || abort "Failed to create ${FILENAME}"
