#!/bin/sh

print_usage() {
    cat << EOF
usage: $0 dependency-name

Clones repository, configure, build and install dependency. By default sources
are below './third_party' and installed software is below './builtin/'.

Override default behavior by setting these environment variables:
  - pkg_clone_flags: extra flags for 'git clone' (e.g. '--recursive')
  - pkg_configure_flags: extra flags for './configure' (if any)
  - pkg_cmake_flags: extra flags for 'cmake .' (if CMakeLists.txt exists)
  - pkg_make_check_rule: rule to run tests (default: not set, no tests are run)
  - pkg_make_flags: extra flags for 'make' (e.g. 'V=0')
  - pkg_make_install_flags: extra flags for 'make install' (e.g. DESTDIR=/opt)
  - pkg_prefix: prefix where to install (e.g. '/usr/local')
  - pkg_steps: steps to execute (default is: 'init patch_pre_autogen autogen patch_post_autogen configure make make_check make_install')

Examples:
  - use four processors to compile libressl:
    - pkg_make_flags=-j4 ./build/dependency libressl
EOF
}

apply_patches() {
    if [ -d ../../../build/patch/$1 ]; then
        for diff in `find ../../../build/patch/$1 -type f`; do
            echo "* apply patch: $diff"
            git apply -v $diff
        done
    fi
}

pkg_init() {
    if [ -f Makefile ]; then
        echo "- make distclean"
        make distclean
    fi
}

pkg_patch_pre_autogen() {
    apply_patches pre-autogen/$pkg_name/common
    if [ "$pkg_cross" != "" ]; then
        apply_patches pre-autogen/$pkg_name/$pkg_cross
    fi
}

pkg_autogen() {
    if [ -x ./autogen.sh ]; then
        echo "- ./autogen.sh"
        ./autogen.sh
    elif [ -f ./configure.ac ]; then
        echo "- autoreconf -i"
        autoreconf -i
    fi
}

pkg_patch_post_autogen() {
    apply_patches post-autogen/$pkg_name/common
    if [ "$pkg_cross" != "" ]; then
        apply_patches post-autogen/$pkg_name/$pkg_cross
    fi
}

pkg_configure() {
    if [ -x ./configure ]; then
        echo "- ./configure --prefix=/ $pkg_configure_flags"
        ./configure --prefix=/ $pkg_configure_flags
    elif [ -f ./CMakeLists.txt ]; then
        echo "- cmake -D CMAKE_INSTALL_PREFIX=/ $pkg_cmake_flags ."
        cmake -D CMAKE_INSTALL_PREFIX=/ $pkg_cmake_flags .
    fi
}

pkg_make() {
    echo "- make $pkg_make_flags"
    make $pkg_make_flags
}

pkg_make_check() {
    if [ "$pkg_make_check_rule" != "" ]; then
        echo "- make $pkg_make_flags $pkg_make_check_rule"
        if ! make $pkg_make_flags $pkg_make_check_rule; then
            if [ -f ./test-suite.log ]; then
                cat ./test-suite.log
            fi
            exit 1
        fi
    else
        echo 'not running checks because $pkg_make_check_rule is not set'
    fi
}

pkg_make_install() {
    if [ "$pkg_make_install_rule" = "" ]; then
        pkg_make_install_rule=install
    fi
    vspec=$pkg_branch_or_tag
    if [ "$pkg_tip" != "" ]; then
        vspec="$pkg_tip"
    fi
    based=$pkg_rootdir/third_party/tmp/$pkg_name-$vspec
    adir=$based/$pkg_cross/$pkg_cross_arch
    echo "- make $pkg_make_install_flags $pkg_make_install_rule"
    make $pkg_make_install_flags $pkg_make_install_rule DESTDIR=$adir
    find $adir -depth -type d -name bin -exec rm -rf {} \;
    find $adir -depth -type d -name pkgconfig -exec rm -rf {} \;
    find $adir -depth -type d -name sbin -exec rm -rf {} \;
    find $adir -depth -type f -name \*.la -exec rm -rf {} \;
    tarname=$pkg_rootdir/$pkg_name-$vspec
    tarname=$tarname-$pkg_cross-$pkg_cross_arch
    tarname=$tarname-`date -u +%Y%m%dT%H%M%SZ`
    tarname=$tarname.tgz
    echo ""
    echo "- create archive $tarname"
    (cd $based && tar -cvzf $tarname ./)
    rm -rf $pkg_rootdir/third_party/tmp
    echo ""
    echo "- install from $tarname into $pkg_prefix"
    install -d $pkg_prefix
    (cd $pkg_prefix && tar -xvzf $tarname)
}

pkg_none() {
    echo "- no action"
}

set -e
if [ $# -ne 1 ]; then
    print_usage
    exit 1
fi
pkg_rootdir=$(cd $(dirname $(dirname $0)) && pwd -P)
if [ -z "$pkg_prefix" ]; then
    pkg_prefix=$pkg_rootdir/builtin
fi
export pkg_prefix
cd $pkg_rootdir
# Note that the spec has access to $pkg_rootdir
. build/spec/$1
if [ "$pkg_repository" != "" ]; then
    rm -rf third_party/src/$pkg_name
    git clone $pkg_clone_flags --single-branch --depth 2                       \
        --branch $pkg_branch_or_tag $pkg_repository third_party/src/$pkg_name
    cd third_party/src/$pkg_name
    if [ "$pkg_tip" != "" ]; then
        if [ "`git rev-parse HEAD`" != "$pkg_tip" ]; then
            # Mainly because this allows us to know which commit was the
            # tip of the branch when we compiled a dependency
            echo "FATAL: unexpected HEAD commit SHA-1" 1>&2
            exit 1
        fi
    fi
fi

if [ -z "$pkg_steps" ]; then
    pkg_steps="init patch_pre_autogen autogen patch_post_autogen configure make make_check make_install"
fi
for step in $pkg_steps; do
    echo ""
    echo "./build/dependency: $step"
    echo ""
    pkg_$step
done
echo ""
