#!/bin/sh

other=""

if [ $1 = "hub" ]; then
 other="leaf"
fi

if [ $1 = "leaf" ]; then
 other="hub"
fi


if test ! -f stamp.$1 && test -f stamp.$other
then
  #Find and remove .c files
  flist="`find src -name \*.c`"
  cfiles="`grep -l \"def [H|L][U|E][B|A]\" $flist`"
  echo "[*] Cleaning up files for $1 build"
  for s in $cfiles 
  do
    bleh="`echo $s | cut -d / -f 2`";
    if [ $bleh = "mod" ]; then
      mod="`echo $s | cut -d / -f 3`";
      os="`echo src/mod/$mod | sed s/\\\.mod/\\\.o/g`";
      rm -f $os
    else
      os="`echo $s | sed s/\\\.c/\\\.o/g`";
      if test -f $os
      then
        if test "`file $os | grep ELF`"
        then
  	  rm -f $os
        else
          if test "`file $os | grep object`"
          then
    	    rm -f $os
          fi
        fi
      fi
    fi
  done
  #Now find and 'touch' header files
  flist="`find src -name \*.h`"
  cfiles="`grep -l \"def [H|L][U|E][B|A]\" $flist`"
  for s in $cfiles
  do
    touch $s
  done
fi
rm -f stamp.*
touch stamp.$1
