#!/bin/bash

# 07Oct2019, Maiko (VE4KLM), the beginnings of a ./configure script for JNOS 2.0
# 12Oct2019, Maiko, B2F now a permanent directive, introduced a new WINLINK_SECURE_LOGIN directive
# 17Jan2020, Maiko, Runtime Candidate, no more FTP users file

rm -f configure.okay

CFGERR=2

echo -e "\n JNOS 2.0 ./configure script Version 1.5 - 22Feb2020, Maiko Langelaar (VE4KLM)\n"

# make sure the makefile is compatible with this configure script
grep 'configure_22Feb2020_v1.5' makefile > /dev/null 2>&1 && echo "1" > makefile.okay || rm -f makefile.okay

if [ -f makefile.okay ]; then
        echo -e "\tmakefile is compatible with this script\n"
else
        echo -e "\tmakefile is NOT compatible with this script\n" ; exit 1;
fi

if [ -f config.h ]; then
  echo -e "\tconfig.h found, leave it alone\n"
else
  echo -e "\tconfig.h missing, put in the default\n"
  cp config.h.default config.h
fi

# Check for presence of ncurses development package
echo "#include <curses.h>" > testcfg.c
cc -c testcfg.c > /dev/null 2>&1 && ((CFGERR--)) && echo -e "\tncurses development installed\n" || echo -e "The ncurses development package is not installed, use :\n\n   RHEL systems : yum install ncurses-devel\n\n UBUNTU systems : sudo apt-get install libncurses5-dev libncursesw5-dev\n"

# Check for present of #define B2F in config.h
# 12Oct2019, Maiko, B2F is now permanent, use WINLINK_SECURE_LOGIN instead
# 23Feb2020, Maiko, Now that I am using MD5 for hash:salt user authentication we ALLWAYS need this library now
#grep 'define WINLINK_SECURE_LOGIN' config.h > /dev/null 2>&1 && echo "1" > configure.wlsl && echo -e "\tWINLINK_SECURE_LOGIN is defined in config.h\n" || rm -f configure.wlsl

# Check for present of #define MD5AUTHENTICATE in config.h
# 21Feb2020, Maiko, Need to tell j2pwmgrV2 we have MD5AUTHENTICATE in affect
#
# 26Feb2020, Maiko, changed the grep to using two greps with a pipe, this works
# very well for finding spaces, tabs, or combination of, within the define.
#
grep MD5AUTHENTICATE config.h | grep define > /dev/null 2>&1 && echo "1" > configure.md5a && echo -e "\tMD5AUTHENTICATE is defined in config.h\n" || rm -f configure.md5a

# Check for presence of openssl development package
# 23Feb2020, Maiko (VE4KLM), We ALWAYS need it now that we are using hash:salt user authentication
#if [ -f configure.wlsl ]; then
 echo "#include <openssl/md5.h>" > testcfg.c
 cc -c testcfg.c > /dev/null 2>&1 && ((CFGERR--)) && echo -e "\topenssl development installed\n" || echo -e "The openssl development package is not installed, use :\n\n   RHEL systems : yum install openssl-devel\n\n UBUNTU systems : sudo apt-get install libssl-dev\n"
#else
#((CFGERR--))
#fi

# 25Feb2020, Maiko, Check if INP2001 is defined, and recommend people #undef it
#
# 26Feb2020, Maiko, changed the grep to using two greps with a pipe, this works
# very well for finding spaces, tabs, or combination of, within the define.
#
grep INP2011 config.h | grep define > /dev/null 2>&1 && echo -e "\tINP2011 is defined in config.h - recommending '#undef INP2011' !!!\n"

# 26Feb2020, Maiko, Let people know we are forcing signed char gcc option
echo -e "\tforcing compiler option '-fsigned-char'\n\t (critical for PI devices and ARM based systems)\n"

# Remove any temporary files generated by this script
rm -f testcfg.c testcfg.o

# Only allow the make to proceed if no configuration errors
if [ $CFGERR -eq 0 ]; then
 echo "1" > configure.okay ; echo -e " You should now be able to run 'make' - good luck.\n"
fi

exit $CFGERR

