#!/bin/bash 

#usage
function usage()
{
	echo "USAGE: pva-pp-postInstall [--nossl]" 1>&2
}

function make_sve_v6_address()
{
	local PATH="/bin:/sbin/:/usr/sbin/:/usr/bin/"
	local HN_IFACE=`ip route list | awk '$1 == "default" { for(i=2;i<NF;++i) { if($i == "dev") { print $(i + 1); exit } } }'`
	local V6_ADDRESS=""
	for i in $HN_IFACE; do
        	V6_ADDRESS=`ip link show ${i%@*} | awk '{if ("link/ether" == $1 && split($2,x,/:/) == 6){printf("fd7d:8d1c:d459:505d:%s%s:%sff:fe%s:%s%s\n",x[1],x[2],x[3],x[4],x[5],x[6]); exit}}'`
	        [ -n "${V6_ADDRESS}" ] && break
	done
	echo $V6_ADDRESS
}

function is_v6_enabled()
{
	local PATH="/bin:/sbin/:/usr/sbin/:/usr/bin/"
	local x=`sysctl -n net.ipv6.conf.venet0.disable_ipv6 2>/dev/null`
	[ "$x" == "0" ]
}

# create dummy service container that will proxy http requests to portal
function create_sve_proxy()
{
	is_v6_enabled
	local v6=$?
	IP_ADDRESS=`auto_assign_sve_address`
	if [ 0 -eq $v6 ]; then
		V6_ADDRESS=`make_sve_v6_address`
		if [ -z "${V6_ADDRESS}" ]; then
        		echo "Cannot make an IPv6 address for SVE. Don't start it"
		        exit 1
		fi
	fi
	echo "Create pp ve $PP_VE_PRIVATE $PP_INSTALL_DIR"

	INITDIR=`pwd`
	
	# Fix vzquota issue
	vzquota off $PP_VEID -f &> /dev/null 

	touch /etc/vz/dists/sve.conf || abort $? "Failed to create /etc/vz/dists/sve.conf"
	
	rm -rf $PP_VE_PRIVATE
	mkdir -p $PP_VE_PRIVATE/ || abort $? "Failed to create $PP_VE_PRIVATE/fs"
	mkvzfs $PP_VE_PRIVATE/fs || abort $? "Failed to create vzfs $PP_VE_PRIVATE/fs"
	mkdir -p $PP_VE_PRIVATE/scripts || abort $? "Failed to create $PP_VE_PRIVATE/scripts"
	ln -s  4 $PP_VE_PRIVATE/.ve.layout || abort $? "Failed to create $PP_VE_PRIVATE/.ve.layout"


	cp $PP_INSTALL_DIR/ve.conf $PP_VE_PRIVATE/ve.conf || abort $? "Failed to copy ve.conf"
	cp $PP_INSTALL_DIR/mount $PP_VE_PRIVATE/scripts/mount || abort $? "Failed to copy mount"
	vzctl register $PP_VE_PRIVATE $PP_VEID || abort $? "Failed to register $PP_VE_PRIVATE"

	# create /sbin 
	mkdir -p $PP_VE_PRIVATE/fs/root/sbin/ || abort $? "Failed to create $PP_VE_PRIVATE/fs/root/sbin/"
	cp $PP_INSTALL_DIR/init $PP_VE_PRIVATE/fs/root/sbin/init || abort $? "Failed to copy init"
	cd $PP_VE_PRIVATE/fs/root/sbin/
	ln init halt || { echo "Failed to create halt link";}
	ln init reboot || { echo "Failed to create reboot link";}

	# create /dev/null
	mkdir -p $PP_VE_PRIVATE/fs/root/dev || abort $? "Failed to create $PP_VE_PRIVATE/fs/root/dev"
	cd $PP_VE_PRIVATE/fs/root/dev
	mknod null c 1 3 || abort $? "Failed to create /dev/null"

	cd $INITDIR

	# set ip
	if [ 0 -eq $v6 ]; then
		vzctl set $PP_VEID --ipadd $IP_ADDRESS --ipadd $V6_ADDRESS --save
	else
		vzctl set $PP_VEID --ipadd $IP_ADDRESS --save
	fi
	[ 0 -eq $? ] || abort $? "Failed to assign IP address to Service Container"

	# Fix vzquota issue
	vzctl quotainit $PP_VEID

	vzctl start $PP_VEID || abort $? "Failed to start Service Container"
}

###################### Include common files #######################

. ${0/%pva-pp-*/pva-pp-functions.sh} || { echo "Failed to load hint functions"; exit -1;}

############################### Go  ##############################

echo "------------------------------------------------------------------------------------------------"
date "+%b %e %T PVA Power Panel postinstall configuration : start"

noSSL=

# parse options
eval set -- $* --
while true ; do
	case "$1" in
	    --help)		usage; exit 0 ;;
	    --nossl) 	noSSL="--nossl" ; shift ;;
	    --)		shift ; break ;;
	    *) 		shift ;;
	esac
done

postinstall $noSSL || abort $? "Power Panel postinstall configuration failed"

# create proxy sve
create_sve_proxy || abort $? "Failed to create Service Container"

date "+%b %e %T PVA Power Panel postinstall configuration : stop"
echo "------------------------------------------------------------------------------------------------"

exit 0

