#!/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.

FLG_S="FALSE"

while getopts js OPT
do
  case $OPT in
    "s" ) FLG_S="TRUE" ; MODE="-s" ;;
    "j" ) FLG_S="FALSE" ; MODE="-j" ;;
  esac
done

shift `expr $OPTIND - 1`


if [ $# -ne 1 ]; then
	echo "usage lxcf-createfile [-j] [-s] LXCNAME"
	exit 1
fi

LXCNAME=${1}
VAL_PREFIX="/opt/lxcf"

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


# create new lxcf management dir
mkdir -p /etc/lxcf/rsc/${LXCNAME}

# create root dir
echo "create "${VAL_PREFIX}/${LXCNAME}
mkdir -p ${VAL_PREFIX}/${LXCNAME}

# create /etc and lxcf config files
echo "create "${VAL_PREFIX}"/${LXCNAME}/etc"
#rsync -a --inplace --exclude='lxcf/' /etc/ ${VAL_PREFIX}/${LXCNAME}/etc 
cp -a /etc ${VAL_PREFIX}/${LXCNAME}/etc 
mkdir -p ${VAL_PREFIX}/${LXCNAME}/etc/lxcf/rsc/${LXCNAME}

# create /run
echo "create "${VAL_PREFIX}/${LXCNAME}"/run"
mkdir -p ${VAL_PREFIX}/${LXCNAME}/run

# create /opt
# echo "create "${VAL_PREFIX}/${LXCNAME}"/opt"
# rsync -a --inplace --exclude='lxcf/' /opt/ ${VAL_PREFIX}/${LXCNAME}/opt 
mkdir -p ${VAL_PREFIX}/${LXCNAME}/opt 

# create /boot
echo "create "${VAL_PREFIX}/${LXCNAME}"/boot"
#rsync -a --inplace --exclude='vmlinuz*' --exclude='initramfs*' /boot/ ${VAL_PREFIX}/${LXCNAME}/boot &
cp -a /boot ${VAL_PREFIX}/${LXCNAME}/boot 

# create /var
echo "create "${VAL_PREFIX}/${LXCNAME}"/var"
#rsync -a --inplace --exclude='images/*' /var/ ${VAL_PREFIX}/${LXCNAME}/var &
mkdir -p ${VAL_PREFIX}/${LXCNAME}/var/lib/libvirt
chmod 755 ${VAL_PREFIX}/${LXCNAME}/var
chmod 755 ${VAL_PREFIX}/${LXCNAME}/var/lib
chmod 755 ${VAL_PREFIX}/${LXCNAME}/var/lib/libvirt
echo -n '.'

# /var/lib/images/* is not copied. 
for i in `ls -1 /var`
do
  if [ $i == "lib" ] ; then
    for j in `ls -1 /var/lib`
    do
      if [ $j == "libvirt" ] ; then
        for k in `ls -1 /var/lib/libvirt`
        do
          if [ $k != "images" ] ; then
            cp -a /var/lib/libvirt/$k ${VAL_PREFIX}/${LXCNAME}/var/lib/libvirt/$k
          fi
        done
      else
        cp -a /var/lib/$j ${VAL_PREFIX}/${LXCNAME}/var/lib/$j
      fi
    done
    echo -n '.'
  else
    cp -a /var/$i ${VAL_PREFIX}/${LXCNAME}/var/$i
    echo -n '.'
  fi
done
echo 

if [ ${FLG_S} = "TRUE" ] ; then
	# create /usr
	echo "create "${VAL_PREFIX}/${LXCNAME}"/usr"
	#rsync -al --inplace /usr/ ${VAL_PREFIX}/${LXCNAME}/usr &
	mkdir -p ${VAL_PREFIX}/${LXCNAME}/usr
	chmod 755 ${VAL_PREFIX}/${LXCNAME}/usr
	for i in `ls -1 /usr`
	do
#		echo "        " /usr/$i
		echo -n "......"
		cp -a /usr/$i ${VAL_PREFIX}/${LXCNAME}/usr/$i 
	done 
	echo
fi

wait

cp /dev/null ${VAL_PREFIX}/${LXCNAME}/var/log/messages
rm -rf ${VAL_PREFIX}/${LXCNAME}/var/tmp/kdecache-root
rm -f ${VAL_PREFIX}/${LXCNAME}/var/lib/libvirt/images/*
rm -rf ${VAL_PREFIX}/${LXCNAME}/var/log/audit/*
touch ${VAL_PREFIX}/${LXCNAME}/var/log/audit/audit.log
chmod 600 ${VAL_PREFIX}/${LXCNAME}/var/log/audit/audit.log
rm -rf ${VAL_PREFIX}/${LXCNAME}/var/tmp/abrt/*


mkdir -p ${VAL_PREFIX}/${LXCNAME}/root
cp /root/.bashrc ${VAL_PREFIX}/${LXCNAME}/root/.
cp /root/.bash_profile ${VAL_PREFIX}/${LXCNAME}/root/.

# create authorized_keys
# echo "create authorized_keys"
mkdir -p ${VAL_PREFIX}/${LXCNAME}/root/.ssh
chmod 700 ${VAL_PREFIX}/${LXCNAME}/root/.ssh/
cp /root/.ssh/lxcf_rsa.pub ${VAL_PREFIX}/${LXCNAME}/root/.ssh/authorized_keys
chmod 600 ${VAL_PREFIX}/${LXCNAME}/root/.ssh/authorized_keys

# echo "created files " ${LXCNAME}

exit 0

