#!/bin/bash

# Copyright (C) 2015-2016 NIWA Hideyuki

FULCONDIR=/var/lib/fulcon
DOCKERFILEDIR=/var/lib/fulcon/driver/dockerfile
OCFDIR=/var/lib/fulcon/OCF
SLOTOS=slot-os

usage()
{
	echo "usage: setup2"
} 


setup_slot_image() 
{
	pushd  $FULCONDIR/setup/$SLOTOS/rootfs >& /dev/null
	echo "New OS image is generated from HOST."
	echo "It takes long time for this processing."
	echo "This processing must not interrupt or not turn off power."
	create-rootfs $SLOTOS
	tar -c . 2> /dev/null | docker import - fulcon/$SLOTOS >& /dev/null
	if [ $? -ne 0 ]; then
		echo "error: can't create a $SLOTOS image"
		popd >& /dev/null
		release-rootfs $SLOTOS
		exit -1
	fi
	popd >& /dev/null
	release-rootfs $SLOTOS
	trap 0
	echo "Generated new image : $SLOTOS"
}

mkdir -p  $FULCONDIR/setup/$SLOTOS/rootfs 

setup_slot_image

(docker rmi `docker images | grep '<none>' | awk '{printf "%s ",$3}'`) >& /dev/null
echo $BUILDRESULT | awk '{split($0,a,"*");for (i in a) print a[i]}'
echo 

DEFAULTIMAGE=`fulcon default-image`
docker images | egrep $DEFAULTIMAGE >& /dev/null
if [ $? -ne 0 ]; then
	DEFAULTIMAGE=""
fi

if [ x"$DEFAULTIMAGE" == x"" ]; then
	fulcon set-default-image "fulcon/$SLOTOS"
	echo "fulcon/"$SLOTOS "is set to the default image."
fi

exit 0

