#!/bin/bash -l

trap 'rm $MERGE;' EXIT

MERGE=$(mktemp)

# Get list of merged files
git diff --name-only 'HEAD@{1}' HEAD > "$MERGE"

# If bsps have changed, use resgen to generate reference res files.
if resgen=$( grep -E '^maps/[a-z_0-9!\-]+\.bsp$' "$MERGE" | grep . ); then
  resgen.sh $resgen
fi

# Find various excuses to regenerate deployed res files.
if grep -q '^maps/res-global$' $MERGE; then # See if global resource include was changed
  ini-res-combine.sh maps/*.bsp
elif rescombine=$(
  (
    grep -E '^maps/[a-z_0-9!\-]+\.[a-z]+$' "$MERGE"; # Find changed map files
    for i in $( grep -E '^models/player/[a-z_0-9\-]+/.*' "$MERGE" | perl -pe 's|^models/player/([a-z_0-9-]+)/.*|\1|' | sort -u ); do # Find changed player models
      grep -lE /"$i"'( |\])*$' maps/*.ini; # Find maps that depend on those player models
    done
  ) | perl -pe 's|^(maps/[a-z_0-9!-]+)\..+|\1.bsp|' | sort -u | grep .
); then
  ini-res-combine.sh $rescombine
fi

# FIXME: PWD is not symlink-safe, so we rely on ~/Steam/SteamApps/common/Half-Life/action to be a fully dereferenced path.
# action_fastdl and action_pkg should be safe to symlink, though.
if [[ $PWD =~ /Steam/SteamApps/common/Half-Life/action$ ]]; then # Are we an HLDS deployment?
  # Ikiwiki's indexdb is rather hackishly required for calculating cfg/*/*/mapcycle.mdwn output.
  if perl -MIkiWiki </dev/null 2> /dev/null; then
    mkdir -p ~/Steam/SteamApps/common/Half-Life/ikiwiki
    wget -q -O ~/Steam/SteamApps/common/Half-Life/ikiwiki/indexdb http://piny.be/ahl/indexdb
  fi
  # If server cfgs have changed, restart
  if grep -E '^cfg/'$(hostname) "$MERGE"; then
    echo "$(hostname) config change detected. Restarting..."
    ahl-restart.sh
  fi
else # If not, we're probably a fastdl deployment and we might want to make convenient packages.
  if [ -d ~/Steam/SteamApps/common/Half-Life/action_pkg ]; then

    # map packages
    MAPUPDATE=0
    for i in $( grep -E '^maps/[a-z_0-9!\-]+\.[a-z]+$' "$MERGE" | perl -pe 's|^maps/([a-z_0-9!-]+)\..+|\1|' | sort -u ); do
      if [ -e maps/"$i".resgen ]; then
        MAPUPDATE=1
        function buildzip {
          zip --filesync ~/Steam/SteamApps/common/Half-Life/action_pkg/map/"$i".zip maps/"$i".* $(grep -E '^[a-z0-9]' maps/"$i".resgen | grep -v ' ' )
        }
        buildzip || rm -f ~/Steam/SteamApps/common/Half-Life/action_pkg/map/"$i".zip && buildzip # zip doesn't like dealing with broken files, so we delete and try again
      fi
    done

    if [ "$MAPUPDATE" = 1 ]; then # make sure 'getconf ARG_MAX' outputs 2097152 or more, otherwise this is unlikely to work
      function buildzip {
        zip --filesync ~/Steam/SteamApps/common/Half-Life/action_pkg/map/00-ALL-MAPS.zip $( for i in $( find maps/ -name '*.bsp' | sed '{s/.bsp$//}' ); do echo "$i".*; grep -E '^[a-z0-9]' "$i".resgen | grep -v ' ' ; done | sort -u )
      }
      buildzip || rm -f ~/Steam/SteamApps/common/Half-Life/action_pkg/map/00-ALL-MAPS.zip && buildzip
    fi

    # model packages
    MODELUPDATE=0
    for i in $( grep models/player "$MERGE" | perl -pe 's|^models/player/([a-z_0-9-]+)/.*|\1|' | sort -u ); do
      MODELUPDATE=1
      function buildzip {
        zip --filesync ~/Steam/SteamApps/common/Half-Life/action_pkg/player/"$i".zip models/player/$i/*.*
      }
      buildzip || rm -f ~/Steam/SteamApps/common/Half-Life/action_pkg/player/"$i".zip && buildzip
    done

    if [ "$MODELUPDATE" = 1 ]; then
      function buildzip {
        zip --filesync ~/Steam/SteamApps/common/Half-Life/action_pkg/player/00-ALL-PLAYER-MODELS.zip models/player/*/*.*
      }
      buildzip || rm -f ~/Steam/SteamApps/common/Half-Life/action_pkg/player/00-ALL-PLAYER-MODELS.zip && buildzip
    fi
  fi
fi
