#!/bin/sh -eu

# Render everything:
# - tiles (with vmaps_tiles script)
# - PNG and MAP (with vmaps_png script)
# - HTM and JPG (with vmaps_htm script) -- to be removed?
# - MP.ZIP and IMG (with vmaps_png script)
# - tile list (if needed)
# - mbtiles database (with vmaps_mbtiles script, if needed)
# - sqlitedb database (with vmaps_sqlitedb script, if needed)
# - index htm+jpg for each region  -- to be removed?
# - img for each region
#
# Arguments: names with or without paths.
# No arguments: process all vmap files
# Version for mapsoft2, 2025-01-03

##################################################

# read global configuration and functions
. vmaps.sh ||:

# read local configuration
. ./vmaps.conf ||:


##############################################################
# process individual vmap files

# Update colormap if needed
if [ ! -f "$CMAP" ]; then
  echo "Colormap is missing. Updating $CMAP"
  vmap_update_cmap $CMAP_SRC $CMAP
fi

[ "$TILE_DIR" = "" ] || vmaps_tiles $@
[ "$OUT_DIR" = "" ]  || vmaps_png $@
[ "$IMG_DIR" = "" ]  || vmaps_img $@

##############################################################
# assemble tile list and databases

if [ -n "$TILE_DIR" ]; then
  if [ -n "$TLIST" ]; then
    check_nt_tiles "$TLIST" || vmaps_tlist
  fi
  if [ -n "$MBTILES" ]; then
    check_nt_tiles "$MBTILES" || vmaps_pack_mbtiles
  fi
else
  # if TILE_DIR not set, render MBTILES directly
  [ "$MBTILES" = "" ] || vmaps_rend_mbtiles
fi

if [ -n "$SQLITEDB" -a "$SQLITEDB" -ot "$MBTILES" ]; then
  vmaps_mbtiles_to_sqlitedb "$MBTILES" "$SQLITEDB"
fi

# assemble img file
if [ -n "$IMG_DIR" -a -n "$IMGMAP" ]; then
  check_nt_img $IMGMAP || vmaps_pack_img
fi

# new-style index
[ "$INDEX_TXT" = "" ] || vmaps_index

##############################################################
# for each region create html index with preview

for brd in $REG_DIR/*.gpx; do
  [ -f "$brd" ] || continue
  reg=$(basename $brd .gpx)
  echo "Updating region: $reg"

  htm=$OUT_DIR/all_$reg.htm.in
  jpg=$OUT_DIR/all_$reg.jpg

  # Build list of map files, update timestamps
  list=""
  maps=""
  png_tstamp="$(mktemp -u tmp_XXXXXX.tstamp)"
  touch  $png_tstamp -d "1970-01-01"

  for i in $VMAP_DIR/*.$VMAP_EXT; do
    name=${i%.*}
    name=${name##*/}

    # Is the file touches the border?
    $MS2NOM --ext --name "$name" --cover "$brd" || continue
    list="$list $name"
    maps="$maps $OUT_DIR/$name.map"

    [ $png_tstamp -nt $OUT_DIR/$name.png ] ||
      touch $png_tstamp -r $OUT_DIR/$name.png

  done
  echo "Map list: $list"


  # make jpg+htm index
  if [ $htm -ot $png_tstamp -o $jpg -ot $png_tstamp ]; then
    echo "Making $htm + $jpg index"

    # Put all maps in a single json file, use jpeg instead of png
    # (to make conversion faster)
    tmp="$(mktemp -u tmp_XXXXXX.json)"
    $MS2CONV $maps --rescale_maps=$JPEG_SCALE -o "$tmp"
    sed -i -e 's/\.png/\.jpg/g' "$tmp"

    # Make thumbnail image + html map
    $MS2RENDER "$tmp" $EXTRA_TRACKS $BRD_DIR/*.gpx -o "$jpg"\
      --trk_draw_dots 0 --trk_draw_transp 0 --trk_draw_width 1\
      --map_draw_brd 0xFFFF0000 --map_max_sc 100\
      --htm "$htm" --mag $INDEX_SCALE;
    rm -f "$tmp"

    # Fix html map
    [ ! -f "$htm" ] || sed -e '
      /^<area/ {
        s/"\([^"]*\)\.jpg"/"\1.png"/
      }
      s/src="/src="MAPDIR\//
      s|OUT/|MAPDIR/|g
      /<\/html>/d
      /<html>/d
    ' -i "$htm"

    # Add list of files to html map
    echo "<ul>" >> "$htm"
    for name in $list; do
      vmap="$VMAP_DIR/$name.$VMAP_EXT"
      [ -f "$vmap" ] || continue

      ## Use last commit in vmap to put timestamp
      d="$(git log -1 --pretty="format:%ci" "$vmap" | cut -d ' ' -f 1)"

      echo "<li><b>$name</b> (<font color=gray>$d</font>): "
      echo "  <a href=\"MAPDIR/$name.png\">[PNG]</a>"
      echo "  <a href=\"MAPDIR/$name.map\">[MAP]</a>"
      echo "  <a href=\"MAPDIR/$name.mp.zip\">[MP.ZIP]</a>"
    done >> "$htm"
    echo "</ul>" >> "$htm"
  fi
  rm -f $png_tstamp

done

