#!/bin/bash
# updates 0install stream xml for new release 

# expected variables:
# SERIES
# VERSION
# FILE
# PACKAGE
# OSTAG_GENERIC
# SF_DIRECTORY
# RC (optional, gets added to download path)

set -x

# transform version for 0install
ZEROVERSION=$(echo ${VERSION} | sed -e "s,_,-,g" -e "s,-alpha,-pre0.," -e "s,-beta,-pre1.," -e "s,\.-z,.," -e "s,\.-r,.," -e "s,-sty.ct.ap,,g" -e "s,-sty.ct,,g" -e "s,-school,,g" )
# and launchpad
LPVERSION=$(echo ${VERSION} | sed -e "s,_,-,g" )

# library and data files
LIBFILE=$(echo ${FILE} | sed -e "s,${PACKAGE},${PACKAGE}-libs,")
DATAFILE=$(echo ${FILE} | sed -e "s,${PACKAGE},${PACKAGE}-data," -e "s,${OSTAG_GENERIC}.,,")
WINFILE=$(echo ${FILE} | sed -e "s,\.${OSTAG_GENERIC},.win32," -e "s,\.bin\.tar\.bz2,.zip,")
WINLIBFILE=$(echo ${WINFILE} | sed -e "s,${PACKAGE},${PACKAGE}-libs,")

# construct uri
# lp
# URIBASE=http://launchpad.net/armagetronad/${SERIES}/${LPVERSION}/+download/
# sf
URIBASE=http://sourceforge.net/projects/armagetronad/files/${SF_DIRECTORY}/${VERSION}/ZeroInstall/
test -z "${RC}" || URIBASE=http://sourceforge.net/projects/armagetronad/files/${SF_DIRECTORY}/${VERSION}/ZeroInstall/${RC}/

URI=${URIBASE}$(basename ${FILE})
LIBURI=${URIBASE}$(basename ${LIBFILE})
DATAURI=${URIBASE}$(basename ${DATAFILE})
WINURI=${URIBASE}$(basename ${WINFILE})
WINLIBURI=${URIBASE}$(basename ${WINLIBFILE})

# determine xml to modify
XML=0install/${PACKAGE}

# exception: 0.2.8 builds go to the stable or beta feed first
if test ${SERIES} == 0.2.8; then
    if echo ${PACKAGE} | grep -v beta; then
        if echo ${PACKAGE} | grep _rc; then
            XML=$(echo ${XML} | sed -e 's,armagetronad,armagetronad-beta,')
        else
    	    echo ${PACKAGE} | grep stable || XML=$(echo ${XML} | sed -e 's,armagetronad,armagetronad-stable,')
	fi
    fi
fi

OS=Linux
CPU=$(echo ${OSTAG_GENERIC} | sed -e "s,-.*,,")
ARCH=${OS}-${CPU}

# add the release
if test -r ${FILE} && ! grep -q version=\"${ZEROVERSION}\" ${XML}-${ARCH}.xml; then
 0launch -o -c 'http://0install.net/2006/interfaces/0publish' \
  ${XML}-${ARCH}.xml \
 --add-version ${ZEROVERSION} \
 --archive-url=${URI} \
 --archive-file=${FILE} \
 --set-main=run\
 --set-released=today
fi

if test -r ${LIBFILE} && ! grep -q version=\"${ZEROVERSION}\" 0install/armagetronad-libs-${ARCH}.xml; then
  0launch -o -c 'http://0install.net/2006/interfaces/0publish' \
  0install/armagetronad-libs-${ARCH}.xml \
 --add-version ${ZEROVERSION} \
 --archive-url=${LIBURI} \
 --archive-file=${LIBFILE} \
 --set-released=today
fi

if test -r ${DATAFILE} && ! grep -q version=\"${ZEROVERSION}\" 0install/armagetronad-data.xml; then
  0launch -o -c 'http://0install.net/2006/interfaces/0publish' \
  0install/armagetronad-data.xml \
 --add-version ${ZEROVERSION} \
 --archive-url=${DATAURI} \
 --archive-file=${DATAFILE} \
 --set-released=today
fi

if test -r ${WINFILE} && ! grep -q version=\"${ZEROVERSION}\" ${XML}-Windows.xml; then
  0launch -o -c 'http://0install.net/2006/interfaces/0publish' \
  ${XML}-Windows.xml \
 --add-version ${ZEROVERSION} \
 --archive-url=${WINURI} \
 --archive-file=${WINFILE} \
 --archive-extract="" \
 --set-released=today
fi

if test -r ${WINLIBFILE} && ! grep -q version=\"${ZEROVERSION}\" 0install/armagetronad-libs-Windows.xml; then
  0launch -o -c 'http://0install.net/2006/interfaces/0publish' \
  0install/armagetronad-libs-Windows.xml \
 --add-version ${ZEROVERSION} \
 --archive-url=${WINLIBURI} \
 --archive-file=${WINLIBFILE} \
 --archive-extract="" \
 --set-released=today
fi

