#!/bin/sh -e
#
# Copyright (C) 2003-2005  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2005  Anton D. Kachalov <mouse@altlinux.org>
# Copyright (C) 2006  Konstantin A. Lepikhov <lakostis@altlinux.org>
# 
# This file defines functions used by spt scripts.
#
# 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.
#

PROG="${0##*/}"

# spt directory.
spt_dir=/usr/share/mspt/spt

# class directory
classdir=$spt_dir/classes

Info()
{
	printf %s\\n "$PROG: $*" >&2
}

verbose=
Verbose()
{
	[ -n "$verbose" ] || return 0
	Info "$*"
}

Fatal()
{
	printf %s\\n "$PROG: Fatal $*" >&2
	exit 1
}

print_version()
{
	local prog="$1" && shift
	cat <<EOF
$prog version 0.4.0
Written by Anton D. Kachalov <mouse@altlinux.org>
Modified by Konstantin A. Lepikhov <lakostis@altlinux.org>

Copyright (C) 2005  Anton D. Kachalov <mouse@altlinux.org>
Copyright (C) 2006  Konstantin A. Lepikhov <lakostis@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

show_usage()
{
	[ -z "$*" ] || Info "$*"
	printf %s\\n "Try \`$PROG --help' for more information." >&2
	exit 1
}
workdir=
chroot_run()
{
		local out
		if ! out="$(hsh-run $verbose $quiet --rooter --save-fakeroot --mount=/dev/pts "$workdir" -- "$@" 2>&1)"; then
				printf %s\\n "$out" >&2
				Fatal 'failed to run cmd in chroot.'
		fi
}

workdir=
chroot_exec()
{
		local out
		local script="$1"
		if ! out="$(hsh-run $verbose $quiet --rooter --save-fakeroot --mount=/dev/pts --execute="$script" "$workdir" 2>&1)"; then
				printf %s\\n "$out" >&2
		fi
}

quiet=
verbose=
workdir=
init_chroot_progs()
{
    makedev_console=1 \
    hsh "$workdir" $quiet $verbose \
         ${ARCH:+--target="$ARCH"} \
         ${REPO:+--repo="$REPO"} \
         ${LANGS:+--install-langs="$LANGS"} \
         ${apt_config:+--apt-config=$apt_config} \
         ${apt_prefix:+--apt-prefix=$apt_prefix} \
         --initroot-only \
         --save-fakeroot \
         --pkg-init-list='setup filesystem rpm fakeroot>=0:0.7.3 findutils tar'" $@" \
         --pkg-build-list=',' || Fatal 'Could not init boot.'
}

workdir=
gen_aptbox()
{
	hsh --cleanup "$workdir"
	mkaptbox "$workdir" $quiet $verbose \
	     ${target:+--target="$ARCH"} \
	     ${repo:+--repo="$REPO"} \
	     ${apt_config:+--apt-config="$apt_config"} \
	     ${apt_prefix:+--apt-prefix="$apt_prefix"}
}

ARCH=
VENDOR=
COMPONENTS_ID=
workdir=
gen_repo()
{
	local repo="$1"
	"$workdir/aptbox/genbasedir" --topdir="$repo" \
	     --no-oldhashfile \
	     ${NAME:+--archive="$NAME"} \
	     ${VERSION:+--version="$VERSION"} \
             ${ORIGIN:+--origin="$ORIGIN"} \
             ${LABEL:+--label="$LABEL"} \
             --architecture="$ARCH" \
             --notautomatic=false \
             $VENDOR $COMPONENTS_ID &&
         Verbose "updated \`$COMPONENTS_ID' repository indices." ||
         Fatal "failed to update \`$COMPONENTS_ID' repository indices."
}

postinstall()
{
    local ROOT_ type_ setupdir cmd
    type_=$1
    ROOT_=$2
    setupdir=$3

    # FIXME!!!
    chroot_run chown root:root /
    chroot_run chmod 755 /

    [ -z "$setupdir" ] && setupdir=$classdir/$type_/setup.d
    if [ ! -d "$setupdir" ];then
      Info "Directory with post-install scripts not found: $setupdir"
      return
    fi

    for i in $setupdir/*;do
	[ "$i" != "$setupdir/*" ] || break
	Info "exec $i"
	chroot_exec "$i" ||:
    done
}

postinstall_local()
{
    local PROFILEDIR ROOT
    PROFILEDIR=$1
    POSTLOCAL=$2
    ROOT=$3
    setupdir=$PROFILEDIR/postinstall/$POSTLOCAL
    if [ ! -d "$setupdir" ];then 
	Info "Profile directory with post-install scripts not found: $setupdir"
	return
    fi
    postinstall local $ROOT $setupdir
}

workdir=
print_uris()
{
	local out
	if ! out="$("$workdir/aptbox/apt-get" -q -y --print-uris install -- "$@" 2>&1)"; then
		printf %s\\n "$out" >&2
		Fatal "failed to calculate package file list."
	fi

	local pattern0="'\\([a-z]\\+\\):\\([^']\\+\\)' .*"
	local pattern1="'\\(file\\|copy\\):\\([^']\\+\\)' .*"

	local all_uris
	if ! all_uris="$(printf %s "$out" |sed -ne "s/^$pattern0/\\2/pg")"; then
		printf %s\\n "$out" >&2
		Fatal "failed to filter package file list."
	fi
	local uris
	if ! uris="$(printf %s "$out" |sed -ne "s/^$pattern1/\\2/pg")"; then
		printf %s\\n "$out" >&2
		Fatal "failed to filter package file list."
	fi

	if [ "$all_uris" != "$uris" ]; then
		printf %s\\n "$out" >&2
		Fatal "calculated package file list is not local."
	fi
	printf %s "$uris" &&
		Verbose "calculated package file list."
}
