#!/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/Cleanup
# ROCK Linux is Copyright (C) 1998 - 2003 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 ---
#
# Cleanup the build environment 
#

fullclean=0
nocheck=0

while [ "$1" ] ; do
	case "$1" in
	    -full)
		fullclean=1 ; shift ;;
	    -nocheck)
		nocheck=1   ; shift ;;
	    -*)
		echo "Usage: $0 [ -full ] [ -nocheck ] [ dir(s) ]"
		exit 1 ;;
	    *)
		break ;;
	esac
done

# Remove src*
#
for x in src* ; do
    if [ -d "$x" ] ; then
	if [ "$#" != 0 ] ; then
		delme=0
		for y ; do [ "${y%/}" = "$x" ] && delme=1 ; done
		[ "$delme" = 0 ] && continue
	fi
	delme=1
	for y in build.pid R.src/build.pid ; do
		if [ $delme = 1 -a -f "$x/$y" ] ; then
			if [ "`fuser "$x/$y"`" ] ; then
				echo "Found active $y in $x: Not removing!"
				delme=0
			fi
		fi
	done
	for y in proc R.orig/download R.orig R.build R.src/mnt* mnt* ; do
		if [ $delme = 1 -a -d "$x/$y" ] ; then
			umount -d -f    "$x/$y" > /dev/null 2>&1
			umount -d -f -l "$x/$y" > /dev/null 2>&1
			rmdir  "$x/$y" > /dev/null 2>&1
			if [ -d "$x/$y" ] ; then
				echo "Found $y in $x: Not removing!"
				delme=0
			fi
		fi
	done

	if [ $delme = 1 ] ; then
		echo "removing $x .."
		rm -rf "$x"
	fi
    fi
done

# Remove build/*
#
fullhelp=0
for x in build/* ; do
    if [ -d "$x" ] ; then
	if [ "$#" != 0 ] ; then
		delme=0
		for y ; do [ "${y%/}" = "$x" ] && delme=1 ; done
		[ "$delme" = 0 ] && continue
	fi
	if [ $fullclean = 1 ] ; then
		for y in dev proc ; do
			umount -d -f    "$x/root/$y" > /dev/null 2>&1
			umount -d -f -l "$x/root/$y" > /dev/null 2>&1
			rmdir  "$x/root/$y" > /dev/null 2>&1
			if [ -d "$x/root/$y" ] ; then
				echo "Found $y in $x: Not removing!"
				delme=0
			fi
		done
		if [ "$delme" != 0 ] ; then
			echo "removing $x .."
			rm -rf "$x"
		fi
	else
		echo "Not removing $x."
		fullhelp=1
	fi
    fi
done
[ $fullhelp -eq 1 ] && echo "Use '$0 -full' to also remove build/*."

[ "$nocheck" = 1 ] && exit 0

# Remove temp/backup files
#
bash scripts/xfind.sh Documentation/. architecture/. misc/. \
	package/. scripts/. target/. -type f \( -name '*~' -o \
	-name 'a.out' -o -name 'core.*' -o -name 'core' \) | xargs rm -vf

# Print warnings for 'liggering' files
#
bash scripts/xfind.sh Documentation/. architecture/. misc/. \
	package/. scripts/. target/.	\
  \(									\
	\( -name 'DEADJOE' -o -name '*-[xX]' -o -name '.[^.]*'		\
           -o -name '*.orig' -o -name '*.rej' -o -name '*#*'		\
           -o -name '*.mine' -o -name '*.r[1-9][0-9]*'			\
           -o -name TRANS.TBL -o -name '*.cksum-err' -o -name x		\
	   -o -name '*[.-]old' -o -name a.out -o -name '*~'		\
	   -o -name '*.incomplete' -o -name '*.ckext-err' \)		\
	-printf 'WARNING: Found %p\n'					\
  \) -o \(								\
	\( \( ! -type l ! -perm 755 ! -perm 644 \) -o			\
	   \( -type l ! -perm 777 \) \)					\
	-printf 'WARNING: Non-Standard Perm at %p\n'			\
  \) -o \(								\
	\( ! -type d ! -type f \)					\
	-printf 'WARNING: Dangerous file: %p\n'				\
  \)

exit 0