#!/bin/bash

# Script to Remove Pure-FTPd User
# Author: ChatGPT
# Description: Removes an FTP user, cleans up their home directory, and updates Pure-FTPd.

# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
    echo "Please run this script as root or with sudo."
    exit 1
fi

# Prompt for FTP username
read -p "Enter the FTP username to remove: " FTP_USER

# Check if the user exists
if ! id "$FTP_USER" &>/dev/null; then
    echo "User '$FTP_USER' does not exist. Exiting."
    exit 1
fi

# Get the user's home directory
FTP_HOME=$(eval echo ~"$FTP_USER")

# Confirm before deletion
echo "This will remove the FTP user '$FTP_USER' and delete their home directory: $FTP_HOME"
read -p "Are you sure? (y/n): " CONFIRM

if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
    echo "Operation cancelled."
    exit 0
fi

# Remove user from Pure-FTPd virtual user database
echo "Removing user '$FTP_USER' from Pure-FTPd virtual database..."
pure-pw userdel "$FTP_USER"
pure-pw mkdb

# Remove the system user and their home directory
echo "Deleting system user '$FTP_USER' and their home directory..."
userdel -r "$FTP_USER"

# Restart Pure-FTPd service
echo "Restarting Pure-FTPd service..."
systemctl restart pure-ftpd

# Final confirmation
echo "FTP user '$FTP_USER' has been removed successfully!"
