#!/bin/bash
#
for line in $(cat typedefs) ; do
    from=$(echo $line | cut -f1 -d:)
    to=$(echo $line | cut -f2 -d:)
    echo "Converting typedef $from to struct $to"

    grep -rP --include=*.[ch] -l $from * | while read file ; do
	sed -r -i -e "s/\b$from\b/struct $to/g" $file
	sed -r -i -e "s/\bP$from\b/struct $to \*/g" $file
	sed -r -i -e "s/struct $to\s*\*\s*\b/struct $to \*/g" $file
	sed -r -i -e "s/\(struct $to\s*\*\)\s*/\(struct $to \*\)/g" $file
    done

    perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+_$from\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\1\};/g; print; }" \
	$(grep -rP -l --include=*.[ch] "\bstruct\s+$to\b" *)

    if [ "$1" == "compile" ] ; then
	cd ../../..
	make drivers/staging/hv/
	echo -n "Press return to commit: "
	read
	cd drivers/staging/hv
    fi
    git commit -s -m "staging/hv: convert typedef $from to struct $to" ../hv

done
