#!/bin/bash
#
# Add release assets (any files) to an existing tagged release in a github repo.
#
# Usage:
#     add-release-assets TAG asset-files
# where
#     TAG             the github release tag (find your git repo tags with command: hub release)
#     asset-files     the assets to add
#
# Example:
#     cd /mygitrepo
#     add-release-assets v1.0 /mygitrepoassets/*
# would add all files under /myrepoassets to github for the corresponding repo at /mygitrepo.
#
# Note1: requires packages 'hub' (and 'git') to be installed.
# Note2: working folder must be the github repo folder, not the assets folder.
# Note3: adding an already existing asset (file with the same name) simply overwrites the old one.
#
# The assets can be downloaded from:
#     https://github.com/USER/REPONAME/releases/download/TAG

echo2() { echo "$@" >&2 ; }
DIE() {
    echo2 "ERROR: ""$1"
    echo2 "Usage: $0 TAG asset(s)"
    exit 1
}

Main() {
    local quietly=no
    if [ "$1" = "--quietly" ] ; then
        quietly=yes
        shift
    fi
    local TAG="$1"
    test -n "$TAG" || DIE "TAG is missing."
    test -n "$2" || DIE "assets are missing."

    shift 1               # rest of the parameters are the assets

    local xx
    for xx in hub git ; do
        test -x /usr/bin/$xx || DIE "required package '$xx' is not installed."
    done
    local asset
    local assets=()
    local coloned=()
    local existing_tags
    local remo

    # Note: we do this in the git folder, not in the asset folder

    existing_tags="$(hub release)"
    test -n "$existing_tags" || DIE "no existing release tags were found!"

    for xx in $existing_tags ; do
        test "$TAG" = "$xx" && break
    done
    test "$TAG" = "$xx" || DIE "TAG '$TAG' is not in existing tags [$existing_tags]"

    if true ; then
        for asset in "$@" ; do
            test -r "$asset" || DIE "asset '$asset' does not exist."
            remo=${asset/:/COLON}         # convert special chars for github !!
            remo=${remo/+/PLUS}           # convert special chars for github !!
            if [ "$remo" != "$asset" ] ; then
                cp "$asset" "$remo"
                coloned+=("$remo")
                assets+=("-a" "$remo")
            else
                assets+=("-a" "$asset")
            fi
            [ $quietly = no ] && echo2 "adding asset $(basename "$asset")"
        done
        if [ $quietly = no ] ; then
            hub release edit "${assets[@]}" -m "" "$TAG"
        else
            hub release edit "${assets[@]}" -m "" "$TAG" 2> /dev/null
        fi
        rm -f "${coloned[@]}"
    # else
    #     for asset in "$@" ; do
    #         test -r "$asset" || DIE "asset '$asset' does not exist."
    #         if [ -z "$(release-asset-names "$TAG" "$asset")" ] ; then
    #             [ $quietly = no ] && echo2 "adding asset $(basename "$asset") to '$TAG'"
    #             hub release edit -a "$asset" -m "" "$TAG"
    #             sleep 0.5
    #         else
    #             DIE "asset '$asset' already exists at github!?"
    #         fi
    #     done
    fi
}

Main "$@"
