#!/bin/sh
set -e
if [ "$1" = "clean" ] ; then
    echo Cleaning libximc...
    rm -f "$LIBSDIR/libximc.a"
    rm -f "$LIBSDIR/libximc.so"
    rm -f "$LIBSDIR/libximc.so.7"
    rm -f "$LIBSDIR/libximc.so.7.0.0"
    exit 0
fi

echo '----------------------------------------------------'
echo '                                                    '
echo '                  BUILDING LIBXIMC                  '
echo '                                                    '
echo '----------------------------------------------------'

echo '----------------------------------------------------'
echo '              Checking dependencies...              '
echo '----------------------------------------------------'
set +e

echo Checking libximc sources that had to be generated by xigen...
set MISSING_FILES_DETECTED=false
FILE_LIST="$LIBXIMC_INC_DIR/ximc.h
    $LIBXIMC_SRC_DIR/ximc-gen.h
    $LIBXIMC_SRC_DIR/ximc-gen.c
    $LIBXIMC_SRC_DIR/libximc.def
    $LIBXIMC_SRC_DIR/fwprotocol.h
    $LIBXIMC_SRC_DIR/fwprotocol.c"
for file in $FILE_LIST ; do
    if [ ! -e "$file" ] ; then
        echo "$file cannot be found. ERROR!"
        MISSING_FILES_DETECTED=true
    else
        echo "$file is found."
    fi
done
[ -n "$MISSING_FILES_DETECTED" ] && exit 1
echo '***** Done!'

set -e
echo '------- Dependencies checking has been done --------'

TARGETS="all dist install"


env $SPECIAL_ENV make $TARGETS DISTCHECK_CONFIGURE_FLAGS="$PACKAGE_EXTRA_CONFIGURE"
unset COPYFILE_DISABLE

if [ "$OS_NAME" = "Darwin" ] ; then
    echo Copying the result to output directory...
    mkdir -p "$LIBSDIR"
    echo Fixing library dependency paths...
    install_name_tool -change libxiwrapper.dylib    @loader_path/Frameworks/libxiwrapper.dylib  "$DISTDIR/local/lib/libximc.framework/Versions/7/libximc"
    install_name_tool -change libbindy.dylib        @loader_path/Frameworks/libbindy.dylib      "$DISTDIR/local/lib/libximc.framework/Versions/7/libximc"
    cp -R "$DISTDIR/local/lib/libximc.framework" "$LIBSDIR"
    echo '----------------------------------------------------'
    echo '           BUILDING LIBXIMC IS COMPLETED            '
    echo '----------------------------------------------------'
    echo "* $LIBSDIR/libximc.framework                        "
    echo '----------------------------------------------------'
    exit 0
else
    echo Copying the result to output directory...
    mkdir -p "$LIBSDIR"
    cp "$DISTDIR/local/include/ximc.h"          "$DISTDIR/ximc/"
    cp "$DISTDIR/local/lib/libximc.a"           "$LIBSDIR"
    cp "$DISTDIR/local/lib/libximc.so.7.0.0"    "$LIBSDIR"
    ln -sf "libximc.so.7.0.0"                   "$LIBSDIR/libximc.so"
    ln -sf "libximc.so.7.0.0"                   "$LIBSDIR/libximc.so.7"
    echo '----------------------------------------------------'
    echo '            BUILDING LIBXIMC IS COMPLETED           '
    echo '----------------------------------------------------'
    echo 'Output:                                             '
    echo "* $DISTDIR/ximc/ximc.h                              "
    echo "* $LIBSDIR/libximc.a                                "
    echo "* $LIBSDIR/libximc.so                               "
    echo "* $LIBSDIR/libximc.so.7                             "
    echo "* $LIBSDIR/libximc.so.7.0.0                         "
    echo '----------------------------------------------------'
fi
