#!/bin/bash


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



SQLDATABASE='APSS_CurrentSprint'
SQLDATABASE2='APSS_CurrentSprint_IntegrationTests'
SQLSERVER='CAL250936'
SQLUSER='sa'
SQLPASS='Defec@t1on'
#CONNECTIONSTRING="metadata=res:\/\/*\/CommonEntities.csdl|res:\/\/*\/CommonEntities.ssdl|res:\/\/*\/CommonEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data source=$SQLSERVER;Initial catalog=$SQLDATABASE;User Id=$SQLUSER;Password=$SQLPASS;MultipleActiveResultSets=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"

#CONNECTIONSTRING="Data source=$SQLSERVER;Initial catalog=$SQLDATABASE;User Id=$SQLUSER;Password=$SQLPASS;MultipleActiveResultSets=True;MultipleActiveResultSets=True;App=EntityFramework"
#CONNECTIONSTRING2="Data source=$SQLSERVER;Initial catalog=$SQLDATABASE2;User Id=$SQLUSER;Password=$SQLPASS;MultipleActiveResultSets=True;MultipleActiveResultSets=True;App=EntityFramework"

CONNECTIONSTRING="Data source=$SQLSERVER;Initial catalog=$SQLDATABASE;User Id=$SQLUSER;Password=$SQLPASS;MultipleActiveResultSets=True;MultipleActiveResultSets=True;App"
CONNECTIONSTRING2="Data source=$SQLSERVER;Initial catalog=$SQLDATABASE2;User Id=$SQLUSER;Password=$SQLPASS;MultipleActiveResultSets=True;MultipleActiveResultSets=True;App"



T="$(date +%s)"

NARROW=${1:-''}

REPO="$(pwd | xargs cygpath --windows | sed 's|cygwin64\\c\\||' | sed 's/\\/\\\\/g')"
REPOJSON="$(pwd | xargs cygpath --windows  | sed 's|cygwin64\\c\\||' | sed 's/\\/\\\\\\\\/g')"

echo "$CONNECTIONSTRING"

for FILE in $(find -iname "app.config" -o -iname "web.config" -o -iname "appsettings.json" | grep "$NARROW" )
do
	if grep -qi "metadata=res" "$FILE"
	then
		printf "$FILE";

		git update-index -q --assume-unchanged $FILE

		if grep -qi "IntegrationTests" "$FILE"
		then
			sed -i "s/Data source=.*App/$CONNECTIONSTRING2/IM" $FILE;
		else
			sed -i "s/Data source=.*App/$CONNECTIONSTRING/IM" $FILE;
		fi

		if [[ "$FILE" == *.config ]]; then
		    sed -i "s=%LUMEDPROJECT_GIT%=$REPO=IM" $FILE;
		elif [[ "$FILE" == *.json ]]; then
		    sed -i "s=%LUMEDPROJECT_GIT%=$REPOJSON=IM" $FILE;
		fi
		printf "\n"
	fi
done

TT="$(($(date +%s)-T))"

#vs2022

echo "done in ${TT} sec"

