#!/bin/bash
SUCCESS=0
FAIL=0
COUNTER=0
DIFF=""
RED=`tput setaf 1`
GREEN=`tput setaf 2`
BASE=`tput setaf 7`

s21_command=(
    "./s21_grep"
    )
sys_command=(
    "grep"
    )

flags=(
    "v"
    "c"
    "l"
    "n"
    "h"
    "o"
)

tests=(
"s test_files/test_0_grep.txt FLAGS"
"for s21_grep.c s21_grep.h Makefile FLAGS"
"for s21_grep.c FLAGS"
"-e for -e ^int s21_grep.c s21_grep.h Makefile FLAGS"
"-e for -e ^int s21_grep.c FLAGS"
"-e regex -e ^print s21_grep.c FLAGS -f test_files/test_ptrn_grep.txt"
"-e while -e void s21_grep.c Makefile FLAGS -f test_files/test_ptrn_grep.txt"
)

manual=(
"-n for test_files/test_1_grep.txt test_files/test_2_grep.txt"
"-n for test_files/test_1_grep.txt"
"-n -e ^\} test_files/test_1_grep.txt"
"-c -e /\ test_files/test_1_grep.txt"
"-ce ^int test_files/test_1_grep.txt test_files/test_2_grep.txt"
"-e ^int test_files/test_1_grep.txt"
"-nivh = test_files/test_1_grep.txt test_files/test_2_grep.txt"
"-e"
"-ie INT test_files/test_5_grep.txt"
"-echar test_files/test_1_grep.txt test_files/test_2_grep.txt"
"-ne = -e out test_files/test_5_grep.txt"
"-iv int test_files/test_5_grep.txt"
"-in int test_files/test_5_grep.txt"
"-c -l aboba test_files/test_1_grep.txt test_files/test_5_grep.txt"
"-v test_files/test_1_grep.txt -e ank"
"-noe ) test_files/test_5_grep.txt"
"-l for test_files/test_1_grep.txt test_files/test_2_grep.txt"
"-o -e int test_files/test_4_grep.txt"
"-e = -e out test_files/test_5_grep.txt"
"-noe ing -e as -e the -e not -e is test_files/test_6_grep.txt"
"-e ing -e as -e the -e not -e is test_files/test_6_grep.txt"
"-c -e . test_files/test_1_grep.txt -e '.'"
"-l for no_file.txt test_files/test_2_grep.txt"
"-e int -si no_file.txt s21_grep.c no_file2.txt s21_grep.h"
"-si s21_grep.c -f no_pattern.txt"
"-f test_files/test_3_grep.txt test_files/test_5_grep.txt"
)

run_test() {
    param=$(echo "$@" | sed "s/FLAGS/$var/")
    "${s21_command[@]}" $param > "${s21_command[@]}".log
    "${sys_command[@]}" $param > "${sys_command[@]}".log
    DIFF="$(diff -s "${s21_command[@]}".log "${sys_command[@]}".log)"
    let "COUNTER++"
    if [ "$DIFF" == "Files "${s21_command[@]}".log and "${sys_command[@]}".log are identical" ]
    then
        let "SUCCESS++"
        echo "[${GREEN}${SUCCESS}${BASE}/${RED}${FAIL}${BASE}] ${GREEN} Success ${BASE} $param"
    else
        let "FAIL++"
        echo "[${GREEN}${SUCCESS}${BASE}/${RED}${FAIL}${BASE}] ${RED} Fail ${BASE} $param"
    fi
    rm -f "${s21_command[@]}".log "${sys_command[@]}".log
}

echo "^^^^^^^^^^^^^^^^^^^^^^^"
echo "TESTS WITH NORMAL FLAGS"
echo "^^^^^^^^^^^^^^^^^^^^^^^"
printf "\n"
echo "#######################"
echo "MANUAL TESTS"
echo "#######################"
printf "\n"

# for i in "${manual[@]}"
# do
#     var="-"
#     run_test "$i"
# done

printf "\n"
echo "#######################"
echo "AUTOTESTS"
echo "#######################"
printf "\n"
echo "======================="
echo "1 PARAMETER"
echo "======================="
printf "\n"

for var1 in "${flags[@]}"
do
    for i in "${tests[@]}"
    do
        var="-$var1"
        run_test "$i"
    done
done
printf "\n"
echo "======================="
echo "2 PARAMETERS"
echo "======================="
printf "\n"

for var1 in "${flags[@]}"
do
    for var2 in "${flags[@]}"
    do
        if [ $var1 != $var2 ]
        then
            for i in "${tests[@]}"
            do
                var="-$var1 -$var2"
                run_test "$i"
            done
        fi
    done
done
# printf "\n"
# echo "======================="
# echo "3 PARAMETERS"
# echo "======================="
# printf "\n"
# for var1 in "${flags[@]}"
# do
#     for var2 in "${flags[@]}"
#     do
#         for var3 in "${flags[@]}"
#         do
#             if [ $var1 != $var2 ] && [ $var2 != $var3 ] && [ $var1 != $var3 ]
#             then
#                 for i in "${tests[@]}"
#                 do
#                     var="-$var1 -$var2 -$var3"
#                     run_test "$i"
#                 done
#             fi
#         done
#     done
# done
# printf "\n"
# echo "======================="
# echo "4 PARAMETERS"
# echo "======================="
# printf "\n"
# for var1 in "${flags[@]}"
# do
#     for var2 in "${flags[@]}"
#     do
#         for var3 in "${flags[@]}"
#         do
#             for var4 in "${flags[@]}"
#             do
#                 if [ $var1 != $var2 ] && [ $var2 != $var3 ] \
#                 && [ $var1 != $var3 ] && [ $var1 != $var4 ] \
#                 && [ $var2 != $var4 ] && [ $var3 != $var4 ]
#                 then
#                     for i in "${tests[@]}"
#                     do
#                         var="-$var1 -$var2 -$var3 -$var4"
#                         run_test "$i"
#                     done
#                 fi
#             done
#         done
#     done
# done
# printf "\n"
# echo "#######################"
# echo "AUTOTESTS"
# echo "#######################"
# printf "\n"
# echo "======================="
# echo "DOUBLE PARAMETER"
# echo "======================="
# printf "\n"
#
# for var1 in "${flags[@]}"
# do
#     for var2 in "${flags[@]}"
#     do
#         if [ $var1 != $var2 ]
#         then
#             for i in "${tests[@]}"
#             do
#                 var="-$var1$var2"
#                 run_test "$i"
#             done
#         fi
#     done
# done
#
# printf "\n"
# echo "#######################"
# echo "AUTOTESTS"
# echo "#######################"
# printf "\n"
# echo "======================="
# echo "TRIPLE PARAMETER"
# echo "======================="
# printf "\n"
#
# for var1 in "${flags[@]}"
# do
#     for var2 in "${flags[@]}"
#     do
#         for var3 in "${flags[@]}"
#         do
#             if [ $var1 != $var2 ] && [ $var2 != $var3 ] && [ $var1 != $var3 ]
#             then
#                 for i in "${tests[@]}"
#                 do
#                     var="-$var1$var2$var3"
#                     run_test "$i"
#                 done
#             fi
#         done
#     done
# done
printf "\n"
echo "FAIL: $FAIL"
echo "SUCCESS: $SUCCESS"
echo "ALL: $COUNTER"
printf "\n"
##############################
