# !/bin/bash
# ████████╗░█████╗░██╗░░██╗░█████╗░██╗░░░██╗██████╗░███╗░░██╗
# ╚══██╔══╝██╔══██╗██║░██╔╝██╔══██╗██║░░░██║██╔══██╗████╗░██║
# ░░░██║░░░██║░░██║█████═╝░██║░░██║╚██╗░██╔╝██████╔╝██╔██╗██║
# ░░░██║░░░██║░░██║██╔═██╗░██║░░██║░╚████╔╝░██╔═══╝░██║╚████║
# ░░░██║░░░╚█████╔╝██║░╚██╗╚█████╔╝░░╚██╔╝░░██║░░░░░██║░╚███║
# ░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚══╝

# Pewarna hidup
red='\e[1;31m'
green="\033[1;97m"
blue="\033[1;96m"
NC='\e[0m'
clear

echo -e "\e[33m──────────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37m           LIST MEMBER SSH & OPENVPN          \E[0m"
echo -e "\e[33m──────────────────────────────────────────────\033[0m" 
echo -e "\E[40;1;37mNO.   USERNAME         EXP DATE         STATUS\E[0m"
echo -e "\e[33m──────────────────────────────────────────────\033[0m"

# Initialize a counter
counter=1
# Initialize an array to store the account names for later reference
declare -a accounts

# List all users and their info with numbering
while read expired
do
    AKUN="$(echo $expired | cut -d: -f1)"
    ID="$(echo $expired | grep -v nobody | cut -d: -f3)"
    exp="$(chage -l $AKUN | grep "Account expires" | awk -F": " '{print $2}')"
    exp_formatted=$(date -d "$exp" +%Y-%m-%d 2>/dev/null)
    status="$(passwd -S $AKUN | awk '{print $2}' )"
    
    if [[ $ID -ge 1000 ]]; then
        # Menghitung jumlah hari hingga kedaluwarsa
        exp_date=$(date -d "$exp" +%s 2>/dev/null)
        current_date=$(date +%s)
        days_left=$(( (exp_date - current_date) / 86400 ))

        if [[ $days_left -le -0 ]]; then
            color="\e[31m" # Merah
        else
            color="\e[0m"  # Default
        fi

        if [[ "$status" = "L" ]]; then
            printf "${color}%-5s %-15s %-15s %-15s %2s \e[0m\n" "$counter" "$AKUN" "$exp_formatted" "LOCKED"
        else
            printf "${color}%-5s %-15s %-15s %-15s %2s \e[0m\n" "$counter" "$AKUN" "$exp_formatted" "UNLOCKED"
        fi

        # Store the account name in the array for later use
        accounts+=("$AKUN")

        ((counter++))  # Increment counter after each account
    fi
done < /etc/passwd

echo -e "\e[33m──────────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37m         MASUKKAN NOMOR ACCOUNT SSH           \E[0m"
echo -e "\e[33m──────────────────────────────────────────────\033[0m"
read -p "Input Number of Account: " number

if [[ -z "$number" ]]; then
    echo -e "${red}No input detected!${NC}"
    exit 1
fi

# Get the username based on the selected number from the array
selected_account="${accounts[$((number - 1))]}"

# Check if the selected account exists
if [[ -z "$selected_account" ]]; then
    echo -e "${red}Account number not found!${NC}"
    exit 1
fi

# Lock the selected account
passwd -l $selected_account
clear
echo -e "\e[33m──────────────────────────────────────────────\033[0m"
echo -e "\E[40;1;37m      LOCKED SSH OPENVPN ACCOUNT SUCCESS      \E[0m"
echo -e "\e[33m──────────────────────────────────────────────\033[0m"
echo -e ""
echo -e "${NC}Username Info   : $selected_account"
echo -e "${NC}Expired Account : $exp_formatted"
echo -e "${NC}Process Locked  : Success"
echo -e ""
echo -e "\e[33m──────────────────────────────────────────────\033[0m"
systemctl restart ws
echo ""
read -n 1 -s -r -p "Press any key to back"
m-sshws
