#!/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"

    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=$(echo ${user} | md5sum | head -c 8 ; echo '');
    

   if [[ "$is_deleted" -eq 1 ]]; then
    
    echo "user deleted ... function will be added later."
    exit 0
    

   else
    
    if [[ $( ps aux | grep "/root/bin/sshrestore --user=${user}" | wc -l ) -gt 1 ]]; 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 "-----------------------------------------------------------------------"
    /root/bin/sshrestore --user=${user} --type=${type} --compressed --zip --password=${pass} --force 
    ohome=$(readlink /home/$user)
    ln -s -f ${ohome}/${user}_${type}.zip /var/www/html/${user}_${type}_bkp.zip
    echo "Zip password is: ${pass}"
    echo "Backup also can be downloaed from http://$(hostname -i)/${user}_${type}_bkp.zip"
    echo -e "Backup for User: ${user} is generated. Password: ${pass} \n link: $(hostname -i)/${user}_${type}_bkp.zip" | mail -s "Backup generated for ${user}" ${mail}

   fi
