#!/bin/bash

root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
#root_dir="$(readlink -f "$(dirname "$0")")/.."
cd "$root_dir"
dir=$(test -n "$1" && echo "$1" || echo "${HOME}/bitcoin")
patch_file="$(pwd)/etc/bitcoin.patch"

cd "$dir" || exit 1

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

if test $? -ne 0; then
  echo 'Unable to checkout necessary commit.'
  echo 'Please pull the latest HEAD from the upstream bitcoin repo.'
  exit 1
fi
git checkout -b "libbitcoind-$(date '+%Y.%m.%d')" || exit 1

patch -p1 < "$patch_file" || exit 1

git add --all || exit 1

[ -n "$( git config user.name )" ] \
    || git config user.name 'Bitcore Build'

[ -n "$( git config user.email )" ] \
    || git config user.email "$( id -n -u )@$( hostname -f )"

git commit -a -m 'allow compiling of libbitcoind.so.' || exit 1

echo 'Patch completed successfully.'
exit 0
