#!/bin/sh -e
# ***** BEGIN LICENSE BLOCK *****
# * Copyright (C) 2007  Alexey Gladkov <legion@altlinux.org>
# * Copyright (C) 2007  Dmitry V. Levin <ldv@altlinux.org>
# *
# * This program 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.
# ***** END LICENSE BLOCK *****

WITH_ACTION=1
. install2-init-functions

if [ -z "$(ls "$destdir")" ]; then
	printf >&2 '%s\n' "${0##*/}: $destdir: directory is empty"
	exit 0
fi

# Prepare mtab in $destdir for postinstall scripts
touch $destdir/etc/mtab

# Run postinstall scripts
ls "$postinstall_dir"/* |
    while read f; do
	[ "$f" != "$postinstall_dir/*" ] || break
	[ -x "$f" ] || continue

	action "Running postinstall script [${f##*/}]" "$f" ||:
    done

# Sync system buffers
sync

# Eject cdrom
if [ -b "$CDROMDEV" ]; then
    sed -i "/ $(printf %s "$destdir" |sed 's,/,\\/,g')/ d" /etc/mtab >/dev/null 2>&1
    umount -fl "$image_dir" 2>/dev/null ||:
    action 'Ejecting removable media' eject -p "$CDROMDEV" ||:
fi
