#!/bin/bash
# copyright (C) 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 distro
DISTRO=`/usr/lib64/lxcf/lxcf-distro`

# check root
if [ ${EUID:-${UID}} != 0 ]; then
    echo "error: Because you are not root, you cannot execute this command. "
    exit 1
fi

# check option
FLG_F=0
FLG_P=0
SNAPPATH="."

while getopts fp: OPT ; do
  case $OPT in
  f) FLG_F=1 ;;
  p) FLG_P=1 ; SNAPPATH=$OPTARG ;;
  esac
done
shift $((OPTIND - 1))

# check args
if [ $# -lt 1 ]; then
	echo "usage: lxcf snapshot [ -f ] [ -p PATH ] {(LXCF_UUID | NAME) ...}"
	exit 1
fi

lsdir() {
  ls -f --ind=none $1 | sed '/^\.\{1,2\}$/d'
}

# When uuid is specified, the container name is returned. 
findname() {
  NAME=$1
  if [ -d /opt/lxcf/${NAME} ]; then
    echo -n $NAME
    return
  fi
  for i in `lsdir /etc/lxcf/rsc`
  do
     if [ x$NAME == x`cat /etc/lxcf/rsc/${i}/uuid` ]; then
       echo -n $i
       return
     fi
  done
  echo -n ""
}

LXCNAME=`findname ${1}`
if [ -z $LXCNAME ]; then
  echo "error:" $1 "is the unknown container name"
  exit -1
fi

LXCF_UUID=`cat /etc/lxcf/rsc/${LXCNAME}/uuid`

if [ ! -d ${SNAPPATH} ] ; then
  echo "error:" $SNAPPATH "is the unknown path"
  exit -1
fi

CONTPATH=`cd $SNAPPATH;pwd`

if [ -e "${CONTPATH}/${LXCF_UUID}_${LXCNAME}.img" -a $FLG_F -eq 0 ]; then
	echo "error: file exists :" ${LXCF_UUID}_${LXCNAME}.img
	exit 1
fi

# take the snapshot
LPWD=$PWD

mkdir -p ${CONTPATH}/${LXCF_UUID}
trap '/bin/rm -rf ${CONTPATH}/${LXCF_UUID} ${CONTPATH}/${LXCF_UUID}_${LXCNAME}.img  ' 0

cd /opt/lxcf/$LXCNAME
/usr/bin/tar czf ${CONTPATH}/${LXCF_UUID}/${LXCF_UUID} * >& /dev/null
cd /opt/lxcf
cp -pr /etc/lxcf/rsc/${LXCNAME}  ${CONTPATH}/${LXCF_UUID}/
echo -n ${LXCNAME} > ${CONTPATH}/${LXCF_UUID}/name
LANG=C yum list installed | \
	awk 'BEGIN{f=0}{if(f==1)print $1;if($1=="Installed")f=1}' \
		>  ${CONTPATH}/${LXCF_UUID}/pkg.info


# The argument of the remainder is processed. 
cd ${CONTPATH}/${LXCF_UUID}
shift 1
SARG=$@
for i in $SARG
do
  ARG_NAME=`findname $i`
  if [ -z $ARG_NAME ]; then
    echo "error:" $i "is the unknown container name"
    exit -1
  fi

  echo -n " ${ARG_NAME}" >> name
  cp -pr /etc/lxcf/rsc/${ARG_NAME}  .

  ARG_UUID=`cat /etc/lxcf/rsc/${ARG_NAME}/uuid`

  /usr/lib64/lxcf/lxcf-snapshot-cluster /opt/lxcf/$LXCNAME /opt/lxcf/$ARG_NAME $ARG_UUID

done

cd ${CONTPATH}

/usr/bin/tar czf "${LXCF_UUID}_${LXCNAME}.img" ${LXCF_UUID} >& /dev/null


trap 0
rm -rf ${LXCF_UUID}

cd $LPWD

exit 0


