#!/bin/bash 

# Fulcon/slot-os Virtual-Platform
# Copyright (C) 2016 NIWA Hideyuki

# check root
if [ ${EUID:-$UID} -ne 0 ] ; then
  echo "error: not executing without superuser privileges."
  exit 1
fi

export PATH=/usr/lib/slot-os/sbin:/usr/lib/fulcon/sbin:$PATH
export FULCONDIR=/var/lib/fulcon
export SLOTOSDIR=/var/lib/slot-os

mkdir -p /var/lib/slot-os

if [ x"`fulcon ls-image | awk '$1=="fulcon/slot-os"{print $1}'`" != x"fulcon/slot-os" ]; then
  slot-os make-base-image
fi

FLG_H=0

while getopts h OPT
do
  case $OPT in
    "h" ) FLG_H=1 ;;
  esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ] ; then
	less /usr/lib/slot-os/doc/help.txt
else
  if [ -f /usr/lib/slot-os/sbin/$1 ]; then
    EXLINE=`echo /usr/lib/slot-os/sbin/$* | sed -e 's/\$//g' -e 's/\`//g' -e "s/'//g" -e 's/\"//g' -e 's/;//g' -e 's/&//g' -e 's/|//g'`
    . $EXLINE
  else
    echo "error: unknown command:" $1
  fi
fi

exit 0
