#!/bin/bash

export VLMCSD_VERSION=$(git describe)
SMALLCC="-pipe -fno-common -fno-exceptions -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fmerge-all-constants"
SMALLLD="-pipe -Wl,--hash-style=sysv -Wl,-z,norelro -Wl,--build-id=none"
SMALL="$SMALLCC $SMALLLD"

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

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

## IBM S/390

export CFLAGS="$SMALLCC"
export PLATFORMFLAGS="-flto=jobserver -fwhole-program -m31 -mesa -mpacked-stack -msmall-exec"
export LDFLAGS="$SMALLLD -Wl,--hash-style=gnu"
export THREADS=0
export FEATURES=full
export CC=s390x-linux-gnu-gcc
export VERBOSE=3

export MULTI_NAME=$BINDIR/vlmcsdmulti-s390-glibc
export CLIENT_NAME=$BINDIR/vlmcs-s390-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-s390-glibc

make -B -j`nproc` allmulti


export PLATFORMFLAGS="-flto=jobserver -fwhole-program -m64 -mzarch -mpacked-stack -msmall-exec"
export MULTI_NAME=$BINDIR/vlmcsdmulti-s390x-glibc
export CLIENT_NAME=$BINDIR/vlmcs-s390x-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-s390x-glibc

make -B -j`nproc` allmulti



## SPARC64

export PLATFORMFLAGS="-flto=jobserver -fwhole-program -mcpu=v7"
export LDFLAGS="$SMALLLD"
export CC=sparc64-linux-gnu-gcc

export MULTI_NAME=$BINDIR/vlmcsdmulti-sparc64v9-glibc
export CLIENT_NAME=$BINDIR/vlmcs-sparc64v9-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-sparc64v9-glibc

make -B -j`nproc` allmulti




## MIPS64 BIG-ENDIAN

export PLATFORMFLAGS="-flto=jobserver -fwhole-program -mips64 -mno-mips16"
export LDFLAGS="$SMALLLD"
export CC=mips64-linux-gnuabi64-gcc

export MULTI_NAME=$BINDIR/vlmcsdmulti-mips64-glibc
export CLIENT_NAME=$BINDIR/vlmcs-mips64-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-mips64-glibc

make -B -j`nproc` allmulti

export PLATFORMFLAGS="-flto=jobserver -fwhole-program -mips64 -mmicromips"
export MULTI_NAME=$BINDIR/vlmcsdmulti-mips64mm-glibc
export CLIENT_NAME=$BINDIR/vlmcs-mips64mm-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-mips64mm-glibc

make -B -j`nproc` allmulti



## MIPS64 LITTLE-ENDIAN

export PLATFORMFLAGS="-flto=jobserver -fwhole-program -mips64 -mno-mips16"
export LDFLAGS="$SMALLLD"
export CC=mips64el-linux-gnuabi64-gcc

export MULTI_NAME=$BINDIR/vlmcsdmulti-mips64el-glibc
export CLIENT_NAME=$BINDIR/vlmcs-mips64el-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-mips64el-glibc

make -B -j`nproc` allmulti

export PLATFORMFLAGS="-flto=jobserver -fwhole-program -mips64 -mmicromips"
export MULTI_NAME=$BINDIR/vlmcsdmulti-mips64elmm-glibc
export CLIENT_NAME=$BINDIR/vlmcs-mips64elmm-glibc
export PROGRAM_NAME=$BINDIR/vlmcsd-mips64elmm-glibc

make -B -j`nproc` allmulti

cd $BINDIR
sstrip -z *



if [ "$1" == "nocopy" ]; then
  exit 0
fi

cd $MANDIR

mkdir -p /usr/local/man/man1 2>/dev/null
mkdir -p /usr/local/man/man5 2>/dev/null
mkdir -p /usr/local/man/man8 2>/dev/null
mkdir -p /usr/local/man/man7 2>/dev/null
cp -a vlmcs.1 vlmcsdmulti.1 /usr/local/man/man1/
cp -a vlmcsd.7 /usr/local/man/man7/
cp -a vlmcsd.8 /usr/local/man/man8/
cp -a vlmcsd.ini.5 /usr/local/man/man5/
pbzip2 -f -9 /usr/local/man/man5/vlmcsd.ini.5 /usr/local/man/man1/vlmcs.1 /usr/local/man/man1/vlmcsdmulti.1 /usr/local/man/man7/vlmcsd.7 /usr/local/man/man8/vlmcsd.8

cd $BINDIR

scp -p vlmcsdmulti-s390-glibc vlmcs-s390-glibc vlmcsd-s390-glibc vlmcsdmulti-s390x-glibc vlmcs-s390x-glibc vlmcsd-s390x-glibc ubuntu64.internal:x/binaries/Linux/s390/glibc
scp -p vlmcsdmulti-sparc64v9-glibc vlmcs-sparc64v9-glibc vlmcsd-sparc64v9-glibc ubuntu64.internal:x/binaries/Linux/sparc/glibc
scp -p vlmcsdmulti-mips64-glibc vlmcs-mips64-glibc vlmcsd-mips64-glibc vlmcsdmulti-mips64mm-glibc vlmcs-mips64mm-glibc vlmcsd-mips64mm-glibc ubuntu64.internal:x/binaries/Linux/mips/big-endian/glibc
scp -p vlmcsdmulti-mips64el-glibc vlmcs-mips64el-glibc vlmcsd-mips64el-glibc vlmcsdmulti-mips64elmm-glibc vlmcs-mips64elmm-glibc vlmcsd-mips64elmm-glibc ubuntu64.internal:x/binaries/Linux/mips/little-endian/glibc
scp -p -P 2222 vlmcsdmulti-s390-glibc vlmcs-s390-glibc vlmcsd-s390-glibc vlmcsdmulti-s390x-glibc vlmcs-s390x-glibc vlmcsd-s390x-glibc s390:vlmcsd
scp -p -P 2222 vlmcsdmulti-s390-glibc vlmcsdmulti-s390x-glibc s390:/usr/local/sbin
