#!/bin/bash

show_help() {
    echo "Usage: $(basename $0) [--internal]"
    echo "options:"
    echo "-h, --help       display this help and exit"
    echo "-i, --internal   create project as internal"
    exit 0
}

while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h|--help) show_help;;
        -i|--internal) internal="--internal" ;;
        *) echo "Unknown parameter passed: $1";show_help ;;
    esac
    shift
done

PROJECT="$(grep PROJECT .obs/common.py | sed 's/PROJECT = "\(.*\)"/\1/')"
EXTRA_OSC_ARGS=""
if [ -n "$internal" ]; then
    PROJECT="ISV${PROJECT:3}"
    EXTRA_OSC_ARGS="-A https://api.suse.de"

    python3 .obs/render_meta.py ${internal} Snapshot | osc ${EXTRA_OSC_ARGS} meta prj "${PROJECT}:Snapshot" -F -
    osc ${EXTRA_OSC_ARGS} meta prjconf "${PROJECT}:Snapshot" -F _config
fi

python3 .obs/render_meta.py ${internal} ToTest | osc ${EXTRA_OSC_ARGS} meta prj "${PROJECT}:ToTest" -F -
python3 .obs/render_meta.py ${internal} | osc ${EXTRA_OSC_ARGS} meta prj "${PROJECT}" -F -
osc ${EXTRA_OSC_ARGS} meta prjconf "${PROJECT}:ToTest" -F _config
osc ${EXTRA_OSC_ARGS} meta prjconf "${PROJECT}" -F _config

if [ -z "$internal" ]; then
    python3 .obs/sync_packages.py
fi