#/bin/bash

HELPERS=helpers
UTILITIES=ctl-utilities
WORK_DIR="/tmp/sharedlib"
SRC_DIR="$WORK_DIR/src"
INSTALL_DIR="$WORK_DIR/install"
MYPREFIX="-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR"

if [ -s $INSTALL_DIR/lib/libafb-helpers.so ] && [ -s $INSTALL_DIR/lib/libctl-utilities.so ]
then
    echo "libraries are already installed, force install[y/N]"
    read force
fi

if [[ "$force" == "y" ]]; then
    rm -fr $SRC_DIR
    mkdir -p $SRC_DIR

    function buildinstall() {
        pushd $1
        rm -fr build
        mkdir build
        cd build
        cmake .. $MYPREFIX
        make
        make install
        popd
    }

    pushd $SRC_DIR

    git clone -b sandbox/benierc/shared-library https://gerrit.automotivelinux.org/gerrit/apps/app-afb-helpers-submodule $HELPERS
    git clone -b sandbox/benierc/shared-library https://gerrit.automotivelinux.org/gerrit/apps/app-controller-submodule $UTILITIES

    buildinstall $HELPERS
    export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$INSTALL_DIR/lib/pkgconfig"
    buildinstall $UTILITIES

    popd
fi

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$INSTALL_DIR/lib/pkgconfig"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$INSTALL_DIR/lib"
