#!/usr/bin/env bash

# Copyright (c) 2021-2025 community-scripts ORG
# Author: GitHub Copilot
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
# Source: https://github.com/vogler/free-games-claimer

source /dev/stdin <<<"$FUNCTIONS_FILE_PATH"
color
verb_ip6
catch_errors
setting_up_container
network_check
update_os

msg_info "Installing Dependencies"
$STD apt-get install -y \
    curl \
    git \
    ca-certificates \
    gnupg \
    xvfb \
    x11vnc \
    novnc \
    websockify \
    tini \
    python3-pip \
    dos2unix \
    libgtk-3-0 \
    libasound2 \
    libxcomposite1 \
    libpangocairo-1.0-0 \
    libpango-1.0-0 \
    libatk1.0-0 \
    libcairo-gobject2 \
    libcairo2 \
    libgdk-pixbuf-2.0-0 \
    libdbus-glib-1-2 \
    libxcursor1
msg_ok "Installed Dependencies"

msg_info "Installing Node.js"
$STD mkdir -p /etc/apt/keyrings
$STD curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
$STD echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
$STD apt-get update
$STD apt-get install -y nodejs
msg_ok "Installed Node.js"

msg_info "Installing Apprise"
$STD pip install apprise
msg_ok "Installed Apprise"

msg_info "Setting up ${APPLICATION}"
$STD mkdir -p /opt/${APPLICATION}
$STD git clone https://github.com/vogler/free-games-claimer.git /opt/${APPLICATION}
cd /opt/${APPLICATION} || exit
$STD mkdir -p /opt/${APPLICATION}/data/browser
$STD mkdir -p /opt/${APPLICATION}/data/screenshots
cat <<EOT >/opt/${APPLICATION}/data/browser/user.js
user_pref("privacy.resistFingerprinting", true);
EOT
$STD ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html
cd /opt/${APPLICATION} || exit
$STD npm install
msg_ok "Set up ${APPLICATION}"

msg_info "Creating Services"
cat <<EOF >/etc/systemd/system/${APPLICATION}-display.service
[Unit]
Description=Xvfb Display Server for ${APPLICATION}
After=network.target

[Service]
Environment=DISPLAY=:1
Environment=WIDTH=1920
Environment=HEIGHT=1080
Environment=DEPTH=24
ExecStart=/usr/bin/Xvfb \${DISPLAY} -ac -screen 0 \${WIDTH}x\${HEIGHT}x\${DEPTH}
Restart=always

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/systemd/system/${APPLICATION}-vnc.service
[Unit]
Description=VNC Server for ${APPLICATION}
After=${APPLICATION}-display.service
Requires=${APPLICATION}-display.service

[Service]
Environment=DISPLAY=:1
Environment=VNC_PORT=5900
ExecStart=/usr/bin/x11vnc -display \${DISPLAY} -forever -shared -rfbport \${VNC_PORT}
Restart=always

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/systemd/system/${APPLICATION}-novnc.service
[Unit]
Description=noVNC Server for ${APPLICATION}
After=${APPLICATION}-vnc.service
Requires=${APPLICATION}-vnc.service

[Service]
Environment=NOVNC_PORT=6080
ExecStart=/usr/bin/websockify --web /usr/share/novnc/ \${NOVNC_PORT} localhost:5900
Restart=always

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/systemd/system/${APPLICATION}.service
[Unit]
Description=${APPLICATION} Service
After=${APPLICATION}-display.service
Requires=${APPLICATION}-display.service

[Service]
Type=oneshot
User=root
WorkingDirectory=/opt/${APPLICATION}
Environment=DISPLAY=:1
Environment=SHOW=1
ExecStart=/usr/bin/node /opt/${APPLICATION}/epic-games.js
ExecStart=/usr/bin/node /opt/${APPLICATION}/prime-gaming.js
ExecStart=/usr/bin/node /opt/${APPLICATION}/gog.js
StandardOutput=journal

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/systemd/system/${APPLICATION}-timer.service
[Unit]
Description=Run ${APPLICATION} daily to claim free games
Requires=${APPLICATION}.service

[Timer]
Unit=${APPLICATION}.service
OnCalendar=*-*-* 12:00:00
RandomizedDelaySec=3600
Persistent=true

[Install]
WantedBy=timers.target
EOF

$STD systemctl enable --now ${APPLICATION}-display
$STD systemctl enable --now ${APPLICATION}-vnc
$STD systemctl enable --now ${APPLICATION}-novnc
$STD systemctl enable ${APPLICATION}.service
$STD systemctl enable ${APPLICATION}-timer.service
msg_ok "Created Services"

RELEASE=$(curl -fsSL https://api.github.com/repos/vogler/free-games-claimer/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4)}')
echo "${RELEASE}" >/opt/${APPLICATION}_version.txt
msg_ok "Setup ${APPLICATION}"

motd_ssh
customize

msg_info "Cleaning up"
$STD apt-get -y autoremove
$STD apt-get -y autoclean
msg_ok "Cleaned"
