#!/bin/sh -e
#
# $Id: initroot,v 1.32 2003/11/26 09:55:06 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The initroot 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

. /usr/share/hasher/functions

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
initroot - fills chroot with initial set of packages.

This program is free software, covered by the GNU General Public License.
initroot comes with ABSOLUTELY NO WARRANTY, see license for details.

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

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

Valid options are:
--no-cache                      do not use cache;
--no-contents-indices           do not use content indices;
--repackage-source              package source as well as binaries;
--number=NUMBER                 subconfig identifier;
--hasher-priv-dir=DIR           hasher-priv directory;
--nprocs=NUMBER                 number of CPUs to use;
--install-langs=LANGLIST        list of languages to install;
--contents-index-bin=INDEX      pkg contents binary index;
--contents-index-all=INDEX      pkg contents complete index;
-q, --quiet                     try to be more quiet;
-v, --verbose                   print a message for each action;
-h, --help                      show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o q,v,h -l no-cache,no-contents-indices,repackage-source,number:,hasher-priv-dir:,nprocs:,install-langs:,contents-index-all:,contents-index-bin:,quiet,verbose,help -- "$@"` || Usage
eval set -- "$TEMP"

no_cache=
no_contents_indices=
nprocs=
install_langs=all
contents_index_all=
contents_index_bin=
repackage_source=
while :; do
	case "$1" in
		--no-cache) no_cache=1; shift
			;;
		--no-contents-indices) no_contents_indices=1; shift
			;;
		--repackage-source) repackage_source=1; shift
			;;
		--number)
			shift
			number="${1##-}"
			[ -z "$(echo -E "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			shift
			;;
		--nprocs)
			shift
			nprocs="$1"
			[ -z "$(echo -E "$nprocs" |tr -d [0-9])" ] ||
				Fatal "--nprocs: $nprocs: invalid number."
			shift
			;;
		--install-langs) shift; install_langs="$1"; shift
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"; shift
			;;
		--contents-index-all) shift; contents_index_all="$1"; shift
			;;
		--contents-index-bin) shift; contents_index_bin="$1"; shift
			;;
		-q|--quiet) quiet=-q; shift
			;;
		-v|--verbose) verbose=-v; shift
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
done

# exactly one argument, please.
[ "$#" -eq 1 ] || Usage

set_workdir "$1"
shift

[ -d "$chroot" ] || Fatal "$chroot: cannot access chroot."
[ -d "$aptbox" ] || Fatal "$aptbox: cannot access aptbox."
[ -r "$APT_CONFIG" ] || Fatal "$APT_CONFIG: cannot access APT configuration."

mkdir -p -m700 $verbose cache
mkdir -p -m700 $verbose cache/chroot/{list,package}{,.new}
mkdir -p -m700 $verbose cache/contents/{list,index}{,.new}

check_helpers

ugid2="$("$getugid2" $number)" ||
	Fatal "getugid2 failed."
uid2="$(echo "$ugid2" |cut -d: -f1)"
gid2="$(echo "$ugid2" |cut -d: -f2)"

[ -n "$nprocs" ] ||
	nprocs="$(N=`egrep -cs ^cpu[0-9]+ /proc/stat ||:`; [ "$N" -gt 0 ] 2>/dev/null && echo -n "$N" || echo 1)"

check_tty

. "$hasher_dir/cache_chroot"
. "$hasher_dir/cache_contents"

initlist="$(print_uris setup filesystem rpm 'fakeroot>=0:0.7.3')" ||
	Fatal "failed to generate initial package file list."
[ -n "$initlist" ] &&
	Verbose "generated initial package file list." ||
	Fatal "failed to generate non-empty initial package file list; check your sources.list"

# reorder to get setup and filesystem first.
initlist="$(echo "$initlist" |fgrep /setup-)
$(echo "$initlist" |fgrep /filesystem-)
$(echo "$initlist" |egrep -v '/(setup|filesystem)-')"

[ -n "$no_cache" ] || echo "$initlist" >"cache/chroot/list.new/init"

buildlist="$(print_uris basesystem 'rpm-build>=0:4.0.4-alt21' 'kernel-headers-common>=1.1.4-alt1' time)" ||
	Fatal "failed to generate build package file list."
buildlist="$(
	echo "$buildlist" |while read bp; do
		echo "$initlist" |grep -qs "^$bp\$" || echo "$bp"
	done
)"
[ -n "$buildlist" ] &&
	Verbose "generated build package file list." ||
	Fatal "failed to generate non-empty build package file list; check your sources.list"

[ -n "$no_cache" ] || echo "$buildlist" >"cache/chroot/list.new/build"

check_chroot_cache

if [ -n "$no_cache" -o -n "$rebuild_chroot_cache" ]; then
	create_chroot
else
	unpack_chroot_cache
fi

check_contents_cache

if [ -n "$no_cache" -o -n "$rebuild_contents_cache" ]; then
	create_contents
else
	unpack_contents_cache
fi


# Create /dev/log.
/usr/bin/mksock -m666 "$chroot/dev/log" ||
	Fatal "failed to create \`$chroot/dev/log' socket."

# Create RPM build directory tree.
cat >"$entry" <<__EOF__
$entry_header

rpminit $verbose
cat >>/usr/src/.rpmmacros <<\EOF
%packager Automated package hasher <hasher@localhost>
%buildhost ${USER:-`id -nu`}.hasher.altlinux.org
%__BTE hasher
%__nprocs $nprocs
${contents_index_bin:+%_pkg_contents_index_bin /.host/contents_index_bin}
%_rpmbuild_clean 0
${repackage_source:+%_rpmbuild_packagesource 1}
EOF
__EOF__
chmod 755 "$entry"

"$chrootuid2" $number "$chroot" /.host/entry &&
	Verbose "created RPM build directory tree." ||
	Fatal "failed to create RPM build directory tree."

