#!/usr/bin/env bash # # Run the complete WCX index update: # # 1. Download the current site index. # 2. Import site metadata into SQLite. # 3. OCR-process newly discovered movies. # # Usage: # scripts/update_wcx.sh # scripts/update_wcx.sh --skip-ocr # # GOOGLE_VISION_API_KEY must be set unless --skip-ocr is used. # set -Eeuo pipefail ROOT_DIR="/storage/disk1/WCX" SYNC_SCRIPT="${ROOT_DIR}/wcx_sync.py" IMPORT_SCRIPT="${ROOT_DIR}/scripts/import_site.py" OCR_BATCH_SCRIPT="${ROOT_DIR}/scripts/process_pending_ocr.py" DATABASE_FILE="${ROOT_DIR}/database/wcx.db" INDEX_FILE="${ROOT_DIR}/import/wcx_site_index.json" LOCK_FILE="${ROOT_DIR}/database/update_wcx.lock" SKIP_OCR=false log() { printf '[%s] %s\n' \ "$(date '+%Y-%m-%d %H:%M:%S')" \ "$*" } fail() { log "ERROR: $*" >&2 exit 1 } on_error() { local exit_code=$? local line_number=$1 log "Update failed on line ${line_number} with exit code ${exit_code}." >&2 exit "${exit_code}" } show_usage() { cat <<'EOF' Usage: update_wcx.sh update_wcx.sh --skip-ocr Options: --skip-ocr Run site synchronization and database import without OCR. --help Show this help. EOF } while [[ $# -gt 0 ]]; do case "$1" in --skip-ocr) SKIP_OCR=true shift ;; --help|-h) show_usage exit 0 ;; *) fail "Unknown argument: $1" ;; esac done trap 'on_error "$LINENO"' ERR [[ -f "${SYNC_SCRIPT}" ]] || fail "Sync script not found: ${SYNC_SCRIPT}" [[ -x "${IMPORT_SCRIPT}" ]] || fail "Import script is missing or not executable: ${IMPORT_SCRIPT}" [[ -x "${OCR_BATCH_SCRIPT}" ]] || fail "OCR batch script is missing or not executable: ${OCR_BATCH_SCRIPT}" [[ -f "${DATABASE_FILE}" ]] || fail "Database not found: ${DATABASE_FILE}" if [[ "${SKIP_OCR}" == false ]] && [[ -z "${GOOGLE_VISION_API_KEY:-}" ]]; then fail "GOOGLE_VISION_API_KEY is not set. Use --skip-ocr to omit OCR." fi # Prevent two update processes from running simultaneously. exec 9>"${LOCK_FILE}" if ! flock -n 9; then fail "Another WCX update is already running." fi cd "${ROOT_DIR}" log "Starting WCX update." log "Database: ${DATABASE_FILE}" log "Step 1/3: Downloading current WCX site index." python3 "${SYNC_SCRIPT}" --out "${INDEX_FILE}" [[ -s "${INDEX_FILE}" ]] || fail "Site index was not created or is empty: ${INDEX_FILE}" log "Site index downloaded successfully." log "Step 2/3: Importing site metadata." "${IMPORT_SCRIPT}" \ --database "${DATABASE_FILE}" \ --input "${INDEX_FILE}" log "Site metadata imported successfully." if [[ "${SKIP_OCR}" == true ]]; then log "Step 3/3: OCR skipped." else log "Step 3/3: Processing pending OCR records." "${OCR_BATCH_SCRIPT}" \ --database "${DATABASE_FILE}" log "OCR processing finished." fi log "WCX update completed successfully."