#!/bin/sh

export VLMCSD_VERSION=$(git describe)
export VERBOSE=3
export CAT=2

if [ `uname -s` != "SunOS" ]; then
  echo "This is no SunOS operating system."
  exit 3
fi

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


SOLARIS_VERSION=`uname -v`

MAKEFLAGS="-Bj"
REUSEOBJFLAGS="-j"

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

CF="-fno-common -fno-exceptions -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fmerge-all-constants -Wno-char-subscripts"
LF="-fwhole-program -Wl,--hash-style=sysv -Wl,--build-id=none"


# 32 bit
if [ "$CAT" != "" ]; then
  gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld allmulti CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x86 PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x86 MULTI_NAME=$BINDIR/vlmcsdmulti-Solaris$SOLARIS_VERSION-x86 CC=gcc CFLAGS="$CF" LDFLAGS="$LF"
else
  gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld MULTI_NAME=$BINDIR/vlmcsdmulti-Solaris$SOLARIS_VERSION-x86 CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x86 PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x86 CC=gcc CFLAGS="$CF" LDFLAGS="$LF" allmulti
fi

gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld vlmcsd PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x86-threads CC=gcc THREADS=1 CFLAGS="$CF" LDFLAGS="-lpthread $LF"
gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x86-openssl1.0-EXPERIMENTAL CRYPTO=openssl_with_aes PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x86-openssl1.0-EXPERIMENTAL CC=gcc CFLAGS="$CF" LDFLAGS="$LF"

# 64 bit

LF="$LF -Wl,-melf_x86_64_sol2"

if [ "$CAT" != "" ]; then
  gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld allmulti MULTI_NAME=$BINDIR/vlmcsdmulti-Solaris$SOLARIS_VERSION-x64 CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x64 PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x64 CC=gcc CFLAGS="$CF" LDFLAGS="$LF" PLATFORMFLAGS="-m64"
else
  gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld MULTI_NAME=vlmcsdmulti-Solaris$SOLARIS_VERSION-x64 CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x64 PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x64 CC=gcc CFLAGS="$CF" LDFLAGS="$LF" PLATFORMFLAGS="-m64" allmulti
fi

gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld vlmcsd PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x64-threads CC=gcc THREADS=1 CFLAGS="$CF" LDFLAGS="$LF -lpthread" PLATFORMFLAGS="-m64"
gmake $MAKEFLAGS LD_ALTEXEC=/usr/bin/gld CLIENT_NAME=$BINDIR/vlmcs-Solaris$SOLARIS_VERSION-x64-openssl1.0-EXPERIMENTAL CRYPTO=openssl_with_aes PROGRAM_NAME=$BINDIR/vlmcsd-Solaris$SOLARIS_VERSION-x64-openssl1.0-EXPERIMENTAL CC=gcc CFLAGS="$CF" LDFLAGS="$LF" PLATFORMFLAGS="-m64"

rm -f *.o *_all.*

cd $BINDIR

gstrip -s --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag vlmcs-* vlmcsd-*
#gstrip -s --strip-unneeded --remove-section=.eh_frame_hdr --remove-section=.eh_frame --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag vlmcs-* vlmcsd-*
#sstrip -z vlmcs-* vlmcsd-*

# Copy stuff to distribution server
scp -p vlmcsd-Sola* vlmcs-* vlmcsdmulti-* root@ubuntu64:x/binaries/Solaris/intel
