#!/bin/bash

#  publishRelease.sh
#  Capo
#
#  Created by Christopher Liscio on 11-07-12.
#  Copyright 2011 SuperMegaUltraGroovy. All rights reserved.

: ${BuildOutputDir?"BuildOutputDir not specified"}
: ${DownloadURLBase?"DownloadURLBase not specified"}
: ${CryptoKeyPath?"CryptoKeyPath not specified"}

VERSION=$(defaults read "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${INFOPLIST_PATH}" CFBundleVersion)
SHORTVERSION=$(defaults read "${ARCHIVE_PRODUCTS_PATH}${INSTALL_PATH}/${INFOPLIST_PATH}" CFBundleShortVersionString)

ARCHIVE_FILENAME="${TARGET_NAME}-${CURRENT_MARKETING_VERSION}.zip"
DOWNLOAD_URL="${DownloadURLBase}/${ARCHIVE_FILENAME}"
KEY_FILE="${CryptoKeyPath}/dsa_priv.pem"
WD=$PWD

SIZE=$(stat -f %z "${BuildOutputDir}/${ARCHIVE_FILENAME}")
PUBDATE=$(date +"%a, %d %b %G %T %z")
SIGNATURE=$(${BuildScriptPath}/sparkle/sign_update.rb "${BuildOutputDir}/${ARCHIVE_FILENAME}" ${KEY_FILE})

[ $SIGNATURE ] || { echo Unable to sign the update; false; }

mkdir -p appcast

cat > ${AppcastPath}/${Product}-${CURRENT_MARKETING_VERSION}.txt <<EOF
DOWNLOAD_URL="${DOWNLOAD_URL}"
VERSION="${VERSION}"
SHORT_VERSION="${SHORTVERSION}"
FILE_LENGTH="${SIZE}"
DSA_SIGNATURE="${SIGNATURE}"
BUILD_DATE="${PUBDATE}"
EOF