#!/bin/bash

export VLMCSD_VERSION=$(git describe)
export VERBOSE=3
export DNS_PARSER=OS

cd "$( dirname "$0" )"
make -C .. clean

MAKEFLAGS="-Bj"
REUSEOBJFLAGS="-j"
CFGCC="-static-libgcc -mdynamic-no-pic -Os -flto=jobserver -fwhole-program -fno-common -fno-exceptions -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fmerge-all-constants"
CFCLANG="-mdynamic-no-pic -Os -flto -fno-common -fno-exceptions -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fmerge-all-constants"
CFGCC42="-static-libgcc -mdynamic-no-pic -Os -fno-common -fno-exceptions -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fmerge-all-constants"

cd ../src
BINDIR="../bin"

make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-MacOSX-x86 CLIENT_NAME=$BINDIR/vlmcs-MacOSX-x86 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x86 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m32 -march=core2 -mmacosx-version-min=10.0" && \
make $MAKEFLAGS vlmcsd THREADS=1 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x86-threads CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m32 -march=core2 -mmacosx-version-min=10.0" && \
make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-MacOSX-x64 CLIENT_NAME=$BINDIR/vlmcs-MacOSX-x64 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x64 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m64 -march=core2 -mmacosx-version-min=10.0" && \
make $MAKEFLAGS vlmcsd THREADS=1 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x64-threads CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m64 -march=core2 -mmacosx-version-min=10.0" && \
#make $MAKEFLAGS CLIENT_NAME=vlmcs-MacOSX-x86-openssl-EXPERIMENTAL PROGRAM_NAME=vlmcsd-MacOSX-x86-openssl-EXPERIMENTAL CRYPTO=openssl_with_aes_soft CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m32 -march=core2 -mmacosx-version-min=10.4" && \
#make $MAKEFLAGS CLIENT_NAME=vlmcs-MacOSX-x64-openssl-EXPERIMENTAL PROGRAM_NAME=vlmcsd-MacOSX-x64-openssl-EXPERIMENTAL CRYPTO=openssl_with_aes_soft CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m64 -march=core2 -mmacosx-version-min=10.4" && \

#make $MAKEFLAGS CLIENT_NAME=vlmcs-iOS-7.1-armv7 PROGRAM_NAME=vlmcsd-iOS-7.1-armv7 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-mthumb -m32 -arch armv7 -miphoneos-version-min=1.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk" && \
#rm -f vlmcs.o vlmcsd.o vlmcsdmulti.o *_all.* && \
#make $REUSEOBJFLAGS vlmcsdmulti-iOS-7.1-armv7 MULTI_NAME=vlmcsdmulti-iOS-7.1-armv7 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-mthumb -m32 -arch armv7 -miphoneos-version-min=1.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk" && \

make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-iOS-armv7 CLIENT_NAME=$BINDIR/vlmcs-iOS-armv7 PROGRAM_NAME=$BINDIR/vlmcsd-iOS-armv7 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-mthumb -m32 -arch armv7 -miphoneos-version-min=1.0 -isysroot ~/toolchains/iPhoneOS.sdk" && \
make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-iOS-armv8-aarch64 CLIENT_NAME=$BINDIR/vlmcs-iOS-armv8-aarch64 PROGRAM_NAME=$BINDIR/vlmcsd-iOS-armv8-aarch64 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-m64 -arch arm64 -miphoneos-version-min=7.0 -isysroot ~/toolchains/iPhoneOS.sdk" && \

make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-iOS-6.1-armv7 CLIENT_NAME=$BINDIR/vlmcs-iOS-6.1-armv7 PROGRAM_NAME=$BINDIR/vlmcsd-iOS-6.1-armv7 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-mthumb -m32 -arch armv7 -miphoneos-version-min=1.0 --sysroot ~/toolchains/iPhoneOS6.1.sdk -isysroot ~/toolchains/iPhoneOS6.1.sdk" && \

make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-iOS-5.1-armv7-clang3.4 CLIENT_NAME=$BINDIR/vlmcs-iOS-5.1-armv7-clang3.4 PROGRAM_NAME=$BINDIR/vlmcsd-iOS-5.1-armv7-clang3.4 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-mthumb -m32 -arch armv7 -miphoneos-version-min=1.0 --sysroot ~/toolchains/iPhoneOS5.1.sdk -isysroot ~/toolchains/iPhoneOS5.1.sdk" && \

#PATH=~/toolchains/iOS5.1-MacOS-Lion/usr/bin:$PATH clang --version
PATH=~/toolchains/iOS5.1-MacOS-Lion/usr/bin:$PATH make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-iOS-5.1-armv6-clang3.1 CLIENT_NAME=$BINDIR/vlmcs-iOS-5.1-armv6-clang3.1 PROGRAM_NAME=$BINDIR/vlmcsd-iOS-5.1-armv6-clang3.1 CC=clang CFLAGS="$CFCLANG" PLATFORMFLAGS="-arch armv6 -miphoneos-version-min=1.0 --sysroot ~/toolchains/iPhoneOS5.1.sdk -isysroot ~/toolchains/iPhoneOS5.1.sdk" && \

#PATH=~/toolchains/gcc4.2/usr/bin/bin:$PATH make $MAKEFLAGS CLIENT_NAME=vlmcs-iOS-4.1-armv6-llvm-gcc4.2 PROGRAM_NAME=vlmcsd-iOS-4.1-armv6-llvm-gcc4.2 CC=llvm-g++-4.2 CFLAGS="$CFGCC42" PLATFORMFLAGS="-arch armv6 -miphoneos-version-min=1.0 --sysroot ~/toolchains/iPhoneOS4.1.sdk -isysroot ~/toolchains/iPhoneOS4.1.sdk" && \
#rm -f vlmcs.o vlmcsd.o vlmcsdmulti.o *_all.* && \
#PATH=~/toolchains/gcc4.2/usr/bin/bin:$PATH make $REUSEOBJFLAGS vlmcsdmulti-iOS-4.1-armv6-llvm-gcc4.2 MULTI_NAME=vlmcsdmulti-iOS-4.1-armv6-llvm-gcc4.2 CC=llvm-g++-4.2 CFLAGS="$CFGCC42" PLATFORMFLAGS="-arch armv6 -miphoneos-version-min=1.0 --sysroot ~/toolchains/iPhoneOS4.1.sdk -isysroot ~/toolchains/iPhoneOS4.1.sdk" && \

#PATH=~/toolchains/gcc4.2/usr/bin:$PATH make $MAKEFLAGS CLIENT_NAME=vlmcs-iOS-4.1-armv7-clang PROGRAM_NAME=vlmcsd-iOS-4.1-armv7-llvm-clang CC=~/toolchains/gcc4.2/usr/bin/bin/clang CFLAGS="$CFGCC42" PLATFORMFLAGS="-mthumb -arch armv7 -miphoneos-version-min=4.1 --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk" && \
#rm -f vlmcs.o vlmcsd.o vlmcsdmulti.o && \
#PATH=~/toolchains/gcc4.2/usr/bin:$PATH make $REUSEOBJFLAGS vlmcsdmulti-iOS-4.1-armv7-llvm-clang MULTI_NAME=vlmcsdmulti-iOS-4.1-armv7-llvm-clang CC=~/toolchains/gcc4.2/usr/bin/bin/clang CFLAGS="$CFGCC42" PLATFORMFLAGS="-mthumb -arch armv7 -miphoneos-version-min=4.1 --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.1.sdk" && \


PATH=~/toolchains/gcc4.2/usr/bin:$PATH make -Bj allmulti SAFE_MODE=1 MULTI_NAME=$BINDIR/vlmcsdmulti-MacOSX-ppc PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-ppc CLIENT_NAME=$BINDIR/vlmcs-MacOSX-ppc CC=gcc CFLAGS="$CFGCC42 -isysroot ~/toolchains/MacOSX10.5.sdk -arch ppc -mmacosx-version-min=10.0" && \


make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-MacOSX-x86-gcc CLIENT_NAME=$BINDIR/vlmcs-MacOSX-x86-gcc PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x86-gcc CC=gcc-6 CFLAGS="$CFGCC" PLATFORMFLAGS="-m32 -march=core2 -mmacosx-version-min=10.11" && \
make $MAKEFLAGS vlmcsd THREADS=1 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x86-threads-gcc CC=gcc-6 CFLAGS="$CFGCC" PLATFORMFLAGS="-m32 -march=core2 -mmacosx-version-min=10.11" && \
make $MAKEFLAGS allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-MacOSX-x64-gcc CLIENT_NAME=$BINDIR/vlmcs-MacOSX-x64-gcc PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x64-gcc CC=gcc-6 CFLAGS="$CFGCC" PLATFORMFLAGS="-m64 -march=core2 -mmacosx-version-min=10.11" && \
make $MAKEFLAGS vlmcsd THREADS=1 PROGRAM_NAME=$BINDIR/vlmcsd-MacOSX-x64-threads-gcc CC=gcc-6 CFLAGS="$CFGCC" PLATFORMFLAGS="-m64 -march=core2 -mmacosx-version-min=10.11" && \

# Sign the iOS binaries
#ldid -S *iOS*

#strip vlmcs-* vlmcsd-* vlmcsdmulti-*

cd $BINDIR
MANDIR="../man"

rm -fr *.dSYM

sudo cp -p ../etc/vlmcsd.kmd /usr/local/bin
sudo cp -p vlmcs-MacOSX-x86-gcc /usr/local/bin/vlmcs
sudo cp -p vlmcsd-MacOSX-x86-gcc /usr/local/bin/vlmcsd

sudo mkdir -p /usr/local/share/man/man8
sudo mkdir -p /usr/local/share/man/man1
sudo mkdir -p /usr/local/share/man/man7
sudo mkdir -p /usr/local/share/man/man5

sudo cp -p $MANDIR/vlmcsd.8 /usr/local/share/man/man8
sudo cp -p $MANDIR/vlmcs.1 $MANDIR/vlmcsdmulti.1 /usr/local/share/man/man1
sudo cp -p $MANDIR/vlmcsd-floppy.7 $MANDIR/vlmcsd.7 /usr/local/share/man/man7
sudo cp -p $MANDIR/vlmcsd.ini.5 //usr/local/share/man/man5

# Copy the stuff to distribution server
scp -p vlmcsd-MacOSX-x* vlmcs-MacOSX-x* vlmcsdmulti-MacOSX-x* root@ubuntu64:x/binaries/MacOSX/intel
scp -p vlmcsd-MacOSX-ppc* vlmcs-MacOSX-ppc* vlmcsdmulti-MacOSX-ppc* root@ubuntu64:x/binaries/MacOSX/ppc
scp -p vlmcsd-iOS* vlmcs-iOS* vlmcsdmulti-iOS* root@ubuntu64:x/binaries/iOS/arm
