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

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;37mUSERNAME          EXP DATE          STATUS\E[0m"
echo -e "\e[33m──────────────────────────────────────────\033[0m"

# Membaca dan mengurutkan data dari /etc/passwd
sort -t: -k1,1 /etc/passwd | 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}%-15s %2s %-15s %2s \e[0m\n" "$AKUN " " $exp_formatted      " "LOCKED"
        else
            printf "${color}%-15s %2s %-15s %2s \e[0m\n" "$AKUN " " $exp_formatted      " "UNLOCKED"
        fi
    fi
done

JUMLAH="$(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd | wc -l)"
echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e "\E[1;37mTOTAL MEMBER SSH & OPENVPN : $JUMLAH USER\E[0m"
echo -e "\e[33m──────────────────────────────────────────\033[0m"
echo -e ""
read -n 1 -s -r -p "Press any key to back"
m-sshws
