#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist/sqlcipher
builddir=/var/tmp/build/[% project %]
mkdir /var/tmp/build
tar -C /var/tmp/dist -xf [% c('input_files_by_name/nss') %]

# application-services uses a newer NDK, 21d, than all the other projects...
export ANDROID_NDK_API_VERSION=[% pc("android-toolchain", "var/android_ndk_version") %][% pc('android-toolchain', 'var/android_ndk_revision') %]
export ANDROID_NDK_HOME=/var/tmp/dist/[% c('var/compiler') %]/android-ndk/android-ndk-r$ANDROID_NDK_API_VERSION
# We need to add the new path to our build tools to PATH
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export ANDROID_NDK_ROOT=$ANDROID_NDK_HOME
export NDK_HOST_TAG=linux-x86_64

export AR="[% c('var/cross_prefix') %]-ar"
# XXX: Mozilla really uses the NDK_API_VERSION here, which is weird.
export CC="[% c('var/cross_prefix') %][% pc('android-toolchain', 'var/android_ndk_version') %]-clang"
export CXX="[% c('var/cross_prefix') %][% pc('android-toolchain', 'var/android_ndk_version') %]-clang++"
export LD="[% c('var/cross_prefix') %]-ld"
export RANLIB="[% c('var/cross_prefix') %]-ranlib"

host=""
[% IF c("var/cross_prefix") == "armv7a-linux-androideabi" -%]
  host="arm-linux"
[% ELSIF c("var/cross_prefix") == "i686-linux-android" -%]
  host="i686-linux"
[% ELSIF c("var/cross_prefix") == "x86_64-linux-android" -%]
  host="x86_64-linux"
[% ELSIF c("var/cross_prefix") == "aarch64-linux-android" -%]
  host="arm-linux"
[% END -%]

SQLCIPHER_CFLAGS=" \
  -DSQLITE_HAS_CODEC \
  -DSQLITE_SOUNDEX \
  -DHAVE_USLEEP=1 \
  -DSQLITE_MAX_VARIABLE_NUMBER=99999 \
  -DSQLITE_THREADSAFE=1 \
  -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \
  -DNDEBUG=1 \
  -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 \
  -DSQLITE_ENABLE_LOAD_EXTENSION \
  -DSQLITE_ENABLE_COLUMN_METADATA \
  -DSQLITE_ENABLE_UNLOCK_NOTIFY \
  -DSQLITE_ENABLE_RTREE \
  -DSQLITE_ENABLE_STAT3 \
  -DSQLITE_ENABLE_STAT4 \
  -DSQLITE_ENABLE_JSON1 \
  -DSQLITE_ENABLE_FTS3_PARENTHESIS \
  -DSQLITE_ENABLE_FTS4 \
  -DSQLITE_ENABLE_FTS5 \
  -DSQLCIPHER_CRYPTO_NSS \
  -DSQLITE_ENABLE_DBSTAT_VTAB \
  -DSQLITE_SECURE_DELETE \
  -DSQLITE_DEFAULT_PAGE_SIZE=32768 \
  -DSQLITE_MAX_DEFAULT_PAGE_SIZE=32768 \
  -I/var/tmp/dist/nss/include \
"

LIBS="\
  -lcertdb \
  -lcerthi \
  -lcryptohi \
  -lfreebl_static \
  -lnspr4 \
  -lnss_static \
  -lnssb \
  -lnssdev \
  -lnsspki \
  -lnssutil \
  -lpk11wrap_static \
  -lplc4 \
  -lplds4 \
  -lsoftokn_static \
"

[% IF c("var/cross_prefix") == "i686-linux-android" || c("var/cross_prefix") == "x86_64-linux-android"-%]
  LIBS="${LIBS} -lgcm-aes-x86_c_lib"
[% END %]
[% IF c("var/cross_prefix") == "armv7a-linux-android" || c("var/cross_prefix") == "aarch64-linux-android"-%]
   LIBS="${LIBS} -larmv8_c_lib"
[% END %]
[% IF c("var/cross_prefix") == "aarch64-linux-android" -%]
  LIBS="${LIBS} -lgcm-aes-aarch64_c_lib"
[% END %]
[% IF c("var/cross_prefix") == "armv7a-linux-androideabi" -%]
  LIBS="${LIBS} -lgcm-aes-arm32-neon_c_lib"
[% END %]
[% IF c("var/cross_prefix") == "x86_64-linux-android"-%]
  LIBS="${LIBS} -lintel-gcm-wrap_c_lib -lintel-gcm-s_lib -lhw-acc-crypto-avx -lhw-acc-crypto-avx2"
[% END %]

tar -C /var/tmp/build -xf [% c('input_files_by_name/sqlcipher') %]
mv /var/tmp/build/[% project %]-[% c('version') %] $builddir
cd $builddir

mkdir $builddir/build
cd build
../configure \
  --host="$host" \
  --with-pic \
  --verbose \
  --disable-shared \
  --with-crypto-lib=none \
  --disable-tcl \
  --enable-tempstore=yes \
  CFLAGS="${SQLCIPHER_CFLAGS}" \
  LDFLAGS="-L/var/tmp/dist/nss/lib" \
  LIBS="${LIBS} -llog -lm"

make sqlite3.h
make sqlite3ext.h
make libsqlcipher.la

mkdir -p $distdir/include/sqlcipher
mkdir -p $distdir/lib

cp -p "$builddir/build/sqlite3.h" "$distdir/include/sqlcipher"
cp -p "$builddir/build/sqlite3ext.h" "$distdir/include/sqlcipher"
cp -p "$builddir/build/.libs/libsqlcipher.a" "$distdir/lib"

# Just in case, ensure that the created binaries are not -w.
chmod +w "$distdir/lib/libsqlcipher.a"

cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
