#!/bin/bash
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
options=`cat ${root_dir}/bin/config_options.sh`
h_and_a_dir=$($root_dir/platform/os.sh h_and_a_dir)
depends_dir=$($root_dir/platform/os.sh depends_dir)
os_dir=$(${root_dir}/platform/os.sh osdir)
host=$(${root_dir}/platform/os.sh host)
btc_dir="${root_dir}/libbitcoind"
ext=$($root_dir/platform/os.sh ext)
artifacts_dir=$($root_dir/platform/os.sh artifacts_dir)
export CPPFLAGS="-I${h_and_a_dir}/include/boost -I${h_and_a_dir}/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include -L${h_and_a_dir}/lib"
echo "Using BTC directory: ${btc_dir}"

cd "${root_dir}"

copy_header_files () {
  if [[ -d "${artifacts_dir}" && -d "${h_and_a_dir}" && -d "${btc_dir}" ]]; then
    mkdir -p "${artifacts_dir}/include" > /dev/null 2>&1
    pushd "${root_dir}"
    echo "Copying headers for caching purposes, this can take a while (but will only be done once), please wait..."
    find libbitcoind -type f \( -name "*.h" -or -name "*.hpp" -or -name "*.ipp" \) -print0 | xargs -0 -I{} rsync -R {} "${artifacts_dir}"/include
    mkdir -p "${artifacts_dir}/lib" > /dev/null 2>&1
    cp -r "${h_and_a_dir}"/lib/libboost_filesystem-mt.a "${h_and_a_dir}"/lib/libboost_thread-mt.a "${h_and_a_dir}"/lib/libboost_chrono-mt.a \
    "${h_and_a_dir}"/lib/libboost_program_options-mt.a "${h_and_a_dir}"/lib/libboost_system-mt.a  "${btc_dir}"/src/secp256k1/.libs/libsecp256k1.a \
    "${btc_dir}"/src/.libs/libbitcoind.a "${btc_dir}"/src/leveldb/libleveldb.a "${btc_dir}"/src/leveldb/libmemenv.a "${artifacts_dir}"/lib/
    if [ "${test}" = true ]; then
      cp -r "${h_and_a_dir}"/lib/libdb* "${artifacts_dir}"/lib/
    fi
    popd
  fi
}

build_dependencies () {
  if [ -d "${btc_dir}" ]; then
    pushd "${depends_dir}"
    echo "using host for dependencies: ${host}"
    boost_files_count=`find "${h_and_a_dir}"/lib -iname "libboost_*-mt.a" | wc -l | xargs`
    db_files_count=`find "${h_and_a_dir}"/lib -iname "libdb*.a" | wc -l | xargs`
    should_rebuild=false
    if [[ "${boost_files_count}" -lt 5 || ( "${db_files_count}" -lt 1  && "${test}" = true ) ]]; then
      should_rebuild=true
    fi
    if [ "${should_rebuild}" = true ]; then
      if [ "${test}" = true ]; then
        make HOST=${host} NO_QT=1 NO_UPNP=1
      else
        make HOST=${host} NO_QT=1 NO_WALLET=1 NO_UPNP=1
      fi
    else
      echo "Looks like libs are already built, so we won't rebuild them. Incidentally, we found: ${boost_files_count} boost libraries and: ${db_files_count} Berkeley DB libraries and you have test mode set to: ${test}"
    fi
    popd
  fi 
}

get_patch_file () {
  if test -e "${root_dir/PATCH_VERSION}"; then
    tag=`cat "${root_dir}/PATCH_VERSION" | xargs`
  else
    echo "no tag file found, please create it in the root of the project as so: 'echo \"v0.10.2\" > PATCH_VERSION'"
    exit 1
  fi
}

compare_patch () {
  cd "${btc_dir}"
  get_patch_file
  echo "running the diff command from HEAD to ${tag}"
  git diff ${tag}..HEAD > /tmp/tmp.patch
  matching_patch=`diff -w /tmp/tmp.patch "${root_dir}/etc/bitcoin.patch"` 
}

debug=
if [ "${BITCORENODE_ENV}" == "debug" ]; then
  options=`cat ${root_dir}/bin/config_options_debug.sh`
fi

test=false
if [ "${BITCORENODE_ENV}" == "test" ]; then
  test=true
  options=`cat ${root_dir}/bin/config_options_test.sh`
fi

patch_file_sha=$(shasum -a 256 "${root_dir}/etc/bitcoin.patch" | awk '{print $1}')
patch_sha=`find "${os_dir}" -iname patch_sha.txt`
last_patch_file_sha=
if [ -e "${patch_sha}" ]; then 
  if [ "${ext}" == "dylib" ]; then
    last_patch_file_sha=$(cat "${os_dir}"/lib/patch_sha.txt)
  else
    last_patch_file_sha=$(cat "${os_dir}"/patch_sha.txt)
  fi
fi
shared_file_built=false
if [[ "${last_patch_file_sha}" == "${patch_file_sha}" && -d "${artifacts_dir}/include" ]]; then
  shared_file_built=true
fi

if [ "${shared_file_built}" = false ]; then
  mac_response=$($root_dir/platform/os.sh mac_dependencies) 
  if [ "${mac_response}" != "" ]; then
    echo "${mac_response}" 
    exit -1
  fi
  only_make=false
  if [ -d "${btc_dir}" ]; then
    echo "running compare patch..."
    compare_patch
    repatch=false
    if [[ "${matching_patch}" =~ [^\s\\] ]]; then
      echo "Warning! libbitcoind is not patched with:\
        ${root_dir}/etc/bitcoin.patch."
      echo -n "Would you like to remove the current patch, checkout the tag: ${tag} and \
apply the current patch from "${root_dir}"/etc/bitcoin.patch? (y/N): "
      if [ "${BITCORENODE_ASSUME_YES}" = true ]; then
        input=y
        echo ""
      else
        read input
      fi
      if [[ "${input}" =~ ^y|^Y ]]; then
        repatch=true
        rm -f "${os_dir}/libbitcoind.*"
        echo "Removing directory: \"${btc_dir}\" and starting over!"
        rm -fr "${btc_dir}"
      fi
    fi
    if [ "${repatch}" = false ]; then
      echo "Running make inside libbitcoind (assuming you've previously patched and configured libbitcoind)..."
      cd "${btc_dir}"
      only_make=true
    fi
  fi

  if [ "${only_make}" = false ]; then
    echo "Cloning, patching, and building libbitcoind..."
    get_patch_file
    echo "attempting to checkout tag: ${tag} of bitcoin from github..."
    cd "${root_dir}"
    git clone --depth 1 --branch "${tag}" https://github.com/bitcoin/bitcoin.git libbitcoind

    cd "${btc_dir}"

    echo '../patch-bitcoin.sh' "${btc_dir}"
    ../bin/patch-bitcoin "${btc_dir}"

    if ! test -d .git; then
      echo 'Please point this script to an upstream bitcoin git repo.'
      exit 1
    fi

  fi
  build_dependencies
  echo './autogen.sh'
  ./autogen.sh

  boost_libdir="--with-boost-libdir=${h_and_a_dir}/lib"

  full_options="${options} ${boost_libdir} --prefix=${os_dir}"
  echo "running the configure script with the following options:\n :::[\"${full_options}\"]:::"
  ${full_options}

  echo 'make V=1'
  make V=1

  echo 'Copying libbitcoind.{so|dylib} to its appropriate location.'
  if test -e "${btc_dir}/src/.libs/libbitcoind.a"; then
    mkdir -p "${artifacts_dir}"
    cp -R "${btc_dir}"/src/.libs/libbitcoind.a "${artifacts_dir}"
    echo "Creating the sha marker for the patching in libbitcoind..."
    echo -n `shasum -a 256 "${root_dir}"/etc/bitcoin.patch | awk '{print $1}'` > "${artifacts_dir}"/patch_sha.txt
    echo "Copying the header files in order to be cached..."
    copy_header_files
  else
    echo "Could not find the libraries after they should have been built, please run make clean inside the libbitcoind dir and run npm install again."
    exit 1
  fi
  echo 'Build finished successfully.'
else
  echo 'Using existing shared library.'
fi

