#! /bin/bash



USAGE="<command> [<args>]

The 'config' commands are:

   apply        Apply a diff to one config
   diff         Show diff between two configs
   install      Set up a config for the tracked kernel
   import       Record the current config to the kdist config db
   propagate    Propagate a change to specified configs
   track        Set up the config repository to use
   update-from  Use a template to update configurations

See 'kdist config <command> --help' for more information on a specific command."

#
# For now nothing interesting to see here.
#
kdist_subcommand=$1
shift


case $kdist_subcommand in
apply|diff|import|install|update-from|track|propagate)
	;;
*)
	usage
esac

#
# Some config helpers
#
config__version () {
	kdist__setup_kernel_version &&
	config__sanitize_version v$KERNEL_VERSION_BASE
}

config__architecture () {
	local arch

	arch=$(config__get_architecture "$1") &&
	case $arch in
	i386)	echo x86_32 ;;
	*)	echo $arch
	esac
}

config__system_architecture () {
	local arch

	arch=$(kdist__architecture) &&
	case $arch in
	i386)	echo x86_32 ;;
	*)	echo $arch
	esac
}

config__resolve_db () (
	# absolute path is forbidden
	if test $1 && test ${1:0:1} = '/'; then
		warn "refuse to resolve absolute path: '$1'"
		return
	fi

	repo=$(configs__get_repository) || {
		warn "Unable to resolve '$1'"
		return
	}

	#
	# First see if the path is complete and relative to the config
	# repo directory.
	#
	if test -f "$repo/$1"; then
		echo "$repo/$1"
		return
	fi

	#
	# From now we need some information to resolve the path,
	# therefore we must be in a kernel repository to retrieve
	# either the flavour or the current kernel version.
	#
	kdist__cd_kernel_topdir || {
		warn "can't resolve config '$1'"
		return
	}

	rev=$(config__version)

	#
	# Resolve 'arch' and 'flavour' if possible either by using the
	# passed config file or by using the system information.
	#
	if ! test "$2"
	then
		arch=$(config__system_architecture)
		flavour=
	else
		if ! test -f "$2"; then
			warn "Config not found: $2"
			return
		fi
		arch=$(config__architecture "$2")
		flavour=$(config__get_flavour "$2")
	fi


	if test -z "$1"; then
		#
		# flavour can be missing from the current conf, and we
		# can't do a full resolution in that case.
		#
		if ! test $flavour; then
			warn "flavour is missing to resolve the current config"
			return
		fi
		# safe since the path is absolute
		config__resolve "$repo/$rev/$arch/$flavour"
		return
	fi

	for f in $rev/"$1" $rev/$arch/"$1" "$1"/$flavour "$1"/$arch/$flavour
	do
		[ -f "$repo/$f" ] && {
			echo "$repo/$f"
			return
		}
	done
	warn "Unable to resolve '$1' with"
	warn "   flavour      : $flavour"
	warn "   revision     : $rev"
	warn "   architecture : $arch"
)

config__resolve () {
	#
	# First try to find the file in the current dir
	#
	if test -f "$1"; then
		# Return the absolute path always
		case $1 in
		[^/]*)	echo "$(pwd)/$1" ;;
		*)	echo "$1"
		esac
		return
	fi
	#
	# Otherwise the path must be relative to the DB
	#
	case $1 in
	""|[^/]*)	config__resolve_db "$@"
			return ;;
	esac
	warn "config not found '$1'"
}


#
# start the config sub command
#
source $libexecdir/kdist-$kdist_command--$kdist_subcommand "$@"
