#!/usr/bin/env bash

#################################
#     ISPConfig Copy Tool       #
#     (c) 2017 by ISPConfig UG  #
#     all rights reserved       #
#################################

CDIR="$PWD" ;
SELF=`readlink $0` ;
if [[ "$SELF" == "" ]] ; then
	SELF="$0" ;
fi
SYSTYPE=`uname` ;
if [[ "$SYSTYPE" == "FreeBSD" ]] ; then
	DIR=$(realpath $(dirname ${SELF})) ;
else
	DIR=`readlink -e $(dirname ${SELF})` ;
fi
ARCH=`uname -m` ;
if [[ "$ARCH" == "amd64" ]] ; then
	ARCH="x86_64" ; 
fi

FIRSTARG="$1"

NO_OUT="0"
for tmpv in "$@" ; do
	if [[ "$tmpv" == "--autoconf" ]] ; then
		NO_OUT="1" ;
	fi
done


if [[ "$PHP" == "" ]] ; then
	PHP=`which ispcphp` ;
	if [[ "$PHP" == "" ]] ; then
		if [[ "$NO_OUT" != "1" ]] ; then
			echo "Missing PHP binary!" ;
		fi
		exit 1;
	fi
fi

if [[ "$(id -u)" != "0" ]] ; then
	if [[ "$NO_OUT" != "1" ]] ; then
		echo "Please run ISPConfig Copy Tool as root.";
	fi
	exit 1;
fi

timmeusercheck="$(echo $SUDO_USER | grep -E 'timme[g]?[0-9]{3}')"
if [[ ${timmeusercheck} == "" ]]; then
    echo "Not logged in as timmexxx user."
    echo "You need to log out and login again using 'ssh -A timmexxx@$(hostname -f)'"
    exit 1
fi

agentcheck="$(ssh-add -l 2&> /dev/null)"; agenterr=$?
## Checking if user is connected using a ssh agentcheck
if [[ ${agenterr} != '0' ]]; then
    echo "SSH Agent missing!"
    echo "You need to log out and login again using 'ssh -A $(logname)@$(hostname -f)'"
    exit 1
fi

PHPVERSION=`$PHP -v 2>/dev/null | head -n 1 | awk '{print $2}' | awk -F'.' '{print $1"."$2}'` ;

if [[ "$PHPVERSION" == "" ]] ; then
	if [[ "$NO_OUT" != "1" ]] ; then
		echo "Could not get PHP version.";
	fi
	exit 1;
fi

rm -f ${DIR}/ispcopy_php.ini ;

echo "disable_functions=" >> ${DIR}/ispcopy_php.ini
echo "max_execution_time=0" >> ${DIR}/ispcopy_php.ini
echo "memory_limit=2048M" >> ${DIR}/ispcopy_php.ini

for M in "mcrypt" "mysqlnd" "mysqli" "simplexml" "mbstring" ; do
	SUPP="" ;
	if [[ "$M" == "mbstring" ]] ; then
		SUPP="${M}[[:space:]]+extension" ;
	elif [[ "$M" != "mysqlnd" ]] ; then
		SUPP="${M}[[:space:]]+support.+enabled" ;
	else
		SUPP="${M}[[:space:]]+.+enabled" ;
	fi
	
	MODCHK=$($PHP -i 2>/dev/null | grep -E -i "${SUPP}");
	if [[ "$MODCHK" != "" ]] ; then
		MODCHK=$($PHP -n -c ${DIR}/ispcopy_php.ini -i 2>/dev/null | grep -E -i "${SUPP}");
		if [[ "$MODCHK" == "" ]] ; then
			echo "extension=${M}.so" >> ${DIR}/ispcopy_php.ini
		fi
	fi
done

mkdir /root/ispcopy > /dev/null 2>&1
if [ $(cat /etc/debian_version | cut -d'.' -f 1) -gt 9 ]
then
  screen -L -Logfile /root/ispcopy/ispcopy_screen_$(date +%Y%m%d-%H%M).log -S ispcopy $PHP -n -c ${DIR}/ispcopy_php.ini -q ${DIR}/isp_copy.php "$@";
else
  screen -L /root/ispcopy/ispcopy_screen_$(date +%Y%m%d-%H%M).log -S ispcopy $PHP -n -c ${DIR}/ispcopy_php.ini -q ${DIR}/isp_copy.php "$@";
fi
rsync -avhP --chown=root:root /root/ispcopy/ "$timmeusercheck"@$(grep "target_server_hostname" /root/ispcopy/ispcopy_input.ini | cut -d'=' -f2):/root/ispcopy/ --rsync-path="sudo rsync" --exclude='root_access.cnf'
echo "Copied log files to $(grep "target_server_hostname" /root/ispcopy/ispcopy_input.ini | cut -d'=' -f2):/root/ispcopy"
#$PHP -n -c ${DIR}/ispcopy_php.ini -q ${DIR}/isp_copy.php "$@";
RET=$? ;

cd $CDIR ;
exit $RET ;




