#!/bin/bash 
    mail="mail_placeholder"
    user="user_placeholder"
    type="type_placeholder"
    ticket="ticket_placeholder"


    function check_screen(){
        if [[ $(screen -ls | grep -c $1) -ge 1 ]]; then
            echo "Screen is already running... exiting";
            exit;
        fi
    }

    if [ ! -e /root/bin/sshrestore ]; then
        echo "Datacycle Backup not enabled on server";
        exit;
    else
    
        var=$(/root/bin/sshrestore -l -u $user);
        if [[ $var == "No backups found for $user!" ]]; then
            echo "No backups found for $user!";
            exit;
        fi
    fi;

    dat=$(date +%Y-%m-%d)
    path=$(echo $var | awk '{ printf "%s\n%s\n%s\n%s",$3,$9,$15,$21}' | grep $type | cut -d ':' -f1);
    echo "Selected backup: ${path}";
    echo "Starting archive... Info will be sent to email. Please check Junk email folder in not recieved"
    echo "[ WARNING ]: Mail function will not work as of now due to cloudmark blocking..save link and password. link will work once backup gets generated"

    userinfo=$(whmapi1 listaccts searchmethod=exact searchtype=user search=$user)
    is_suspended=$(whmapi1  listsuspended | grep ${user} | wc -l)
    is_deleted=$(echo "$userinfo" | grep -c "No accounts found")
    check_screen ${user}
    pass=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c10; echo '');
    
   if [[ "$is_deleted" -eq 1 ]]; then
    
    echo "[ Warning ]: User is DELETED... "
    echo; echo; echo "------------------------------------------------------------------"

    if ps aux | grep "/root/bin/sshrestor[e] --user=${user}" ; then
        echo "Backup is already running for ${user}. Please wait for sometime and try again."
        exit 0
    fi

    screen -dmS backupgen_${user} bash -c " /etc/profile ; . ~/.bash_profile ; /root/bin/sshrestore --user=${user} --type=${type} --compressed --zip --password=${pass} --force --deleted --shareable; /bin/echo -e \"Backup for User: ${user} is generated. Password: ${pass} \\n link: \$(hostname -i)/${user}_${type}_deleted_bkp.zip\" | /usr/bin/mail -s \"Backup generated for ${user}\" ${mail} ;"
    echo "Backup is running on screen backupgen_${user}. You will recieve a mail after backup is generated.";
    echo -e "Link: http://$(hostname -i)/${user}_${type}_deleted_bkp.zip "
    echo -e "Password: $pass"
    sleep 10;
    exit 0
    

   else
    
    if ps aux | grep "/root/bin/sshrestor[e] --user=${user}" ; then
        echo "Backup is already running for ${user}. Please wait for sometime and try again."
        exit 0
    fi

    echo "User is present. Backup will be stored in users homedir....Rsyncing the file to user home dir...";
    echo; echo; echo "-----------------------------------------------------------------------"
    screen -dmS backupgen_${user} bash -c " /etc/profile ; . ~/.bash_profile ; /root/bin/sshrestore --user=${user} --type=${type} --compressed --zip --password=${pass} --force ; ohome=$(/usr/bin/readlink /home/$user) ; /usr/bin/ln -s -f \${ohome}/${user}_${type}.zip /var/www/html/${user}_${type}_bkp.zip ; /bin/echo -e \"Backup for User: ${user} is generated. Password: ${pass} \\n link: \$(hostname -i)/${user}_${type}_bkp.zip\" | /usr/bin/mail -s \"Backup generated for ${user}\" ${mail} ;"
    echo "Backup is running on screen backupgen_${user}. You will recieve a mail after backup is generated.";
    echo -e "Link: http://$(hostname -i)/${user}_${type}_bkp.zip"
    echo -e "Password: $pass"
    sleep 10;
   fi
