#!/bin/bash

# This utility must be run in root of distro:

if [ ! -d "lsvmload" ]; then
    echo "$0: must be run from root of source directory"
    exit 1
fi

if [ ! -f "LICENSE" ]; then
    echo "$0: LICENSE file not found"
    exit 1
fi

# Generate license.h:
licensefile=`/bin/mktemp`
echo "/*" >> $licensefile
echo "**==============================================================================" >> $licensefile
echo "**" >> $licensefile
cat LICENSE | sed 's/.*/** &/g' >> $licensefile
echo "**" >> $licensefile
echo "**==============================================================================" >> $licensefile
echo "*/" >> $licensefile

# Prepend license.h to each file that does not already have it:
files=`find . -name '*.[ch]'`
for i in ${files}
do
    grep -q "^\*\* MIT License" $i
    if [ "$?" != "0" ]; then
        echo "Patching $i"
        cat ${licensefile} > $i.bak
        cat $i >> $i.bak
        cp $i.bak $i
        rm -f $i.bak
    fi
done
