#!/usr/bin/bash

# for +() in request
shopt -s extglob

function filter {
	echo "$@" | sed '
		s/+/%2B/g
		'
}

function show_req {
	xdg-open "https://$BS/request/show/$1"
}

function show_pkg {
	PRJ="`filter $1`"
	PKG="`filter $2`"
	xdg-open "https://$BS/package/show/$PRJ/$PKG"
}

function show_prj {
	PRJ="`filter $1`"
	xdg-open "https://$BS/project/monitor/$PRJ"
}

function show_devel {
	PRJ="`osc develproject openSUSE:Factory $1`" || exit 1
	show_pkg "$PRJ" "$1"
}

BS=build.opensuse.org
DEVEL=0

while getopts "dip" OPT; do
	case "$OPT" in
		i) BS=build.suse.de
		;;
		p) BS=pmbs.links2linux.de
		;;
		d) DEVEL=1
		;;
	esac
done

shift $(($OPTIND-1))

if [ $DEVEL -eq 1 ]; then
	show_devel "$1"
elif [ $# -eq 1 ]; then
	case "$1" in
	+([0-9]))
		show_req "$1"
		;;
	*/*)
		show_pkg "${1%/*}" "${1#*/}"
		;;
	*)
		show_prj "$1"
		;;
	esac
elif [ $# -eq 2 ]; then
	show_pkg "$1" "$2"
else
	exit 1
fi
