#! /bin/bash

kdist=$0

#
# The version can be changed during the installation.
#
KDIST_VERSION=v0.0.4

kdist_version() (
	local version

	# file used in tarball realeases
	if test -f kdist-version; then
		cat kdist-version
		return
	fi

	cd $(dirname $0)

	if ! test -d .git; then
		echo $KDIST_VERSION
		return
	fi

	version=$(git describe --match "v[0-9]*" --abbrev=4 HEAD 2>/dev/null) || {
		echo $KDIST_VERSION
		return
	}
	git update-index -q --refresh 2>/dev/null
	test -z "$(git diff-index --name-only HEAD --)" || {
		version+=-dirty
	}
	echo $version
)

#
# This path is changed during installation.
#
LIBEXECDIR=/usr/libexec/kdist

libexecdir=${KDIST_EXEC_PATH:-$LIBEXECDIR}

test -d "$libexecdir" || {
	echo >&2 "invalid exec-path '$libexecdir'"
	exit 1
}

libexecdir=$(cd "$libexecdir" && pwd)

#
#
#
kdist_usage() {
	cat >&2 <<EOF
Usage: kdist [--version] <command> [<args>]

The kdist commands are:

   config       Deals with kernel configs operations.
   log          Show what changed between different kernel version.
   package      Build various packages (source, binary, devel...).
   release      Deals with kernel release operations.
   whatchanged  Show what happened between two releases.

See 'kdist <command> --help' for more information on a specific command.
EOF
}

while
	case "$#,$1" in
	*,--version)
		echo kdist version $(kdist_version)
		exit ;;
	*,-*|0,*)
		kdist_usage
		exit 1 ;;
	*)
		break;;
	esac
do
	shift
done

kdist_command=$1
shift

#
# Include all kdist libraries
#
source $libexecdir/kdist--lib || exit

#
# Call the command handler: it assumes to be in kdist topdir.
#
if ! test -f $libexecdir/kdist-$kdist_command; then
	die "'$kdist_command' command not found."
fi

source $libexecdir/kdist-$kdist_command "$@"
