#!/bin/bash


if [ ! -d .git ];
then
	echo "in a git directory plz"
	exit 0
fi

if [ -z "$1" ];
then
    # no file provided
    echo "search for code"
    git status --short | grep -E '^\?\?|^ M|^MM|^AM' | grep -E ".ts|.cs" | while read -r line;
    do

        if [[ "$line" =~ ^\?\? ]];
        then
            # new directory , find code in it
            path=$(echo "$line" | awk '{print $2}');
            for FILE in $(find "$path" -iname "*.ts" -o -iname "*.cs" )
            do
                # echo "found new code : $FILE"
                logCode "$FILE"
            done
        else
            # touched file , call logCode with it
            file=$(echo "$line" | awk '{print $2}');
            # echo "found touched code : $file"
            logCode "$file"
        fi
    done
    exit 0;
else
    # one file provided , add log to every function
    # but first remove previously inserted console.log

    echo " found [$1]"

    awk '{ if ($0 !~ /console.log/) print }' "$1" > tmp
    cat tmp > "$1"

    awk -f ~/bin/logCode.awk "$1" > tmp
    cat tmp > "$1"

    rm tmp

    # awk -f ~/bin/logCode.awk "$1"
fi

