#!/usr/bin/env bash
# Afrihost - Script to restore from graveyardmtn
# Date: 2020/02/07
# Date Modified: 7 Sept 2022
# Modified by: Juan.vz

txtgrn='\e[0;32m'       # green
txtund=$(tput sgr 0 1)  # Underline
txtbld=$(tput bold)     # Bold
txtrst='\e[0m'          # Text reset
bldgrn='\e[1;32m' # Green
bldblu='\e[1;34m' # Blue
bldwht='\e[1;37m' # White
bldred='\e[1;31m' # Red

CPUSER_input=$2
CPSOURCE_input=$3
CURRENTHOST=`hostname`

get_graveyard_list () {
        echo -e "\nSearching graveyard for $CPUSER"
        if echo  "find  /backup/graveyard/*/*"$CPUSER"* -exec ls -altrh {} \; " | ssh -q graveyard@graveyard.afrihost.com ; then 
                echo -e ":::Found cpmove/afrimove files on graveyard :::\n"
                echo -e ":: To restore any of the found CPMOVES, run:"
                echo -e $bldgrn"# graveyard_restore restore [username] [sourceserver]"$txtrst
                echo -e ":: To restore any of the found AFRIMOVES, run:"
                echo -e $bldgrn"# graveyard_restore copyonly [username] [sourceserver],
# then /usr/local/afribin/afrimove_restoreacc [/home/.graveyardtmp/afrimove*.tar.gz]"$txtrst
                else 
                echo -e "::: NOT FOUND - no cpmove or afrimove for user found on the entire graveyard. Contact server-admin to have a look as to why not :::\n"
        fi
}

run_restore () {
if ls -1 /home/cpmove-"$CPUSER".* ; then rm -rfv  /home/cpmove-"$CPUSER".* ; fi
rsync -avP graveyard@graveyardmtn.afrihost.com:/backup/graveyard/"$CPSOURCE"/*"$CPUSER"* /home/

if ls -1 /home/afrimove-"$CPUSER".* | grep "afrimove" ; then
        echo -e ${bldred}"\n/usr/local/afribin/graveyard_restore can not restore afrimove files."${txtrst}
        echo -e ${bldgrn}"Run: /usr/local/afribin/afrimove_restoreacc [/path/to/afrimove_file.tar.gz]\n"${txtrst}
        exit
fi

echo -e "\ncpmove copied to `ls -latrh /home/cpmove-"$CPUSER"*`"
echo -e "\nRestoring package now"
echo "/scripts/restorepkg --force `ls -1 /home/cpmove-"$CPUSER"*`"  | bash
rm -fv `ls -1 /home/cpmove-"$CPUSER"*`
telegram-send "$(hostname) - graveyard_restore "$CPUSER""
}

run_copyonly () { 
graveyard_restore list  "$CPUSER"
if [ -f  /home/cpmove-"$CPUSER".* ] ; then rm -rfv  /home/cpmove-"$CPUSER".* ; fi 
  if [ ! -d "/home/.graveyardtmp" ]; then
        mkdir -p "/home/.graveyardtmp" ;
  fi
if [ -f  /home/.graveyardtmp/cpmove-"$CPUSER".* ]  ; then rm -rfv /home/.graveyardtmp/cpmove-"$CPUSER".* ; fi
rsync -avP graveyard@graveyardmtn.afrihost.com:/backup/graveyard/"$CPSOURCE"/*"$CPUSER"* /home/.graveyardtmp 2>/dev/null
if [ -f  /home/.graveyardtmp/*move-"$CPUSER".* ]  ; then  
        echo -e "\ncpmove or afrimove file copied to `ls -latrh /home/.graveyardtmp/*move-"$CPUSER"*`"
else 
        echo -e "\nNo cpmoves, afrimoves or backups found on graveyardmtn for "$CPUSER"*"
fi
}

restore_from_grave () {
#if echo "$CPUSER" | grep "afrimove" ; then
#        echo "/usr/local/afribin/graveyard_restore can not restore afrimove files."
#        echo "Run: /usr/local/afribin/graveyard_restore [/path/to/afrimove_file.tar.gz]"
#        exit
#fi

if  grep "$CPUSER" /etc/userdomains  ; then
 echo "!!! NOTE THIS DOMAIN/USER ALREADY EXISTS !!!"
 while true; do
      read -p "Are you sure you would like to overwrite this existing account/user??[y/n]" yn
      case $yn in
          [Yy]* ) run_restore ; echo "Account restored" ;  exit ;; 
          [Nn]* ) echo "Maybe that was a good choice :)" ;exit;;
          * ) echo "Please answer yes or no.";;
      esac
     done
 exit
 else
 run_restore
fi
}

usercheck () {
if  echo "$CPUSER_input" | /usr/local/afribin/domaingrep ; then
         CPUSER=$(grep "$CPUSER_input" /etc/trueuserdomains | awk '{print $NF}')
 elif 
         awk '{print $NF}' /etc/trueuserdomains | grep "$CPUSER_input" ; then 
         CPUSER=""$CPUSER_input""
         echo "CPUSER = $CPUSER"
 else
        echo "Supplied 2nd option is not a cpanel domain or user on this server"
fi
}

usage_short() {
clear
cat <<EOF

`echo -e $bldblu"@@@@@@@@@@@@@@@@@@@@@@ . .@@@@@@
@@@@@@@@@ ...... @@@@ ....  *@@@
@@@@@@%....... ....(  @@@@@** @@
@@@@@..  @@@@@@@&  . @@@@@@@@( @
@@@@.. @@ &@@@@& @@ . @@@@@@@@@(
@@@.. &@@@@@@@@@@@@, .@/@@@@@@@@
@@@@ .  &@@----@@@  ./@@@@@@@@@@
@@@@@ .  @@@&(@@#. . @@@@@@@@@@@
@@@@(..... ..........@@@@@@@@@@@
@@@,........ ... .....@@@@@@@@@@
@@@@@&...... .....  @@@@@@@@@@@@
@@@@@...............@@@@@@@@@@@@
@@@ .....      ......%%%@@@@@@@@
%%%%%% .......... #%%%%%@@@@@@@@"$txtrst`


::: /usr/local/afribin/graveyard_restore  - Restore account from graveyard :::

Usage:  This tool should be run on the target server where the domain is currently pointing to, unless you have other plans:
        
        Step 1: find all possible cpmoves on graveyard for a particular user:
        `echo -e $bldgrn"# graveyard_restore list [username]"$txtrst`

        Step 2: choose the one you'd like to be restored/copy by specifying the source hostname.
        `echo -e $bldgrn"# graveyard_restore restore [username] [sourceserver]"$txtrst`

        Restore Example: cpmove from ul.aserv.co to restore to sonic.aserv.co.za
        `echo -e $bldblu"# graveyard_restore restore cpanelusername ul.aserv.co.za"$txtrst`  
        
        CopyOnly example: cpmove from ul.aserv.co to copy to temp location on sonic.aserv.co.za
        `echo -e $bldblu"# graveyard_restore copyonly cpanelusername ul.aserv.co.za"$txtrst`

        
        If you need help:
        # contact server-admin

EOF
}


if [ -z "$1" ]; then
        usage_short
else
 while [ -n "$1" ]; do
 case "$1" in
        list)
                if ! [ "$2" ] ; then
                echo "Need a username/domain"
                exit 1
                fi
                CPUSER="$2"
                #if !  grep "$CPUSER" /etc/userdomains || ! grep $CPUSER ; then
                #        echo "not a valid user/dom to this system"
                #        exit
                #fi
                get_graveyard_list 
                exit 0
        ;;
        restore)
                if ! [ "$2" ] ; then
                echo "Need a username/domain"
                exit 1
                fi
                CPUSER="$2"
                #if !  grep "$CPUSER" /etc/userdomains ; then
                #        echo "not a valid user/dom to this system"
                #        exit
                #fi
                if ! [ "$3" ] ; then
                        echo "Need the source server from graveyard"
                        exit 1
                fi
                CPSOURCE=$3
                if !  echo "$CPSOURCE" | egrep "aserv.co.za|aserv.email|aserv.site" ; then
                        echo "The source server you've specified is not a shared server, please try again."
                        exit
                fi
                restore_from_grave
       ;;
       copyonly)
                if ! [ "$2" ] ; then
                echo "Need a username/domain"
                exit 1
                fi
                CPUSER="$2"
                #if !  grep "$CPUSER" /etc/userdomains ; then
                #        echo "not a valid user/dom to this system"
                #        exit
                #fi
                if ! [ "$3" ] ; then
                        echo "Need the source server from graveyard"
                        exit 1
                fi
                CPSOURCE=$3
                if !  echo "$CPSOURCE" | egrep "aserv.co.za|aserv.email|aserv.site" ; then
                        echo "The source server you've specified is not a shared server, please try again."
                        exit
                fi
                run_copyonly
        ;;
        esac
        shift
        done
fi
