#!/bin/sh

# Prepare flags, main #include
MAINOPTS="-E"
if [ -e "./composer.ph" ] ; then
  MAINOPTS="$OPTS -I./composer.ph"
elif [ -e "./vendor/composer.ph" ] ; then
  MAINOPTS="$OPTS -I./vendor/composer.ph"
fi

# Uncover pkg folders
for VND in $(find ./vendor/ -type f -name 'composer.json')
do
  VND="$(dirname $VND)"

  # Test for `composer.ph` in pkg
  PKGOPTS="-I$VND"
  if [ -e "$VND/composer.ph" ]
  then
     echo "Preprocessing all *.src to *.php in $VND"
     PKTOPTS="$PKGOPTS -I$VND/composer.ph"
  else
     #echo "No composer.ph in $VND"
     continue
  fi

  # Convert each *.src to *.php
  for SRC in $(find $VND -type f -name '*.src')
  do
    TARGET="$(dirname $SRC)/$(basename $SRC .src).php"
    phrep $PKGOPTS $MAINOPTS -i "$SRC" -f -o "$TARGET"
  done
done