#!/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

rm -f configure.okay

CFGERR=2

echo -e "\n JNOS 2.0 ./configure script Version 1.7 - 30Aug2020, Maiko Langelaar (VE4KLM)\n"

# make sure the makefile is compatible with this configure script
grep 'configure_30Aug2020_v1.7' 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
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 presence of openssl development package
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

# Warn folks RDATECLI will not be supported soon (gcc 10 and stime() no more)
# 03Aug2020, Maiko (VE4KLM), I don't see the point, just use ntp linux side.
grep 'define RDATECLI' config.h > /dev/null 2>&1 && echo -e "  you should #undef RDATECLI in config.h and use ntp services linux side\n  *compile will fail with gcc version 10, no point supporting anymore ?\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

