Blob Blame History Raw
#!/bin/bash
#
# Building kernels with osc made easy

command=
spec=

die()
{
	echo "$@" >&2
	exit 1
}

usage()
{
	die \
"Usage: $0 [build] <specfile>
       $0 upload project [directory]"
}

osc()
{
	echo "osc" $API "$@"
	command osc $API "$@"
}

_osc()
{
	command osc $API "$@"
}

try_spec()
{
	local spec=$1

	if test -e "$spec"; then
		echo "$spec"
		return 0
	elif test -e "kernel-source$VARIANT/$spec"; then
		echo "$_"
		return 0
	else
		return 1
	fi
}

find_spec()
{
	local spec

	case $(uname -m) in
	i?86)
		if try_spec kernel-pae.spec; then
			return 0
		fi
	esac
	if try_spec kernel-default.spec; then
		return 0
	fi
	if test -n "$VANILLA_ONLY"; then
		if try_spec kernel-vanilla.spec; then
			return 0
		fi
	fi
	echo "Unable to find a specfile to build" >&2
	return 1
}

setup_api()
{
	local prjvar=$1 repovar=$2

	PROJECT=${!prjvar}
	REPOSITORY=${!repovar}
	if test -z "$PROJECT"; then
		die "$prjvar not set in config.sh"
	fi
	if test -z "$REPOSITORY"; then
		REPOSITORY=standard
	fi
}


setup_ibs()
{
	API="-A https://api.suse.de"
	WEBCLIENT="https://build.suse.de"
	ARCHS="i586 x86_64 ia64 ppc ppc64 s390 s390x"
	setup_api IBS_PROJECT IBS_REPOSITORY
}

setup_obs()
{
	API="-A https://api.opensuse.org"
	WEBCLIENT="https://build.opensuse.org"
	ARCHS="i586 x86_64"
	setup_api OBS_PROJECT OBS_REPOSITORY
}

create_package()
{
	local project=$1 package=$2

	local meta="<package project=\"$project\" name=\"$package\"><title>$package</title><description/></package>"
	echo "Creating package $package in project $project"
	echo "$meta" | _osc meta pkg -F - "$project" "$package" || exit
}

create_project()
{
	local project=$1

	local archs=$(printf '<arch>%s</arch>' $ARCHS)
	local repository="<repository name=\"standard\"><path repository=\"$REPOSITORY\" project=\"$PROJECT\"/>$archs</repository>"
	local title="<title>Kernel Builds for $(sed -n 's/^GIT Branch: //p' *source-timestamp)</title>"
	local meta="<project name=\"$project\">$title<description/>$repository</project>"
	echo "Creating project $project"
	echo "$meta" | _osc meta prj -F - "$project" || exit
}

package_exists()
{
	local project=$1 package=$2

	_osc meta pkg "$home_project" "$source_package" &>/dev/null
}

project_exists()
{
	local project=$1

	_osc meta prj "$home_project" &>/dev/null
}


#### build ####

do_build()
{
	if test -n "$1"; then
		spec=$1
	else
		spec=$(find_spec) || usage
	fi

	arch=$(uname -m | sed 's/^i.86$/i586/')
	if test -n "$OBS_PROJECT"; then
		setup_obs
	elif test -n "$IBS_PROJECT"; then
		setup_ibs
	else
		die "Neither OBS_PROJECT nor IBS_PROJECT set in config.sh"
	fi

	rm -rf "$(dirname "$spec")/.osc"
	osc build --no-checks --local-package --alternative-project="$PROJECT" "$REPOSITORY" "$spec"
}

### upload ###

do_upload()
{
	if test -n "$OBS_PROJECT"; then
		internal=false
	else
		internal=true
	fi
	case $1 in
	--ibs)
		internal=true
		shift
		;;
	--obs)
		shift
		;;
	esac
	if $internal; then
		setup_ibs
	else
		setup_obs
	fi
	case $# in
	1)
		home_project=$1
		spec=$(try_spec kernel-source$VARIANT.spec)
		if test -z "$spec"; then
			die "Can't find kernel-source$VARIANT directory"
		fi
		dir=$(dirname "$spec")
		;;
	2)
		home_project=$1
		dir=$2
		;;
	*)
		usage
	esac
	source_package="kernel-source$VARIANT"

	if test "$dir" != "."; then
		echo cd "$dir"
		cd "$dir"
	fi
	rm -rf .osc
	if ! project_exists "$home_project"; then
		create_project "$home_project"
	fi
	if ! package_exists "$home_project" "$source_package"; then
		create_package "$home_project" "$source_package"
	fi
	osc init "$home_project" "$source_package" || exit
	osc addremove || exit
	local skip_validation=
	if osc ci --skip-validation --help >/dev/null 2>&1; then
		skip_validation=--skip-validation
	fi
	osc ci $skip_validation -m "imported commit $(sed -n 's/^GIT Revision: //p' *source-timestamp)"
	for spec in *.spec; do
		pkg=${spec%.spec}
		if test "$pkg" = "$source_package"; then
			continue
		fi
		osc linkpac -f -C copy "$home_project" "$source_package" \
		                    "$home_project" "$pkg" || exit
	done

	echo "Done. You can watch the build progress at"
	echo "  $WEBCLIENT/project/monitor?project=${home_project//:/%3A}"
}


case "$1" in
build | upload)
	command=$1
	shift
	;;
-* | "" | *.spec)
	# "build" is the default
	command=build
	;;
*)
	usage
esac

source "$(dirname "$0")/config.sh"
do_$command "$@"