#!/bin/sh -Cefux


set -Cefux


cd /root/files/fbsd/embedded || exit 1


_sign()
{
	local key=private
	local file="$1"
	
	
	openssl dgst -sha512 -sign "$key" "$file" \
	| openssl enc -base64 -out "$file.sig"
}


umount /mnt || true

mdconfig -d -u 200 || true

rm -f last-updated.txt upgrade.img upgrade.img.sig upgrade.img.xz \
upgrade.img.xz.sig upgrade.uzip upgrade.uzip.sig

cp -a rootfs/root/last-updated.txt .

truncate -s 512m upgrade.img

mdconfig -u 200 -f upgrade.img

last_updated="`cat last-updated.txt`"
if [ ! "$last_updated" ]
then
	echo 'invalid last-updated.txt contents'
	exit 1
fi
newfs -t -U -L "root-$last_updated" /dev/md200

fsck -y /dev/md200

mount -o noatime /dev/md200 /mnt

tar -cf- -C rootfs/ . | tar -xpf- -C /mnt

umount /mnt

fsck -y /dev/md200

mdconfig -d -u 200

#mkuzip -A lzma -d -S -o upgrade.uzip upgrade.img

_sign last-updated.txt

_sign upgrade.img

xz upgrade.img

#_sign upgrade.img.xz
#
#_sign upgrade.uzip
