#!/bin/sh
# copyright (C) 2013 FUJITSU LIMITED All Rights Reserved

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301, USA.

# check default separate mdoel
/usr/lib/lxcf/lxcf-config
if [ -e /etc/lxcf/separate ] ; then
  FLG_S=1
  VAL_SEPARATE="-s"
else
  FLG_S=0
  VAL_SEPARATE=""
fi

# check options
FLG_C=0
VAL_CONFIG=""
VAL_PATH=""
FLG_P=0
JSONFILE=""
FILEPATH=""

while getopts sjc:p: OPT
do
  case $OPT in
    "s" ) FLG_S=1 ; VAL_SEPARATE="-s" ;;
    "j" ) FLG_S=0 ; VAL_SEPARATE="-j" ;;
    "c" ) FLG_C=1 ; VAL_CONFIG="-c" ; JSONFILE=$OPTARG ;;
    "p" ) FLG_P=1 ; VAL_PATH="-p" ;   FILEPATH=$OPTARG ;;
  esac
done

shift `expr $OPTIND - 1`


if [ $# -ne 2  -a  $# -ne 3 ] ; then
  echo "usage : sysgen-n [ -p path ] [ -c resourcefaile ] NAME NUM"
  echo "        sysgen-n [ -p path ] [ -c resourcefaile ] NAME NUM1 NUM2"
  exit -1
fi

MAXNUM=50
LNAME=$1

if [ $# -eq 3 ] ; then
  NUM1=$2
  if ! expr "$NUM1" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM1
    exit -1
  fi

  NUM2=$3
  if ! expr "$NUM2" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM2
    exit -1
  fi
else
  NUM1=1
  NUM2=$2
  if ! expr "$NUM2" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM2
    exit -1
  fi
fi

if ! /usr/lib/lxcf/lxcf-parmchk-num $NUM1 ; then
  echo $NUM1 "is not a integer"
  exit 1
fi

if ! /usr/lib/lxcf/lxcf-parmchk-num $NUM2 ; then
  echo $NUM2 "is not a integer"
  exit 1
fi

if [ $NUM1 -le 0 -o $NUM1 -gt $MAXNUM ]; then
  echo "illegal range :" $NUM1
  exit -1
fi

if [ $NUM1 -ge $NUM2 -o $NUM2 -le 0 -o $NUM2 -gt $MAXNUM ]; then
  echo "illegal range :" $NUM2
  exit -1
fi

for i in `seq $NUM1 $NUM2`
do
  /usr/lib/lxcf/lxcf-sysgen ${VAL_SEPARATE} ${VAL_CONFIG} ${JSONFILE} ${VAL_PATH} ${FILEPATH} ${LNAME}`printf "%04d" ${i}` 
done

exit 0

