#!/bin/bash
echo "START ODOO 17"
# Load functions
source /4server/sbin/ODOO_19/ODOO_19.lib
source /4server/sbin/helpers
get_contract_info

# Config variables
UUID="${UUID:-default}"
BRANCH="${BRANCH:-release}"

ODOO_DB_USER="${UUID}"
#export ODOO_DB_PASSWORD=$(echo "$SECRET" | jq -r '.psql')


echo "ENV: $HDD $DOMAIN_SLOTS $BACKUP_SLOTS $CONTAINERDBID"
echo "DBID: $CONTAINERDBID"

BASEURL="${BASEURL:-/4server/data/$UUID}"
DATA_DIR="$BASEURL/odoo/"
CUSTOM_DIR="$BASEURL/git/$UUID/custom/"
ENTERPRISE_DIR="$BASEURL/git/$UUID/enterprise/"
LOGS_DIR="$BASEURL/logs/"
CONFIG_DIR="$BASEURL/config/"
CC_DIR="$BASEURL/cc/"
BACKUP_DIR="/BACKUP/$UUID"
GIT_DIR="$BASEURL/git-server/"
INSTALL_DIR="$BASEURL/install/"
SSH_DIR="$BASEURL/.ssh/"
ETC_DIR="$BASEURL/etc/"

SERVER_IP=$(ip -4 addr show eth0 | awk '/inet/ {print $2}' | cut -d/ -f1 | head -n1)

LABEL_DOMAINS=("$UUID.odoo4projects.com")
if [ -f "$BASEURL/etc/domain" ]; then
    while IFS= read -r domain || [[ -n $domain ]]; do
        [ -z "$domain" ] && continue
        LABEL_DOMAINS+=("$domain")
    done < "$BASEURL/etc/domain"
else
    echo "[DEBUG] No additional domain file found at $BASEURL/etc/domain"
fi

RULE=""
for d in "${LABEL_DOMAINS[@]}"; do
    RESOLVED_IP=$(nslookup "$d" 2>/dev/null | awk '/^Address: / { print $2 }' | head -n1 || true)
    if [ -z "$RESOLVED_IP" ]; then
        echo "[DEBUG] Could not resolve $d, skipping"
        continue
    fi
    if [ "$RESOLVED_IP" = "$SERVER_IP" ]; then
        RULE+=" || Host(\`$d\`)"
    else
        echo "[DEBUG] Skipping $d (does not match SERVER_IP $SERVER_IP)"
    fi
done
RULE="${RULE# || }"
DOMAIN_LABEL="traefik.http.routers.$UUID.rule=$RULE"
echo "[DEBUG] Final Traefik label: $DOMAIN_LABEL"

docker exec "$UUID" mkdir -p /var/lib/odoo/.local/share/Odoo/
docker exec "$UUID" ln -s /home/odoo/.local/share/Odoo/filestore /var/lib/odoo/.local/share/Odoo/filestore


find "$BASEURL" -type d -exec chmod 777 {} \;
PORT=$((CONTAINERDBID + 2200))
echo "PORT $PORT"
mkdir -p ${ETC_DIR}
echo "GITPATH ${ETC_DIR}gitpath"
touch ${ETC_DIR}gitpath

mkdir -p ${GIT_DIR}keys
touch ${GIT_DIR}keys/id_rsa.pub


echo "git clone \"ssh://git@${UUID}.odoo4projects.com:${PORT}/git-server/repos/odoo.git\"" > "${ETC_DIR}/gitpath"


docker stop "$UUID" 2>/dev/null
docker rm "$UUID" 2>/dev/null

EXTRA_DOCKER_PARAMETER=""

docker run -d --name "$UUID" \
		--network 4server_4projects \
		--restart=always \
		$EXTRA_DOCKER_PARAMETER \
		-v "$DATA_DIR/odoo-web-data:/home/odoo/.local/share/Odoo" \
		-v "$CUSTOM_DIR:/mnt/addons/custom" \
		-v "$ENTERPRISE_DIR:/mnt/addons/enterprise" \
		-v "$LOGS_DIR:/mnt/logs" \
		-v "$CC_DIR:/mnt/cc" \
		-v "$BACKUP_DIR:/mnt/backup" \
		-v "$GIT_DIR:/git-server" \
		-v "$INSTALL_DIR:/mnt/install" \
		-v "$SSH_DIR:/etc/sshkey" \
		-v "$ETC_DIR:/mnt/etc" \
		-p "$PORT:22" \
		-e HOST="beedb" \
		-e USER="$ODOO_DB_USER" \
		-e PASSWORD="$ODOO_DB_PASSWORD" \
		-e UUID="$UUID" \
		-e HDD="$HDD" \
		-e DOMAIN_SLOTS="$DOMAIN_SLOTS" \
		-e BACKUP_SLOTS="$BACKUP_SLOTS" \
    		-e WORKER="$WORKER" \
		-e GIT="$GIT" \
    		--label "$DOMAIN_LABEL" \
		--label "traefik.http.services.$UUID.loadbalancer.server.port=8069" \
		--label "traefic.http.routers.$UUID.entrypoints=web, websecure" \
    		--label "traefik.http.routers.$UUID.tls.certresolver=production" \
    		--label "traefik.http.routers.$UUID.tls=true" \
    		--label "traefik.http.routers.$UUID.service=$UUID" \
		docker.odoo4projects.com/4projects/odoo_17:$BRANCH

docker exec "$UUID" mkdir -p /var/lib/odoo/.local/share/Odoo
docker exec "$UUID" rm -rf /var/lib/odoo/.local/share/Odoo/filestore
docker exec "$UUID" ln -s /home/odoo/.local/share/Odoo/filestore /var/lib/odoo/.local/share/Odoo/filestore


docker exec $UUID chown -R odoo:odoo /home/odoo/.local
docker exec $UUID chown -R odoo:odoo /var/lib/odoo/.local/share/Odoo
docker exec $UUID chown -R odoo:odoo /mnt/*
docker exec $UUID chown odoo:odoo /git-server/keys/id_rsa.pub


check_and_create_db
