set -e

mkdir -p ${BOSH_INSTALL_TARGET}/src
cp -a . ${BOSH_INSTALL_TARGET}/src
export GOPATH=$BOSH_INSTALL_TARGET

export GOROOT=$(readlink -nf /var/vcap/packages/golang)
export PATH=$GOROOT/bin:$PATH

CGO_ENABLED=0 go build -a -installsuffix static code.cloudfoundry.org/diego-ssh/cmd/sshd
GOOS=windows CGO_ENABLED=0 go build -a -installsuffix static code.cloudfoundry.org/diego-ssh/cmd/sshd
GOOS=windows CGO_ENABLED=0 go build -o diego-sshd-windows2012R2.exe -tags=windows2012R2 -a -installsuffix static code.cloudfoundry.org/diego-ssh/cmd/sshd

ldd sshd && echo 'diego-sshd must be statically linked' && false

cp sshd ${BOSH_INSTALL_TARGET}/diego-sshd
cp sshd.exe ${BOSH_INSTALL_TARGET}/diego-sshd.exe
cp diego-sshd-windows2012R2.exe ${BOSH_INSTALL_TARGET}

tar -xzvf winpty/winpty-*.tgz -C ${BOSH_INSTALL_TARGET}

# clean up source artifacts
rm -rf ${BOSH_INSTALL_TARGET}/src ${BOSH_INSTALL_TARGET}/pkg
