#!/bin/bash

# -----
# Add "# " to lines ": << 'EOF'" and "EOF" (simplified script)
# == AND ==
# remove "# " from lines "# : << 'EOF'" and "# EOF" (advanced script)
# to toggle Logging Feature. Reverse to toggle back.
# -----

: << 'EOF'
# Simplified script without logging feature

# Dynamically ensure correct pwd
cd "$(dirname "${BASH_SOURCE[0]}")"

# Load environment variables from .env.monthly-bvn-build
source .env.monthly-bvn-build 2>/dev/null
export LOCAL_BVN_REPO_PATH GIT_BRANCH SITE_DIR_PATH LOG_PATH

# Ensure log dir exists (silent)
mkdir -p "$(dirname "$LOG_PATH")"

# Update repo via HTTPS (public repo, no auth needed)
git pull origin "$GIT_BRANCH" || true

# Remove 11ty build artifacts
rm -rf "${SITE_DIR_PATH}"

# Rebuild static site
npx @11ty/eleventy
EOF

# ----------

# : << 'EOF'
# Advanced script with logging feature added
#!/bin/bash

# Disable auto-exit for custom handling (add set -e if desired)
set +e

# Dynamically ensure correct pwd (no logging yet, as LOG_PATH undefined)
cd "$(dirname "${BASH_SOURCE[0]}")"
exit_code=$?
if [ $exit_code -ne 0 ]; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: cd to script directory failed with code $exit_code" >&2
    exit $exit_code
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - cd to script directory: SUCCESS" >&2

# Load environment variables from .env.monthly-bvn-build (log to stderr for this step)
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting source .env.monthly-bvn-build" >&2
source .env.monthly-bvn-build 2>/dev/null
export LOCAL_BVN_REPO_PATH GIT_BRANCH SITE_DIR_PATH LOG_PATH
exit_code=$?
if [ $exit_code -eq 0 ] && [ -n "$LOCAL_BVN_REPO_PATH" ] && [ -n "$GIT_BRANCH" ] && [ -n "$SITE_DIR_PATH" ] && [ -n "$LOG_PATH" ]; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Source .env.monthly-bvn-build: SUCCESS (base: $LOCAL_BVN_REPO_PATH, log: $LOG_PATH)" >&2
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Source .env.monthly-bvn-build: FAILURE (code $exit_code or missing vars) - Aborting" >&2
    exit $exit_code
fi

# Ensure log dir exists (silent)
mkdir -p "$(dirname "$LOG_PATH")"

# Now define log function, as LOG_PATH is set
echo "$(date '+%Y-%m-%d %H:%M:%S') - === BUILD START -- Log: $LOG_PATH ===" >> "$LOG_PATH"

log_step() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_PATH"
}

log_exit() {
    echo "--------------------------------" >> "$LOG_PATH"
}

# Log the successful setup to the file
log_step ".env.monthly-bvn-build loaded successfully: $LOG_PATH"

# Log environment details
log_step "Environment: GIT_BRANCH=$GIT_BRANCH, LOCAL_BVN_REPO_PATH=$LOCAL_BVN_REPO_PATH, SITE_DIR_PATH=$SITE_DIR_PATH"

# Update repo from correct branch (non-fatal, HTTPS public)
log_step "Starting git pull from $GIT_BRANCH (HTTPS public repo)"
git pull "$GIT_REPO" "$GIT_BRANCH" || true
exit_code=$?
if [ $exit_code -eq 0 ]; then
    log_step "git pull: SUCCESS"
else
    log_step "git pull: FAILURE (code $exit_code, but continuing)"
fi

# Remove 11ty build artifacts
log_step "Starting rm -rf ${SITE_DIR_PATH}"
rm -rf "${SITE_DIR_PATH}"
exit_code=$?
if [ $exit_code -eq 0 ]; then
    log_step "rm -rf ${SITE_DIR_PATH}: SUCCESS"
else
    log_step "rm -rf ${SITE_DIR_PATH}: FAILURE (code $exit_code)"
    log_exit
    exit $exit_code
fi

# Rebuild static site
log_step "Starting npx @11ty/eleventy"
npx @11ty/eleventy
exit_code=$?
if [ $exit_code -eq 0 ]; then
    log_step "npx @11ty/eleventy: SUCCESS - Rebuild complete"
else
    log_step "npx @11ty/eleventy: FAILURE (code $exit_code)"
    log_exit
    exit $exit_code
fi

# Overall script completion
log_step "Script completed successfully"
log_step "=== BUILD END: $(date '+%Y-%m-%d %H:%M:%S') ==="
log_exit
exit 0

# EOF
