#!/bin/sh
DEBUG_LOGGING=${1:-false}
TILES_HEALTHCHECK=${2:-http://172.17.0.1/oe-status/Raster_Status/default/2004-08-01/16km/0/0/0.jpeg}
GC_HEALTHCHECK=${3:-http://172.17.0.1/oe-status/1.0.0/WMTSCapabilities.xml}
S3_CONFIGS=$4
SERVER_STATUS=${5:-false}
USE_SSL=${6:-false}
SERVER_NAME=${7:-localhost}

echo "[$(date)] Starting reproject service" >> /var/log/onearth/config.log

if [ ! -f /.dockerenv ]; then
  echo "This script is only intended to be run from within Docker" >&2
  exit 1
fi

cd /home/oe2/onearth/docker/reproject

# Create config directories
mkdir -p /onearth
chmod 755 /onearth
mkdir -p /etc/onearth/config/endpoint/
mkdir -p /etc/onearth/config/conf/
mkdir -p /etc/onearth/config/layers/

# Scrape OnEarth configs from S3
if [ -z "$S3_CONFIGS" ]
then
  echo "[$(date)] S3_CONFIGS not set for OnEarth configs, checking for local configs" >> /var/log/onearth/config.log

  # Check if configs are already mounted (local development)
  # Look for any YAML endpoint config to detect local setup
  LOCAL_CONFIGS=$(find /etc/onearth/config/endpoint/ -name "*.yaml" 2>/dev/null | head -1)
  if [ -z "$LOCAL_CONFIGS" ]; then
    echo "[$(date)] No local configs detected, using sample data" >> /var/log/onearth/config.log
    # Copy sample configs
    cp ../sample_configs/conf/* /etc/onearth/config/conf/
    cp ../sample_configs/endpoint/* /etc/onearth/config/endpoint/
    cp -R ../sample_configs/layers/* /etc/onearth/config/layers/
  else
    echo "[$(date)] Local configs detected, skipping sample config copy" >> /var/log/onearth/config.log
    # Only copy conf files if they don't exist (needed for tilematrixsets.xml, etc.)
    find ../sample_configs/conf/ -name "*.xml" -exec sh -c 'test ! -f "/etc/onearth/config/conf/$(basename "$1")" && cp "$1" "/etc/onearth/config/conf/"' _ {} \;
  fi

  # Load test layers
  mkdir -p /var/www/html/reproject_endpoint/date_test/default/tms
  cp oe2_test_mod_reproject_date.conf /etc/httpd/conf.d
  cp ../test_configs/layers/oe2_test_mod_reproject_layer_source*.config /var/www/html/reproject_endpoint/date_test/default/tms/oe2_test_mod_reproject_date_layer_source.config
  cp ../test_configs/layers/oe2_test_mod_reproject_date*.config /var/www/html/reproject_endpoint/date_test/default/tms/

  mkdir -p /var/www/html/reproject_endpoint/static_test/default/tms
  cp oe2_test_mod_reproject_static.conf /etc/httpd/conf.d
  cp ../test_configs/layers/oe2_test_mod_reproject_layer_source*.config /var/www/html/reproject_endpoint/static_test/default/tms/oe2_test_mod_reproject_static_layer_source.config
  cp ../test_configs/layers/oe2_test_mod_reproject_static*.config /var/www/html/reproject_endpoint/static_test/default/tms/

else
  echo "[$(date)] S3_CONFIGS set for OnEarth configs, downloading from S3" >> /var/log/onearth/config.log

  python3 /usr/bin/oe_sync_s3_configs.py -f -d '/etc/onearth/config/endpoint/' -b $S3_CONFIGS -p config/endpoint >>/var/log/onearth/config.log 2>&1
  python3 /usr/bin/oe_sync_s3_configs.py -f -d '/etc/onearth/config/conf/' -b $S3_CONFIGS -p config/conf >>/var/log/onearth/config.log 2>&1

  for f in $(grep -L 'reproject:' /etc/onearth/config/endpoint/*.yaml); do
    CONFIG_SOURCE=$(yq eval ".layer_config_source" $f)
    CONFIG_PREFIX=$(echo $CONFIG_SOURCE | sed 's@/etc/onearth/@@')

    mkdir -p $CONFIG_SOURCE

    python3 /usr/bin/oe_sync_s3_configs.py -f -d $CONFIG_SOURCE -b $S3_CONFIGS -p $CONFIG_PREFIX >>/var/log/onearth/config.log 2>&1
  done
fi

echo "[$(date)] OnEarth configs copy/download completed" >> /var/log/onearth/config.log

# Copy tilematrixsets config file
cp /home/oe2/onearth/src/modules/mod_wmts_wrapper/configure_tool/tilematrixsets.xml /etc/onearth/config/conf/

# Set Apache logs to debug log level
if [ "$DEBUG_LOGGING" = true ]; then
    perl -pi -e 's/LogLevel warn/LogLevel debug/g' /etc/httpd/conf/httpd.conf
    perl -pi -e 's/LogFormat "%h %l %u %t \\"%r\\" %>s %b/LogFormat "%h %l %u %t \\"%r\\" %>s %b %D/g' /etc/httpd/conf/httpd.conf
fi

# Setup Apache extended server status
if [ "$SERVER_STATUS" = true ]; then
cat >> /etc/httpd/conf/httpd.conf <<EOS
LoadModule status_module modules/mod_status.so

<Location /server-status>
   SetHandler server-status
   Allow from all 
</Location>

# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#
ExtendedStatus On

# Turn off indexes
<Location />
      Options -Indexes
</Location>
EOS
fi

# Comment out welcome.conf
sed -i -e 's/^\([^#].*\)/# \1/g' /etc/httpd/conf.d/welcome.conf

# Disable fancy indexing
sed -i -e '/^Alias \/icons\/ "\/usr\/share\/httpd\/icons\/"$/,/^<\/Directory>$/s/^/#/' /etc/httpd/conf.d/autoindex.conf

# Copy oe-status layers
mkdir -p /etc/onearth/config/layers/oe-status/
cp ../oe-status/layers/*.yaml /etc/onearth/config/layers/oe-status/

# Configure oe-status for reproject health checks
# Check if oe-status_reproject.yaml already exists (from volume mount) as is the case during local deployment
OE_STATUS_REPROJECT_EXISTS=false
if [ -f "/etc/onearth/config/endpoint/oe-status_reproject.yaml" ]; then
  OE_STATUS_REPROJECT_EXISTS=true
  echo "[$(date)] Using existing oe-status_reproject.yaml from config directory" >> /var/log/onearth/config.log
else
  echo "[$(date)] Copying oe-status_reproject.yaml for temporary use" >> /var/log/onearth/config.log
  cp ../oe-status/endpoint/oe-status_reproject.yaml /etc/onearth/config/endpoint/
fi

mkdir -p $(yq eval ".twms_service.internal_endpoint" /etc/onearth/config/endpoint/oe-status_reproject.yaml)
if [ "$USE_SSL" = true ]; then
  python3 /usr/bin/oe2_reproject_configure.py /etc/onearth/config/endpoint/oe-status_reproject.yaml -l "https://$SERVER_NAME" >>/var/log/onearth/config.log 2>&1
else
  python3 /usr/bin/oe2_reproject_configure.py /etc/onearth/config/endpoint/oe-status_reproject.yaml >>/var/log/onearth/config.log 2>&1
fi

# Only remove the file if we copied it temporarily (not from volume mount)
if [ "$OE_STATUS_REPROJECT_EXISTS" = false ]; then
  echo "[$(date)] Removing temporary oe-status_reproject.yaml" >> /var/log/onearth/config.log
  rm /etc/onearth/config/endpoint/oe-status_reproject.yaml
else
  echo "[$(date)] Preserving oe-status_reproject.yaml from config directory" >> /var/log/onearth/config.log
fi

echo "[$(date)] Starting Apache server" >> /var/log/onearth/config.log
/usr/sbin/httpd -k start
sleep 2

# Wait for GC and Tile services to be ready
time_out=600
echo "[$(date)] Begin checking $GC_HEALTHCHECK and $TILES_HEALTHCHECK endpoints...">>/var/log/onearth/config.log 2>&1;
while [[ "$(curl -k -s -m 10 -o /dev/null -w ''%{http_code}'' "$TILES_HEALTHCHECK")" != "200" || 
         "$(curl -k -s -m 60 -o /dev/null -w ''%{http_code}'' "$GC_HEALTHCHECK")" != "200" ]]; do 
  if [[ $time_out -le 0 ]]; then
    echo "[$(date)] ERROR: Timed out waiting for endpoint $GC_HEALTHCHECK or $TILES_HEALTHCHECK">>/var/log/onearth/config.log 2>&1;
    /usr/sbin/httpd stop; cat /var/log/onearth/config.log; exit 1;
  else
  	echo "[$(date)] Waiting for $GC_HEALTHCHECK or $TILES_HEALTHCHECK endpoints...">>/var/log/onearth/config.log 2>&1;
  	sleep 60; #curl in 60 second intervals
  	time_out=$(($time_out-60));
  fi
done

echo "[$(date)] Completed healthcheck wait" >> /var/log/onearth/config.log

# Load additional endpoints
echo "[$(date)] Loading additional endpoints" >> /var/log/onearth/config.log

# Create reproject configuration directories
for f in $(grep -l 'reproject:' /etc/onearth/config/endpoint/*.yaml); do
  # WMTS Endpoint
  mkdir -p $(yq eval ".wmts_service.internal_endpoint" $f)

  # TWMS Endpoint
  mkdir -p $(yq eval ".twms_service.internal_endpoint" $f)
done

# Run endpoint configuration in parallel
if [ "$USE_SSL" = true ]; then
  grep -l 'reproject:' /etc/onearth/config/endpoint/*.yaml | parallel -j 4 python3 /usr/bin/oe2_reproject_configure.py {} -l "https://$SERVER_NAME" >>/var/log/onearth/config.log 2>&1
else
  grep -l 'reproject:' /etc/onearth/config/endpoint/*.yaml | parallel -j 4 python3 /usr/bin/oe2_reproject_configure.py {} >>/var/log/onearth/config.log 2>&1
fi

echo "[$(date)] Completed reproject configuration" >> /var/log/onearth/config.log

echo "[$(date)] Restarting Apache server" >> /var/log/onearth/config.log
/usr/sbin/httpd -k restart
sleep 2

# Run logrotate hourly
echo "0 * * * * /etc/cron.hourly/logrotate" >> /etc/crontab

# Start cron
supercronic -debug /etc/crontab > /var/log/cron_jobs.log 2>&1 &

# Tail the logs
exec tail -qFn 10000 \
  /var/log/cron_jobs.log \
  /var/log/onearth/config.log \
  /etc/httpd/logs/access_log \
  /etc/httpd/logs/error_log