#!/bin/bash

if [ "x$AWS_ID" == "x" ] || [ "y$AWS_KEY" == "y" ]; then
	echo "environment vars AWS_ID and AWS_KEY must be set to deploy"
    exit 1
fi

cd /home/piper/piper-hal

VERSION="$(dpkg-parsechangelog -Sversion)"

# Build package
dpkg-buildpackage -A

# Upload to S3
aws configure set aws_access_key_id $AWS_ID
aws configure set aws_secret_access_key $AWS_KEY
aws configure set region us-west-1

aws s3 cp "/home/piper/piper-hal_${VERSION}_all.deb" \
    s3://piper-deb-packages/piper-hal/

RESULT=$?
if [ $RESULT -ne 0 ]; then
	echo "error connecting to s3 server, return=$RESULT"
	exit $RESULT
fi

exit $?

