#!/bin/bash

ks_requires_liveimg=("wget" "rsync" "cpio" "tar" "zstd" "lz4" "unzip" "sha256sum")
liveimg()
{
	local PROG TEMP ret=0 archive='' isdir='' dev=''
	local url='' proxy='' noverifyssl='' checksum=''

	PROG="kickstart"
	message "command: ${FUNCNAME[0]} $*"

	ks_check_requires "${FUNCNAME[0]}" ||
		return 1

	PROG="${FUNCNAME[0]}"
	TEMP=`getopt -n "$PROG" -l 'url:,proxy:,checksum:,noverifyssl' -- "$PROG" "$@"` ||
		return 1
	eval set -- "$TEMP"

	while :; do
		case "$1" in
			--) shift; break
				;;
			--noverifyssl) noverifyssl=1
				;;
			--url) shift; url="$1"
				;;
			--checksum) shift; checksum="$1"
				;;
			--proxy) shift; proxy="$1"
				;;
		esac
		shift
	done

	if [ -z "$url" ]; then
		message "url required"
		return 1
	fi

	if ! mountpoint -q "$ks_rootdir"; then
		message "sysroot does not exist"
		return 1
	fi

	rm -rf -- "$ks_datadir/liveimg"
	mkdir -p -- "$ks_datadir/liveimg"

	case "$url" in
		http://*|https://*)
			env \
				${proxy:+"http_proxy=$proxy"} \
				${proxy:+"https_proxy=$proxy"} \
				wget -nd -P "$ks_datadir/liveimg/" "$url" \
					${noverifyssl:+--no-check-certificate}
			archive="$(find "$ks_datadir/liveimg" -type f -print -quit)"
			;;
		file://*)
			archive="${url#file://}"
			;;
		dir://*)
			archive="$url"
			;;
		*)
			message "unexpected url: $url"
			return 1
			;;
	esac

	if [ -n "$checksum" ]; then
		local cursum

		cursum="$(sha256sum < "$archive")"
		cursum="${cursum%% *}"

		if [ "$checksum" != "$cursum" ]; then
			message "computed checksum did NOT match"
			return 1
		fi
	fi

	case "$archive" in
		*.tar|*.tbz|*.tgz|*.txz|*.tar.bz2|*.tar.gz|*.tar.xz)
			tar -C "$ks_rootdir" -xf "$archive" ||
				ret=1
			;;
		*.tar.zst)
			zstd -d < "$archive" | tar -C "$ks_rootdir" -xf - ||
				ret=1
			;;
		*.tar.lz4)
			lz4 -d < "$archive"| tar -C "$ks_rootdir" -xf - ||
				ret=1
			;;
		*.zip)
			unzip -d "$ks_rootdir" "$archive" ||
				ret=1
			;;
		*.cpio)
			cpio -id -D "$ks_rootdir" --no-absolute-filenames < "$archive" ||
				ret=1
			;;
		dir://*)
			verbose "syncing ${archive#dir://} directory"
			set -- -abX
			if dev="$(cttyhack)" && [ -n "$dev" ] && [ -z "${dev##/dev/ttyS*}" ]; then
				set -- "$@" --info=stats2 --no-i-r
			else
				set -- "$@" ${verbose:+--info=progress2 --no-i-r}
			fi
			rsync "$@" "${archive#dir://}/" "$ks_rootdir/" ||
				ret=1
			;;
		*)
			message "unsupported archive format: $archive"
			return 1
			;;
	esac

	if [ "$ret" = 1 ]; then
		message "unable to unpack tar archive: $archive"
		return 1
	fi
}

