#!/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 /opt/lxcf
mkdir -p /opt/lxcf
chmod 755 /opt/lxcf

# check /etc/lxcf/rsc
mkdir -p /etc/lxcf/rsc
chmod 755 /etc/lxcf
chmod 755 /etc/lxcf/rsc

# check lxcf.conf
if [ ! -e /etc/lxcf/lxcf.conf ] ; then
  cp -p /usr/lib/lxcf/lxcf.conf /etc/lxcf/lxcf.conf
fi

# 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 ;;
  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 "error: illegal format json file :" ${JSONFILE}
    exit 1
  fi
fi

# check -p path
if [ ${FLG_P} -eq 1 ] ; then
  if [ ! -d $FILEPATH ] ; then
    echo "error:" $FILEPATH "is not a directory"
    exit 1
  fi
  if echo $FILEPATH | grep "^/opt/lxcf" ; then
    echo "error:" $FILEPATH "overlaps with passing other containers. "
    echo "      " $FILEPATH "contains /opt/lxcf."
    exit 1
  fi
  for i in /opt/lxcf/*
  do
    apath=`readlink -f $i`
    if echo $FILEPATH | grep "^${apath}" ; then
      echo "error:" $FILEPATH "overlaps with passing other containers. "
      echo "      " $FILEPATH "contains $apath."
      exit 1
    fi    
  done
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 "error: 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 "error: The same LXC name already exists:" ${LXCNAME}
    return
  fi

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

  PREFIX=/opt/lxcf/${LXCNAME}

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

  echo "Generate" ${LXCNAME}
  trap '/usr/lib/lxcf/lxcf-erase ${LXCNAME};exit 1' 2

  # 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 &

# check /etc/hosts
if [ ! -e /etc/hosts ] ; then
  echo "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4" > /etc/hosts
  echo "::1         localhost localhost.localdomain localhost6 localhost6.localdomain6" >> /etc/hosts
fi

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


exit 0

