# adapt_test_src_file - Make changes to openssl test source file for vxWorks
#
# Copyright (c) 2019, 2021-2022 Wind River Systems, Inc.
#      
# The right to copy, distribute, modify or otherwise make use
# of this software may be licensed only pursuant to the terms
# of an applicable Wind River license agreement.
#
#/bin/sh

. $(dirname "$0")/common


####################################################
### Adapt each test source file                  ###
####################################################
for runnable_file in ${runnable_file_list}
do

runnable_file_no_suffix=${runnable_file%.c}

searched=$(grep "int ${runnable_file_no_suffix}(int argc, char .argv..)" ${runnable_file})
if [ ! -z "${searched}" ]; then
	continue
fi

cat <<EOF >> ${runnable_file}

#if defined(OPENSSL_SYS_VXWORKS_SUPPORT)
int ${runnable_file_no_suffix}(int argc, char *argv[]);
int ${runnable_file_no_suffix}(int argc, char *argv[])
{
    int ret = 0;
    char * testfn = "${runnable_file_no_suffix}";
    char * argvTemp[1];

    if (argc == 0)
    {
        argc++;
        argvTemp[0] = testfn;
        argv = argvTemp;
    }

    test_main(argc, argv);

    if(0 == setup_tests())
	{
		ret = 1;
	}
    if(EXIT_SUCCESS != run_tests(testfn))
	{
		ret = 2;
	}
    TEST_info("%-30s %s ",testfn, (ret == EXIT_SUCCESS) ? "successfully":"failed");
    remove_all_tests();
    return ret;
}
#endif
EOF

sed -i "/int setup_tests(void)/{s/^/static /};/void cleanup_tests(void)/{s/^/static /};/int global_init(void)/{s/^/static /};/const OPTIONS \*test_get_options(void)/{s/^/static /}" ${runnable_file}

done

sed -i "/int setup_tests(void)/{s/^/static /};/void cleanup_tests(void)/{s/^/static /};/int global_init(void)/{s/^/static /};/const OPTIONS \*test_get_options(void)/{s/^/static /}" testutil.h 
