#!/bin/sh

term() {
  echo "--> Got SIGTERM"
  echo "--> killing inotifywait with PID $inotifywait"
  kill -TERM "$inotifywait"
}

srcdir="$1"

trap term SIGTERM

inotifywait -mr -e close_write --format %w%f $srcdir | while read -r FILE; do
  case "$FILE" in
    *consts.rs)
      echo "--> patching $FILE"
      sed -i '/pub type U1024/d;/pub type P1024/d' $FILE
      echo "--> finished patching $FILE"
      ;;
  esac
done &
inotifywait=$(ps -ef | grep inotifywait | grep -v grep | tr -s ' ' | cut -d ' ' -f2)
echo "--> waiting for inotifywait $inotifywait"
wait "$inotifywait"
echo "--> watcher terminated"