#!/usr/bin/env bash

# Copyright (c) 2021-2026 community-scripts ORG
# Author: CrazyWolf13
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
# Source: https://docs.craftycontrol.com/pages/getting-started/installation/linux/

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

msg_info "Setting up TemurinJDK"
setup_java
$STD apt install -y temurin-{8,11,17,21,25}-jre
sudo update-alternatives --set java /usr/lib/jvm/temurin-25-jre-amd64/bin/java
msg_ok "Installed TemurinJDK"

msg_info "Setup Python3"
$STD apt install -y \
  python3 \
  python3-dev \
  python3-pip \
  python3-venv
rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED
msg_ok "Setup Python3"

msg_info "Installing Crafty-Controller (Patience)"
useradd crafty -m -s /bin/bash
cd /opt
mkdir -p /opt/crafty-controller/crafty /opt/crafty-controller/server
RELEASE=$(curl -fsSL "https://gitlab.com/api/v4/projects/20430749/releases" | grep -o '"tag_name":"v[^"]*"' | head -n 1 | sed 's/"tag_name":"v//;s/"//')
echo "${RELEASE}" >"/opt/crafty-controller_version.txt"
curl -fsSL "https://gitlab.com/crafty-controller/crafty-4/-/archive/v${RELEASE}/crafty-4-v${RELEASE}.zip" -o "crafty-4-v${RELEASE}.zip"
$STD unzip crafty-4-v"${RELEASE}".zip
cp -a crafty-4-v"${RELEASE}"/. /opt/crafty-controller/crafty/crafty-4/
rm -rf crafty-4-v"${RELEASE}"

cd /opt/crafty-controller/crafty
python3 -m venv .venv
chown -R crafty:crafty /opt/crafty-controller/
$STD sudo -u crafty bash -c '
    source /opt/crafty-controller/crafty/.venv/bin/activate
    cd /opt/crafty-controller/crafty/crafty-4
    pip3 install --no-cache-dir -r requirements.txt
'
msg_ok "Installed Craft-Controller and dependencies"

msg_info "Setting up service"
cat <<EOF >/etc/systemd/system/crafty-controller.service
[Unit]
Description=Crafty 4
After=network.target

[Service]
Type=simple
User=crafty
WorkingDirectory=/opt/crafty-controller/crafty/crafty-4
Environment=PATH=/usr/lib/jvm/temurin-25-jre-amd64/bin:/opt/crafty-controller/crafty/.venv/bin:$PATH
ExecStart=/opt/crafty-controller/crafty/.venv/bin/python3 main.py -d
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
$STD systemctl enable -q --now crafty-controller
CREDS_FILE="/opt/crafty-controller/crafty/crafty-4/app/config/default-creds.txt"
for i in $(seq 1 30); do
  [[ -f "$CREDS_FILE" ]] && break
  sleep 2
done
if [[ -f "$CREDS_FILE" ]]; then
  {
    echo "Crafty-Controller-Credentials"
    echo "Username: $(grep -oP '(?<="username": ")[^"]*' "$CREDS_FILE")"
    echo "Password: $(grep -oP '(?<="password": ")[^"]*' "$CREDS_FILE")"
  } >>~/crafty-controller.creds
fi
msg_ok "Service started"

motd_ssh
customize
cleanup_lxc
