#!/bin/sh
# copyright (C) 2013-2014 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
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 ; JSONFILE=$OPTARG;;
    "p" ) FLG_P=1 ; FILEPATH=$OPTARG
	if ! [ -d $FILEPATH ] ; then
		echo $FILEPATH "is not a directory"
		exit 1
	fi
	;;
  esac
done

shift `expr $OPTIND - 1`

# check args
if [ $# -lt 1 ]; then
	echo "usage lxcf sysgen [-s][-j][-c configfile] LXCNAME"
	exit 1
fi

# check jsonfile
if [ ${FLG_C} -eq 1 ] ; then
  /usr/lib/lxcf/lxcf-check-json ${JSONFILE}
  if [ $? -ne 0 ] ; then
    echo "illegal format json file :" ${JSONFILE}
    exit 1
  fi
fi


# generate one container
lxcf_sysgen1() {
  LXCNAME=${1}

  VNAME=`virsh list --all | \
    awk '{if ((NR > 2) && (NF == 3) && ($2 == "'${LXCNAME}'")) print $2}'`
  if [ x${LXCNAME} == x${VNAME} ] ; then
    echo "The same VM name already exists:" ${LXCNAME}
    return
  fi

  LNAME=`virsh -c lxc:/// list --all | \
    awk '{if ((NR > 2) && (NF == 3) && ($2 == "'${LXCNAME}'")) print $2}'`

  if [ x${LXCNAME} == x${LNAME} ] ; then
    echo "The same LXC name already exists:" ${LXCNAME}
    return
  fi

  if ! /usr/lib/lxcf/lxcf-parmchk-cname $LXCNAME ; then
    echo $LXCNAME "is not a container name"
    exit 1
  fi

  PREFIX=/opt/lxcf/${LXCNAME}

  # check LXCNAME
  if [ -e /${PREFIX} ] ; then
	echo "There is already " ${LXCNAME}
	exit 1
  fi 

  if ! [ -e /etc/hosts ] ; then
     echo "127.0.0.1	localhost" > /etc/hosts
  fi

  echo "Generate" ${LXCNAME}

  # set symbolic link for -p option
  if [ $FLG_P -eq 1 ] ; then
    mkdir -p ${FILEPATH}/${LXCNAME}
    ln -s  ${FILEPATH}/${LXCNAME} ${PREFIX}
  fi

  # main operation
  /usr/lib/lxcf/lxcf-createfile ${VAL_SEPARATE} ${LXCNAME}
  /usr/lib/lxcf/lxcf-setup      ${VAL_SEPARATE} ${LXCNAME}
  /usr/lib/lxcf/lxcf-define     ${LXCNAME}

  # disable lxcf.service
  rm /opt/lxcf/${LXCNAME}/etc/systemd/system/multi-user.target.wants/lxcf.service

  # create an initial rsc file
  /usr/lib/lxcf/lxcf-resource1 initrsc ${LXCNAME} > /etc/lxcf/rsc/${LXCNAME}/resource.val

  /usr/lib/lxcf/lxcf-start ${LXCNAME}

  # create known_hosts entry
  sed -i "/^${LXCNAME}[ |,]/d"  /root/.ssh/known_hosts
  ssh-keyscan localhost |& egrep "ssh-rsa" >> /root/.ssh/known_hosts
  LXCIPADR=`awk '{if ($2 == "'${LXCNAME}'") printf "%s",$1}' /etc/hosts`
  sed -i "s/localhost/${LXCNAME},${LXCIPADR}/" /root/.ssh/known_hosts 

  # set resource 
  if [ $FLG_C -eq 1 ] ; then
    echo "load json file :" $JSONFILE
    /usr/lib/lxcf/lxcf-load $LXCNAME $JSONFILE
  fi
  #/usr/lib/lxcf/lxcf-resource show ${LXCNAME}
}

# erase yum chache
yum clean packages >& /dev/null &
yum clean all >& /dev/null &

# generate containers of args
for i in $*
do
  lxcf_sysgen1 $i 
done


exit 0

