#!/bin/ash -efu
#
# mki-pack-results
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

# Format: <MKI_PACKTYPE>:<MKI_OUTNAME>[:<SUBDIR>]
typeslist="${MKI_PACK_RESULTS:?pack rules required}"

run_pack() {
	local str= type= MKI_OUTNAME= MKI_IMAGESUBDIR=

	case "$1" in
		boot:*|cpio:*|data:*|isoboot:*|isodata:*|yaboot:*|squash:*|tar:*|tarbz2:*)
			type="${1%%:*}"
			str="${1#*:}"
			MKI_OUTNAME="${str%%:*}"
			;;
		data)
			type='data'
			;;
		*) # custom
			type='custom'
			str="${1#*:}"
			MKI_OUTNAME="${str%%:*}"
			MKI_PACKTYPE="${1%%:*}"
			;;
	esac

	[ "$str" = "$MKI_OUTNAME" ] ||
		MKI_IMAGESUBDIR="${str#*:}"

	mki-pack-$type
}

set -- $typeslist

if [ "$#" -eq 1 ]; then
	run_pack "$1"
	exit
fi

unset CLEANUP_OUTDIR

for t in "${@-}"; do
	run_pack "$t"
done
