#!/bin/bash
set -e

DB_HOST="${DB_HOST:-db}"
DB_USER="${DB_USER:-videodb}"
DB_PASSWORD="${DB_PASSWORD:-videodb_secret}"
DB_NAME="${DB_NAME:-videodb}"
DB_PREFIX="${DB_PREFIX:-videodb_}"
INGEST_API_TOKEN="${INGEST_API_TOKEN:-changeme}"

CONFIG_FILE="/var/www/html/config.inc.php"

# ── Write config.inc.php from environment variables ──────────────────────────
cat > "$CONFIG_FILE" <<PHP
<?php
\$config['db_server']   = '${DB_HOST}';
\$config['db_user']     = '${DB_USER}';
\$config['db_password'] = '${DB_PASSWORD}';
\$config['db_database'] = '${DB_NAME}';
\$config['db_prefix']   = '${DB_PREFIX}';
\$config['offline']     = 0;
\$config['debug']       = 0;
\$config['httpclientlog'] = 0;
\$config['IMDBage']     = 604800;
\$config['hierarchical'] = 1;
\$config['cache_pruning'] = 1;
\$config['http_caching'] = 0;
\$config['lookupdefault_edit'] = 0;
\$config['lookupdefault_new'] = 2;
\$config['diskid_digits'] = 4;
\$config['thumbnail_level'] = 1;
\$config['thumbnail_quality'] = 80;
\$config['xml'] = 0;
\$config['xml_thumbnails'] = 0;
\$config['rss'] = 1;
\$config['pdf'] = 1;
\$config['pdf_font_title'] = 'Arial';
\$config['pdf_font_plot'] = 'Times';
\$config['pdf_font_size'] = 10;
\$config['pdf_image_max_width'] = 95;
\$config['pdf_image_max_height'] = 135;
\$config['pdf_image_media_width'] = 8;
\$config['pdf_page_width'] = 210;
\$config['pdf_text_length'] = 500;
\$config['pdf_margin'] = 5;
\$config['pdf_left_margin'] = 5;
\$config['pdf_right_margin'] = 5;
\$config['pdf_image_height'] = 24;
\$config['pdf_image_width'] = intval((\$config['pdf_image_max_width'] / \$config['pdf_image_max_height']) * \$config['pdf_image_height']);
\$config['xls'] = 1;
\$config['xls_sheet_title'] = 'VideoDB';
\$config['xls_output_filename'] = 'VideoDB';
\$config['xls_show_headline'] = 1;
\$config['xls_mark_unseen'] = 1;
\$config['xls_mark_lent'] = 1;
\$config['xls_extra_fields'] = 'title (plot), diskid, genres, language, mediatype, runtime, year, custom1, custom2, custom3, custom4, insertdate, owner, lent';
\$config['dvdb_user'] = '';
\$config['dvdb_password'] = '';
\$config['ingest_api_token'] = '${INGEST_API_TOKEN}';
PHP

chown www-data:www-data "$CONFIG_FILE"
echo "[entrypoint] config.inc.php written."

# ── Wait for MySQL (use PHP mysqlnd — same driver the app uses) ────────────────
echo "[entrypoint] Waiting for MySQL at ${DB_HOST}..."
until php -r "exit(@mysqli_connect('${DB_HOST}','${DB_USER}','${DB_PASSWORD}','${DB_NAME}') ? 0 : 1);" 2>/dev/null; do
    printf "."
    sleep 2
done
echo ""
echo "[entrypoint] MySQL is ready."

# ── Initialize DB schema (idempotent) ─────────────────────────────────────────
php /usr/local/bin/init-db.php

# ── Ensure cache dirs exist and are writable ──────────────────────────────────
mkdir -p \
    /var/www/html/cache/smarty \
    /var/www/html/cache/imdb \
    /var/www/html/cache/img \
    /var/www/html/cache/thumbs \
    /var/www/html/cache/javascript \
    /var/www/html/cache/local
chown -R www-data:www-data /var/www/html/cache /var/www/html/images

echo "[entrypoint] Starting Apache..."
exec "$@"
