#
# the source for the configure options used in this script is: https://github.com/termux/termux-packages/blob/master/packages/openssh/build.sh
#
#
# define the API and the NDK version to use
# 
API=33
NDK=/data/develop/android/android-ndk-r27b

TOOLCHAIN=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64

TARGET=aarch64-linux-android

SYSROOT=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/

TARGET_ROOT=/data/develop/android/sysroot
TARGET_SYSROOT=/data/develop/android/sysroot

ANDROID_NDK_HOME=/data/develop/android/android-ndk-r27b

LDFLAGS="--sysroot /data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/  -s -ffunction-sections -fdata-sections -Wl,--gc-sections -L/data/develop/android/sysroot/usr/lib -L/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot//lib" 

CFLAGS="--sysroot /data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/ -O2 -w -I/data/develop/android/sysroot/usr/include -I/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot//usr/include -Wno-implicit-function-declaration -Wno-int-conversion -fPIE"

AR=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar
AS=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang
STRIP=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
CC=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang
RANLIB=/data/develop/android/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ranlib

RESTORE_LDNS_CONFIG=1

# workaround to disable the use of the wrong ldsn-config file by the configure script
# #
if [ -r /usr/bin/ldns-config -a ! -r /usr/bin/ldns-config.org ] ; then
  echo "Renaming /usr/bin/ldns-config to /usr/bin/ldns-config.org ..."
  sudo mv /usr/bin/ldns-config /usr/bin/ldns-config.org
  RESTORE_LDNS_CONFIG=0
fi

# export CFLAGS="$CFLAGS  -Wno-implicit-int -DHAVE_ATTRIBUTE__SENTINEL__=1"
export CPPFLAGS+=" -DHAVE_ATTRIBUTE__SENTINEL__=1 -DBROKEN_SETRESGID "
export CXXFALGS="${CPPFLAGS}"

export LD=$CC

# destination for the compiled OpenSSH
# 
export TARGET_ROOT=/system

# config arguments for the configure script
# 
  echo "Creating the file configure_arguments ..."

  cat >configure_arguments <<EOF
--disable-etc-default-login
--disable-lastlog
--disable-libutil
--disable-pututline
--disable-pututxline
--disable-strip
--disable-utmp
--disable-utmpx
--disable-wtmp
--disable-wtmpx
--sysconfdir=${TARGET_ROOT}/etc/ssh
--with-cflags=-Dfd_mask=int
--with-libedit
--without-stackprotect
--with-pid-dir=/data/local/tmp/var/run
--with-privsep-path=/data/local/tmp/var/empty
 --with-privsep-user=nobody
--with-xauth=${TARGET_ROOT}/bin/xauth
--with-default-path=${TARGET_ROOT}/bin
--with-ldns=$TARGET_ROOT/usr
--with-maildir=/data/local/tmp/var/mail
ac_cv_func_endgrent=yes
ac_cv_func_fmt_scaled=no
ac_cv_func_getlastlogxbyname=no
ac_cv_func_readpassphrase=no
ac_cv_func_strnvis=no
ac_cv_header_sys_un_h=yes
ac_cv_lib_crypt_crypt=no
ac_cv_search_getrrsetbyname=no
ac_cv_func_bzero=yes
-with-ssl-dir=/data/develop/android/sysroot/usr
EOF


CFLAGS="$CFLAGS -I/data/develop/android/sysroot/selinux/usr/include -DHAVE_SETRESGID=1" 
LDFLAGS="$LDFLAGS -L/data/develop/android/sysroot/selinux/usr/lib"
LDFLAGS="$LDFLAGS -ldl -lcrypto -lssl -lpcre2 " 

echo "Configuring for installing in the directory ${TARGET_ROOT} ..."

$PREFIX  ./configure --host ${TARGET} --target=${TARGET} --prefix=${TARGET_ROOT}/usr/ $( cat configure_arguments )   --datarootdir=${TARGET_ROOT}/usr  --with-selinux --with-libedit=/data/local/tmp/develop/sysroot/usr $*

find . -name Makefile -exec sed -i -e "s/-DBROKEN_SETRESGID//g"  {} \;

echo 

if [ $RESTORE_LDNS_CONFIG = 0 -a ! -r /usr/bin/ldns-config ] ; then
  echo "Restoring /usr/bin/ldns-config ..."
  if [ ! -r /usr/bin/ldns-config.org ] ; then
    echo "ERROR: The file /usr/bin/ldns-config.org does NOT exist"
  else
    sudo mv /usr/bin/ldns-config.org /usr/bin/ldns-config
  fi
fi


# LDFLAGS="$LDFLAGS -ldl -lcrypto -lssl" ./myconfigure --with-selinux --with-libedit=/data/local/tmp/develop/sysroot/usr
