#!/bin/bash

# Copyright (C) 2016 NIWA Hideyuki

FULCONDRIVER=`fulcon driver-name`
PATH=/usr/lib/slot-os/lib:/usr/lib/fulcon/driver/$FULCONDRIVER:/usr/lib/fulcon/lib:$PATH

SLOTOSDIR=/var/lib/slot-os

usage()
{
	echo "usage: update-deploy NUMBER ... : SRC_FILE DEST_PATH"
}

lsdir() {
  ls -f --ind=none $1 | sed '/^\.\{1,2\}$/d'
}

if [ $# -lt 1 ]; then
	usage
	exit -1
fi

if [ x"$1" == x"update-deploy" ]; then
	usage
	exit -1
fi

ARGS=$*

NUMS=`echo $ARGS | cut -d ':' -f 1`
FILES=`echo $ARGS | cut -d ':' -f 2`

if [ x"$NUMS" == x"$FILES" -o x"$NUMS" == x"" -o x"" == x"$FILES" ]; then
	usage
	exit -1
fi

if [ `echo $FILES | awk '{print NF}'` -ne 2 ]; then
	usage
	exit -1
fi

SRC=`echo $FILES | cut -d ' ' -f 1`
DIST=`echo $FILES | cut -d ' ' -f 2`

for i in $NUMS
do
	if [ x"$i" == x"all" ]; then
		for j in `slot-os list | awk '{print $4}'`
		do
			fulcon update-deploy -c $j $SRC $DIST
		done
		exit 0
	fi

	if [ -f $SLOTOSDIR/slot/$i/fulcon ]; then
		NAME=`cat $SLOTOSDIR/slot/$i/fulcon`
		fulcon update-deploy -c $NAME $SRC $DIST
	else
		echo $i is not exist
	fi
done

