#!/bin/bash
# run this from the MEGAcmd folder
# but first set up the environment per the PDF and tar files at https://sourceforge.net/projects/qosgpl/files/QNAP%20NAS%20Tool%20Chains/
# and put the MEGAcmd tree at /opt/cross-project/qnap/
# this first sample build uses the arm cross compile toolchain, testing on the TS-228

#prerequisites:
#  - qnap installed cross compiler from http://download.qnap.com/dev/Toolchain/TS-x31%2B_cross-project-arm_al.20150909.tar.gz
#     (unzipped to /opt/)
#  - 32 bit libz, eg this works for ubuntu 16.04: sudo apt-get install zlib1g:i386

export BUILDROOT=$(pwd)/../../../..

export PROJECT=arm-linux-gnueabihf
export PRJROOT=${BUILDROOT}/opt/cross-project/arm/linaro
export ARCH=arm
export TARGET=arm-linux-gnueabihf
export CROSS_COMPILE=${TARGET}-
export TARGET_PREFIX=${PRJROOT}/${PROJECT}/libc
export TARGET_PREFIX=${PRJROOT}/${PROJECT}/libc
export SYS_TARGET_PREFIX=${PRJROOT}/${PROJECT}/libc
export PATH=${PRJROOT}/bin:${PATH}:/usr/sbin:/sbin


export MEGA_CMD_DIR=${BUILDROOT}/opt/cross-project/qnap/MEGAcmd

#export PATH=${BUILDROOT}/opt/cross-project/arm/linaro/arm-linux-gnueabihf/libc/usr/lib/:/opt/cross-project/arm/linaro/bin:$PATH
#export LD_LIBRARY_PATH=${BUILDROOT}/opt/cross-project/arm/linaro/arm-linux-gnueabihf/libc/usr/lib:$LD_LIBRARY_PATH

export MACHINE=armada38x
export CUSTOM_CONFIG_ARGS="--host=${TARGET} --disable-dependency-tracking"

export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}g++
export LD=${CROSS_COMPILE}ld
export CFLAGS="$CFLAGS -fPIC -fexceptions -fvisibility=hidden -fsigned-char"
export CPPFLAGS="-DNDEBUG"
export CXXFLAGS="$CXXFLAGS $CFLAGS -DNDEBUG -DENABLE_BACKUPS -DMEGACMD_USERAGENT_SUFFIX=QNAP -DHAVE_LIBUV -std=c++11"
export AR=${CROSS_COMPILE}ar
export NM=${CROSS_COMPILE}nm
export STRIP=${CROSS_COMPILE}strip
export RANLIB=${CROSS_COMPILE}ranlib
export OBJDUMP=${CROSS_COMPILE}objdump
export LDFLAGS=
export ConfigOpt=
#export ARCH=$ARCH
export ToolChainSysRoot=$SYS_TARGET_PREFIX
export SysRootPrefix=$SYS_TARGET_PREFIX
export SysRootInclude=$SYS_TARGET_PREFIX/include
export SysRootLib=$SYS_TARGET_PREFIX/lib

export CROSS_COMPILE=

export AUTOMAKE_OPTIONS="no-dependencies"

${CXX} --version

echo ---------- env set up --------------------
env | sort
echo ---------- env set up --------------------

#fix any utf-8 w/ BOM files that the compiler can't handle
cd $MEGA_CMD_DIR
find src -name "*.h" | xargs dos2unix
find src -name "*.c" | xargs dos2unix
find src -name "*.cpp" | xargs dos2unix
find sdk/include -name "*.h" | xargs dos2unix
find sdk/src -name "*.c" | xargs dos2unix
find sdk/src -name "*.cpp" | xargs dos2unix


cd $MEGA_CMD_DIR/sdk
pwd

mkdir -p norecurse
mv norecurse/* .

./contrib/build_sdk.sh -a -e -g -I -n -q -R -v -X -C"$CUSTOM_CONFIG_ARGS" -O linux-generic32 -0 2>&1 | tee $MEGA_CMD_DIR/mybuildlogs-sdkbuild.out

if ! [ -e src/libmega.a ] ; then
    echo "libtool could not link it so we do it ourselves. Including Zen and MediaInfo somehow reference a nonexistent path /home/slava in the Synology libtool libraries. If we left those out it would make the lib ok."
    echo "we make the lib using $AR directly, with and output the same name src/libmega.la that satisfies the configure script for MEGAcmd."
    rm -f src/libmega.la
    $AR -rcs ./src/libmega.a `find src -name "*.o"` `find sdk_build -name "*.a"`
    cp ./src/libmega.a ./src/libmega.la
fi

if ! [ -e ./sdk_build/install/lib/libmega.a ] ; then
    cp ./src/libmega* ./sdk_build/install/lib/
fi

if ! [ -e ./sdk_build/install/lib/libmega.a ] ; then
   echo "SDK build failed"
   exit 1
fi

mv ./Makefile* norecurse/
mv ./config* norecurse/
cd ..

echo -------------------------------------------------------- sdk end --------------------------------------


echo about to build megacmd ---------------------------

sh autogen.sh 2>&1 | tee $MEGA_CMD_DIR/mybuildlogs-autogen2.out
./configure --without-libraw SUBDIRS="" --enable-static --with-cryptopp --with-readline=${BUILDROOT}/opt/cross-project/arm/marvell/arm-none-linux-gnueabi/libc/marvell-f/src/MEGAcmd/sdk/sdk_build/install/include/ \
            $CUSTOM_CONFIG_ARGS  2>&1 | tee $MEGA_CMD_DIR/mybuildlogs-configure2.out 


export INCLUDES="-I$MEGA_CMD_DIR/sdk/sdk_build/install/include -I$MEGA_CMD_DIR/sdk/include/ -I$MEGA_CMD_DIR/include/mega/posix"
#somehow autotools is determined to have WIN32 defined
#make SUBDIRS='' 2>&1 | tee $MEGA_CMD_DIR/mybuildlogs-make2.out

rm -f mega-cmd-server
rm -f mega-cmd
rm -f mega-cmd-exec

$CXX $CXXFLAGS src/megacmd.cpp src/comunicationsmanager.cpp src/megacmdutils.cpp src/megacmdcommonutils.cpp src/configurationmanager.cpp src/megacmdlogger.cpp src/megacmdsandbox.cpp src/listeners.cpp src/megacmdexecuter.cpp src/comunicationsmanagerportsockets.cpp src/comunicationsmanagerfilesockets.cpp -Isdk/include -Isdk/include/mega/posix -Isdk/sdk_build/install/include  sdk/sdk_build/install/lib/libmega.a sdk/sdk_build/install/lib/libcryptopp.a    sdk/sdk_build/install/lib/libcurl.a sdk/sdk_build/install/lib/libcares.a sdk/sdk_build/install/lib/libssl.a sdk/sdk_build/install/lib/libcrypto.a  sdk/sdk_build/install/lib/libsqlite3.a sdk/sdk_build/install/lib/libfreeimage.a sdk/sdk_build/install/lib/libmediainfo.a sdk/sdk_build/install/lib/libzen.a sdk/sdk_build/build/libuv-v1.8.0/.libs/libuv.a -lpthread -ldl -lrt sdk/sdk_build/install/lib/libz.a -o mega-cmd-server

$CXX $CXXFLAGS src/megacmdcommonutils.cpp src/megacmdshell/*.cpp -Isdk/include -Isdk/include/mega/posix -Isdk/sdk_build/install/include  sdk/sdk_build/install/lib/libmega.a sdk/sdk_build/install/lib/libcryptopp.a    sdk/sdk_build/install/lib/libcurl.a sdk/sdk_build/install/lib/libcares.a sdk/sdk_build/install/lib/libssl.a sdk/sdk_build/install/lib/libcrypto.a  sdk/sdk_build/install/lib/libsqlite3.a -lpthread -ldl -lrt sdk/sdk_build/install/lib/libz.a ./sdk/sdk_build/install/lib/libreadline.a ./sdk/sdk_build/install/lib/libtermcap.a -o mega-cmd

$CXX $CXXFLAGS src/megacmdcommonutils.cpp src/client/*.cpp src/megacmdshell/megacmdshellcommunications.cpp -Isdk/include -Isdk/include/mega/posix -Isdk/sdk_build/install/include  sdk/sdk_build/install/lib/libmega.a sdk/sdk_build/install/lib/libcryptopp.a    sdk/sdk_build/install/lib/libcurl.a sdk/sdk_build/install/lib/libcares.a sdk/sdk_build/install/lib/libssl.a sdk/sdk_build/install/lib/libcrypto.a  sdk/sdk_build/install/lib/libsqlite3.a -lpthread -ldl -lrt sdk/sdk_build/install/lib/libz.a -o mega-exec

echo "outputs should be 3 files: mega-cmd mega-cmd-server mega-exec.  ls output: "
ls mega-cmd mega-cmd-server mega-exec

if [ -e mega-cmd ] && [ -e mega-cmd-server ] && [ -e mega-exec ] ; then

    mkdir -p install
    cp src/client/mega-* install/
    cp mega-cmd mega-exec mega-cmd-server install/

    mkdir -p build/QNAP_NAS/megacmdpkg/arm-x41
    cp install/* build/QNAP_NAS/megacmdpkg/arm-x41

    MAJOR=`sed -n -e 's/.*MAJOR_VERSION \([0-9]*\).*/\1/p' < src/megacmdversion.h`
    MINOR=`sed -n -e 's/.*MINOR_VERSION \([0-9]*\).*/\1/p' < src/megacmdversion.h`
    MICRO=`sed -n -e 's/.*MICRO_VERSION \([0-9]*\).*/\1/p' < src/megacmdversion.h`
    MEGACMD_VERSION="\"$MAJOR.$MINOR.$MICRO\""
    sed -i build/QNAP_NAS/megacmdpkg/qpkg.cfg -e "s/^QPKG_VER=.*/QPKG_VER=$MEGACMD_VERSION/"
else
    echo "Build failed"
    exit 1
fi

