#!/bin/bash
#Funtions
Help()
{
   # Display Help
   echo "Remove user from backupserver"
   echo
   echo "Syntax: deluserutil [-a|v|s|ts] <UserName>"
   echo "options:"
   echo "-h, --help     Print this Help."
   echo 
   echo "Define user type for user that shoud we removed"
   echo "-a, --admin  #Backup Admin"
   echo "-v, --viewer #Backup Viewer "
   echo "-s, --source #Backup Source (ex:a webserver)"
   echo 
   echo "Set backup source options for new user"
   echo "-ts, --sftp  #Backup source will use sftp/rsync to upload files"
   echo "-tf, --ftp   #Backup source will use ftp to upload files"
   echo
}


#Input Handeler
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -h | --help)
    Help
    exit
    ;;
    -v | --viewer)
    USERTYPE=viewer
    shift
    shift
    ;;
    -a |--admin)
    USERTYPE=admin
    shift
    ;;
    -s|--source)
    USERTYPE=source
    shift
    ;;
    -tf|--ftp )
    UPTYPE=FTP
    shift 
    ;;
    -ts|--sftp )
    UPTYPE=SFTP
    shift 
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

username="$1"


#Input Check
if [ -z "$username" ]
then
    echo "Please provide a username"
    echo "For more information type deluserutil -h"
    echo
    exit
fi

if [ "$username" = "root" ]; then
    echo "Root is not allowed"
fi

getent passwd $username  > /dev/null
if [ $? -eq 2 ]; then
    echo "This username does not exists"
    exit
fi

if [ -z "$USERTYPE" ]
then
    echo "Please provide user type"
    echo "For more information type deluserutil -h"
    echo
    exit
fi
if [ "$USERTYPE" = "source" ]; then
if [ -z "$UPTYPE" ]
then
    echo "Please provide upload methode"
    echo "For more information type deluserutil -h"
    echo
    exit
fi
fi

#Confirmation before setting user
echo "Removing the user with the following information"
echo "Username: $username"
echo "User type: $USERTYPE"
if [ "$USERTYPE" = "source" ]; then
echo "Upload methode $UPTYPE"
fi
while true; do
    read -p "Do you wish to REMOVE $username? " yn
    case $yn in
        [Yy]* ) break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done


#Remove Admin user
if [ "$USERTYPE" = "admin" ]; then
userdel -rf $username
fi


#Remove Viewer user
if [ "$USERTYPE" = "viewer" ]; then
umount /vhome/$username/backup
sed -i "/\/$username\//d" /etc/fstab
userdel -f $username
rm -rf /vhome/$username
fi


#Remove sftp source user
if [ "$USERTYPE" = "source" ] && [ "$UPTYPE" = "SFTP" ]; then
while true; do
    read -p "Do you wish to REMOVE the backups connected to this user? " yn
    case $yn in
        [Yy]* ) rm -rf /backups/sftp/"$username" && break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done
userdel -f $username
fi


#Remove ftp source user
if [ "$USERTYPE" = "source" ] && [ "$UPTYPE" = "FTP" ]; then
while true; do
    read -p "Do you wish to REMOVE the backups connected to this user? " yn
    case $yn in
        [Yy]* ) rm -rf /backups/ftp/"$username" && break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done
 sed -i "/\<$username\>/d" /etc/ftpusers 
 userdel -f $username
fi
