#!/bin/sh

set -e

me="$(basename "$0")"

if [ "$1" = "-h" ]; then
  echo "Usage: $me [libdir]"
  echo
  echo "This will generate .lib files from .a files in the 'dir' directory recursively:"
  echo "  if a .lib file does not exist."
  echo "  if a .lib file is older than a .a file."
  echo "If libdir is omitted, libdir will be the parent dir of the dir where" \
       "$me is. That is, dir/.."
  exit 0
fi

dir="${1:-$(dirname "$0")/..}"

: ${EMXOMF:=emxomf -s -l -q -p 256}
: ${TOUCH:=touch -r}

echo "Generating .lib from .a in \`$dir' recursively..."

find "$dir" -name '*.a' | while IFS= read -r libaout
do
  libomf="${libaout%.a}.lib"
  if ! [ -f "$libomf" ]; then
    echo "Building $libomf"
  elif [ "$libaout" -nt "$libomf" ]; then
    echo "Updating $libomf"
  else
    continue
  fi

  $EMXOMF "$libaout" && $TOUCH "$libaout" "$libomf"
done
