#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Build-Pkg
# ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
# 
# 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

# Helper functions
is_installed()
{
	if [ $install_checks_true == 1 ]; then return 0; fi
	[ -e "$1" ];
}

is_removed()
{
	if [ $remove_checks_true == 1 ]; then return 0; fi
	[ ! -e "$1" ];
}

all_installed() {
	while read dummy M; do
		[ -e "/$M" ] && echo "/$M"
	done < <( cat /var/adm/postinstall/*-install.?????? 2> /dev/null | grep "$1"; ) | sort -u
}

all_removed() {
	while read dummy M; do
		[ -e "/$M" ] || echo "/$M"
	done < <( cat /var/adm/postinstall/*-remove.?????? 2> /dev/null | grep "$1"; ) | sort -u
}

all_touched() {
	while read dummy M; do
		[ -e "/$M" ] || echo "/$M"
	done < <( cat /var/adm/postinstall/*-install.?????? /var/adm/postinstall/*-remove.?????? 2> /dev/null | grep "$1"; ) | sort -u
}

any_installed() {
	if [ $install_checks_true == 1 ]; then return 0; fi
	cat /var/adm/postinstall/*-install.?????? 2> /dev/null | grep -q "$1" 
}

any_removed() {
	if [ $remove_checks_true == 1 ]; then return 0; fi
	cat /var/adm/postinstall/*-remove.?????? 2> /dev/null | grep -q "$1" 
}

any_touched() {
	if [ $install_checks_true == 1 ]; then return 0; fi
	if [ $remove_checks_true == 1 ]; then return 0; fi
	cat /var/adm/postinstall/*-install.?????? /var/adm/postinstall/*-remove.?????? 2> /dev/null | grep -q "$1" 
}

install_checks_true=0
remove_checks_true=0

while [ "$#" -ge 1 ]; do
	case "$1" in
		-a)
			install_checks_true=1
			shift
			;;
		-r)
			remove_checks_true=1
			shift
			;;
		*)
			echo
			echo "Usage: $0 [ -a ] [ -r ]"
			echo
			echo "	-a	execute all postinstall actions"
			echo "	-r	execute all postremove actions"
			echo
			exit 1
			;;
	esac
done

# Backup postinstall scripts for postremove operation
all_installed "etc/postinstall/.*\.sh" | while read M;
do
	echo "Creating backup of $M in /etc/postinstall/postremove/"
	cp -af "$M" "/etc/postinstall/postremove/${M##*/}"
done

# Execute removed postinstall scripts
all_removed "etc/postinstall/.*\.sh" | while read M; 
do
	P="/etc/postinstall/postremove/${M##*/}"
	if [ -e $P ]; then
		echo "Executing postremove script $P"
		. "$P"
		rm -f "$P"
	else
		echo "WARNING: backup of $M not found in /etc/postinstall/postremove"
	fi
done
	
# Source postinstall scripts
for N in /etc/postinstall/*.sh; 
do
	echo "Executing postinstall script $N"
	. "$N"
done

# Remove the postinstall/postremove logs
rm -f /var/adm/postinstall/*-{install,remove}.??????