#!/bin/bash

rm -rf ./tmpfile
for dir in `cat scripts/filelist.gpl`;
do
    # echo Searching $dir:
    files=`find $dir \( -name \*.[ch] -o -name \*cpp \)`
    # echo Files: $files

    for file in ${files};
    do
    # echo Processing $file
       
     if [ -L "$file" ]; then 		# is it a symlink ?  
       echo Skipping $file - symbolic link
     else

       # Remove "clean" TI Legal headers:
       pattern="connected with the use of this program"
       addlines=4
       minlines=32

       found=`grep -c "$pattern" $file`
       if [ "$found" = "1" ]; then
          # echo Found the header in $file
          line=`grep -n "$pattern" $file | sed "s/:.*//g"`
          line=$(($line+$addlines))
          cond=$(($line < $minlines))
          if [ "$cond" = "1" ]; then
             # echo Removing "clean" TI header from $file
             tail --lines=+$line $file > ./tmpfile
             mv -f ./tmpfile $file
          fi
       fi

       # Add back GPLv2 Copyright
       echo Adding GPLv2 copyright notice to $file
       fname=`echo $file | sed "s/[^.]*\///g"`
       echo \/\*           > ./tmpfile
       echo \ \* $fname   >> ./tmpfile
       tail --lines=+3 scripts/GPL-Header >> ./tmpfile
       cat $file          >> ./tmpfile
       dos2unix ./tmpfile
       mv -f ./tmpfile $file

     fi  # Not symbolic link
    done
done

for dir in `cat scripts/filelist.apache`;
do
    # echo Searching $dir:
    files=`find $dir \( -name \*.[ch] -o -name \*cpp \)`
    # echo Files: $files

    for file in ${files};
    do
    # echo Processing $file
       
     if [ -L "$file" ]; then 		# is it a symlink ?  
       echo Skipping $file - symbolic link
     else

       # Remove "clean" TI Legal headers:
       pattern="connected with the use of this program"
       addlines=4
       minlines=32

       found=`grep -c "$pattern" $file`
       if [ "$found" = "1" ]; then
          # echo Found the header in $file
          line=`grep -n "$pattern" $file | sed "s/:.*//g"`
          line=$(($line+$addlines))
          cond=$(($line < $minlines))
          if [ "$cond" = "1" ]; then
             # echo Removing "clean" TI header from $file
             tail --lines=+$line $file > ./tmpfile
             mv -f ./tmpfile $file
          fi
       fi

       # Add back Apache Copyright
       echo Adding Apache copyright notice to $file
       fname=`echo $file | sed "s/[^.]*\///g"`
       echo \/\*              > ./tmpfile
       echo \ \* $fname      >> ./tmpfile
       tail --lines=+3 scripts/APACHE-Header >> ./tmpfile
       cat $file             >> ./tmpfile
       dos2unix ./tmpfile
       mv -f ./tmpfile $file

     fi  # Not symbolic link
    done
done

for dir in `cat scripts/filelist.bsd`;
do
    # echo Searching $dir:
    files=`find $dir \( -name \*.[ch] -o -name \*cpp \)`
    # echo Files: $files

    for file in ${files};
    do
    # echo Processing $file
       
     if [ -L "$file" ]; then 		# is it a symlink ?  
       echo Skipping $file - symbolic link
     else

       # Remove "clean" TI Legal headers:
       pattern="connected with the use of this program"
       addlines=4
       minlines=32

       found=`grep -c "$pattern" $file`
       if [ "$found" = "1" ]; then
          # echo Found the header in $file
          line=`grep -n "$pattern" $file | sed "s/:.*//g"`
          line=$(($line+$addlines))
          cond=$(($line < $minlines))
          if [ "$cond" = "1" ]; then
             # echo Removing "clean" TI header from $file
             tail --lines=+$line $file > ./tmpfile
             mv -f ./tmpfile $file
          fi
       fi

       # Add back BSD Copyright
       echo Adding BSD copyright notice to $file
       fname=`echo $file | sed "s/[^.]*\///g"`
       echo \/\*              > ./tmpfile
       echo \ \* $fname      >> ./tmpfile
       tail --lines=+3 scripts/BSD-Header >> ./tmpfile
       cat $file             >> ./tmpfile
       dos2unix ./tmpfile
       mv -f ./tmpfile $file

     fi  # Not symbolic link
    done
done

for dir in `cat scripts/filelist.gpl scripts/filelist.apache scripts/filelist.bsd`;
do
    # echo Searching $dir
    files=`grep -l -R -i "CCX" $dir` 
    # echo Files: $files

    for file in ${files};
    do
    # echo Processing $file
       
     if [ -L "$file" ]; then            # is it a symlink ?  
       echo Skipping $file - symbolic link
     else
       sed -e "s/CCX/XCC/ig" $file | sed -e "s/NOXCC/NOCCX/g" > ./tmpfile
       dos2unix ./tmpfile
       mv -f ./tmpfile $file
       echo "CCX Modified in: " $file
     fi
    done
done

