#!/bin/bash -ef
# note: bash-dependent, see checkbashisms
#
# Copyright (C) 2003-2008  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2006  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2007  Kirill A. Shutemov <kas@altlinux.org>
# 
# The hsh utility for the hasher project
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. hsh-sh-functions

show_help()
{
	cat <<EOF
hsh - build package using hasher utilities.

Usage: $PROG [options] [<path-to-workdir>] <package>...

<path-to-workdir> must be valid writable directory.

Options:
  --apt-config=FILE                 path to custom apt.conf file;
  --apt-prefix=DIR                  path to apt directory prefix (e.g. /usr);
  --build-args=ARGS                 extra arguments for rpmbuild;
  --cleanup-only                    do not build at all, just cleanup workdir;
  --eager-cleanup                   cleanup build environment after each
                                    successful build;
  --lazy-cleanup                    cleanup build environment before each
                                    new build;
  --excludedocs                     do not install documentation files;
  --hasher-priv-dir=DIR             alternative hasher-priv directory;
  --initroot-only                   do not build, stop after initroot stage;
  --install-langs=LIST              colon-separated list of languages to install;
  --mountpoints=LIST                comma-separated list of known mount points;
  --cache-dir=DIR                   alternative cache directory;
  --no-cache                        do not use initroot cache;
  --no-contents-indices             do not use content indices;
  --no-repackage-source             do not repackage source along with binaries;
  --no-sisyphus-check-in[=LIST]     do not run sisyphus_check input tests
                                    [specified in comma-separated LIST];
  --no-sisyphus-check[=LIST]        do not run sisyphus_check tests
                                    [specified in comma-separated LIST];
  --no-sisyphus-check-out[=LIST]    do not run sisyphus_check output tests
                                    [specified in comma-separated LIST];
  --nprocs=NUMBER                   number of CPUs to use;
  --number=NUMBER                   subconfig identifier;
  --packager=EMAIL                  override default RPM packager tag;
  --pkg-build-list=LIST             override default build package file list;
  --pkg-init-list=LIST              override default initial package file list;
  --query-repackage                 repackage the source before query
                                    for requirements;
  --query-req-prog=FILE             program to run to query for requirements
                                    instead of autogenerated script;
  --rebuild-prog=FILE               program to run for rebuild instead of
                                    autogenerated script;
  --repackage-source                repackage source along with binaries;
  --repo=DIR                        repository directory;
  --repo-bin=DIR                    binary packages destination directory,
                                    overriding --repo option for binary packages;
  --repo-src=DIR                    source packages destination directory,
                                    overriding --repo option for source packages;
  --save-fakeroot                   save fakeroot state;
  --target=ARCH                     target architecture;
  --wait-lock                       wait for workdir and hasher-priv locks;
  --no-wait-lock                    do not wait for workdir and hasher-priv locks;
  --without-stuff                   do not use built packages;
  --with-stuff                      allow use of built packages;
  --with-qemu=ARCH                  copy qemu executable into the chroot .host directory;
  -q, --quiet                       try to be more quiet;
  -v, --verbose                     print a message for each action;
  -V, --version                     print program version and exit;
  -h, --help                        show this text and exit.
    
Report bugs to http://bugs.altlinux.ru/

EOF
	exit
}

TEMP=`getopt -n $PROG -o $getopt_common_opts -l apt-config:,apt-prefix:,build-args:,cache-dir:,cleanup-only,contents-index-all:,contents-index-bin:,eager-cleanup,excludedocs,hasher-priv-dir:,initroot-only,install-langs:,lazy-cleanup,mountpoints:,no-cache,no-contents-indices,no-repackage-source,no-sisyphus-check::,no-sisyphus-check-in::,no-sisyphus-check-out::,no-stuff,nprocs:,number:,packager:,pkg-build-list:,pkg-init-list:,query-repackage,query-req-prog:,rebuild-prog:,repackage-source,repo:,repo-bin:,repo-src:,save-fakeroot,target:,wait-lock,no-wait-lock,without-stuff,with-stuff,with-qemu:,$getopt_common_longopts -- "$@"` ||
	show_usage
eval set -- "$TEMP"

cleanup_only=
initroot_only=
while :; do
	case "$1" in
		--apt-config)
			apt_config="$(opt_check_read "$1" "$2")"
			shift
			;;
		--apt-prefix)
			apt_prefix="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--build-args) shift; rpmargs="$1"
			;;
		--cache-dir) shift; cache_dir="$1"
			;;
		--cleanup-only) cleanup_only=1
			;;
		--eager-cleanup) lazy_cleanup=
			;;
		--excludedocs) exclude_docs=--excludedocs
			;;
		--hasher-priv-dir)
			hasher_priv_dir="$(opt_check_dir "$1" "$2")"
			shift
			;;
		--initroot-only) initroot_only=1
			;;
		--install-langs) shift; install_langs="$1"
			;;
		--lazy-cleanup) lazy_cleanup=1
			;;
		--mountpoints) shift; known_mountpoints="$1"
			;;
		--no-cache) no_cache=1
			;;
		--no-contents-indices) no_contents_indices=1
			;;
		--no-repackage-source|--repackage-source) repackage_source="$1"
			;;
		--no-sisyphus-check) shift
			[ -n "$1" ] && no_sisyphus_check="$1" || no_sisyphus_check=all
			;;
		--no-sisyphus-check-in) shift
			[ -n "$1" ] && no_sisyphus_check_in="$1" || no_sisyphus_check_in=all
			;;
		--no-sisyphus-check-out) shift
			[ -n "$1" ] && no_sisyphus_check_out="$1" || no_sisyphus_check_out=all
			;;
		--nprocs)
			nprocs="$(opt_check_number_ge_0 "$1" "$2")"
			shift
			;;
		--number)
			if [ -z "${2##/*}" ]; then
				[ -d "$2" ] ||
					fatal "$1: $2: Directory not available."
				number="$2"
			else
				number="$(opt_check_number_ge_0 "$1" "$2")"
			fi
			shift
			;;
		--packager) shift; packager="$1"
			;;
		--pkg-build-list) shift; build_list="$1 "
			;;
		--pkg-init-list) shift; init_list="$1 "
			;;
		--query-repackage) query_repackage=1
			;;
		--query-req-prog)
			prog_query_req="$(opt_check_read "$1" "$2")"
			shift
			;;
		--rebuild-prog)
			prog_rebuild="$(opt_check_read "$1" "$2")"
			shift
			;;
		--repo) shift; repo="$1"
			;;
		--repo-bin) shift; repo_bin="$1"
			;;
		--repo-src) shift; repo_src="$1"
			;;
		--save-fakeroot) save_fakeroot=1
			;;
		--target) shift; target="$1"
			[ -z "${target##[A-Za-z]*}" ] ||
				fatal "--target: $target: invalid architecture."
			;;
		--wait-lock) lock_nowait=
			;;
		--no-wait-lock) lock_nowait=1
			;;
		--without-stuff|--no-stuff) no_stuff=1
			;;
		--with-stuff) no_stuff=
			;;
		--with-qemu)
			qemu_arch="$2"
			shift
			;;
		--contents-index-*)
			message "Warning: obsolete option $1 ignored."
			shift
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

[ "$repackage_source" = '--no-repackage-source' ] ||
	repackage_source='--repackage-source'

if [ -n "$initroot_only" -a -n "$cleanup_only" ]; then
	show_usage '--initroot-only and --cleanup-only are mutually exclusive options.'
fi

if [ -n "$prog_query_req" -a -n "$query_repackage" ]; then
	show_usage '--query-req-prog and --query-repackage are mutually exclusive options.'
fi

if [ -z "$initroot_only" -a -z "$cleanup_only" ]; then
	if [ -z "$workdir" ]; then
		# At least two arguments.
		[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
	fi

	if [ -z "$workdir" -o -d "${1:-}" ]; then
		set_workdir "${1:-}"
		shift
	else
		set_workdir
	fi

else
	if [ -z "$workdir" ]; then
		# Exactly one argument.
		[ "$#" -ge 1 ] || show_usage 'Insufficient arguments.'
		[ "$#" -le 1 ] || show_usage 'Too many arguments.'
	fi

	if [ "$#" -ge 1 ]; then
		set_workdir "$1"
		shift
	else
		set_workdir
	fi
fi

repo_bin="${repo_bin:-${repo:-$def_repo}/"${target:-$def_target}"/RPMS.hasher/}"
repo_src="${repo_src:-${repo:-$def_repo}/SRPMS.hasher/}"
repo="${repo:-$def_repo}"
target="${target:-$def_target}"

repo_dir_to_abs()
{
    if [ ${1:0:1} = "/" ]; then
	echo $1
    else
	echo "$workdir/$1"
    fi
}

repo_bin=`repo_dir_to_abs "$repo_bin"`
repo_src=`repo_dir_to_abs "$repo_src"`
repo=`repo_dir_to_abs "$repo"`

cat << EOF
apt_config=$apt_config
apt_prefix=$apt_prefix
cache_dir=$cache_dir
chroot=$chroot
known_mountpoints=$known_mountpoints
no_stuff=$no_stuff
number=$number
packager=$packager
repo=$repo
repo_bin=$repo_bin
repo_src=$repo_src
target=$target
workdir=$workdir
def_repo=$def_repo
def_target=$def_target
EOF

exit 0
# TODO:
cat >/dev/null << EOF
build_list=$build_list
exclude_docs=$exclude_docs
hasher_priv_dir=$hasher_priv_dir
install_langs=$install_langs
init_list=$init_list
no_cache=$no_cache
no_contents_indices=$no_contents_indices
no_sisyphus_check=$no_sisyphus_check
no_sisyphus_check_in=$no_sisyphus_check_in
no_sisyphus_check_out=$no_sisyphus_check_out
nprocs=$nprocs
prog_rebuild=$prog_rebuild
prog_query_req=$prog_query_req
qemu_arch=$qemu_arch
query_repackage=$query_repackage
repackage_source=$repackage_source
rpmargs=$rpmargs
save_fakeroot=$save_fakeroot
EOF
