#! /bin/bash

# Copyright (C) 2007-2009 Apple Inc. All rights reserved.
# Push the current tree to the named server and build and install it.

SERVER="$1"
USERNAME=${USERNAME:-local}
PASSWORD=${PASSWORD:-local}

TREE=$(pwd)
BUILDDIR=$(basename $TREE)

SSH_CONFIG=~/tmp/.$(basename $0).ssh
trap "rm -f $SSH_CONFIG 2>/dev/null" 0 1 2 3 15

cat >$SSH_CONFIG <<EOF
Host *
    CheckHostIP no
    VerifyHostKeyDNS no
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
EOF

BUILDIT=${BUILDIT:-"/usr/local/bin/buildit"}

BUILD="sudo -S env BUILDIT_DIR=/var/tmp $BUILDIT $BUILDDIR $ARCHS -othercflags=-g -merge / "

if [ -z "$QUICK" ] ; then
    ARCHS=${ARCHS:-"-arch i386 -arch x86_64"}
else
    ARCHS=${ARCHS:-"-arch $(/usr/bin/arch)"}
    BUILDXTRA="-noverify -nosum"
fi 

BUILD="sudo -S env BUILDIT_DIR=/var/tmp $BUILDIT $BUILDDIR $BUILDXTRA $ARCHS -othercflags=-g -merge / "

die()
{
    echo $* 1>&2
    exit 1
}

rm -rf $TREE/samba/source/test.dir
rm -rf $TREE/samba/source/a.out.dSYM

RSYNC_PASSWORD=$PASSWORD rsync \
    --rsh="ssh -F $SSH_CONFIG" \
    --archive --cvs-exclude --delete-excluded --exclude=.git --compress \
    $TREE $USERNAME@$SERVER:~ || die rsync failed

echo $BUILD
ssh -F $SSH_CONFIG \
    $USERNAME@$SERVER $BUILD || die build failed

