#!/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 - 2006 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
umount=0
nocheck=0

while [ "$1" ] ; do
	case "$1" in
	    -full)
		fullclean=1 ; shift ;;
	    -nocheck)
		nocheck=1   ; shift ;;
	    -umount)
		fullclean=1 ; nocheck=1
		umount=1    ; shift ;;
	    -*)
		echo "Usage: $0 [ -full ] [ -nocheck ] [ -umount ] [ <dir(s)> ]"
		echo
		echo " Cleanup the build environment, deleting the directories src, src.*,"
		echo " build/*/ROCK/src.* and various temporary/backup files."
		echo " Also check for old lingering files."
		echo " Without options, remove src and all src.* and build/*/ROCK/src.*"
		echo " directories, and all temporary/backup files, and perform checks for old"
		echo " lingering files."
		echo " If directories are given, delete only them. Directories used by ROCK scripts"
		echo " at the moment are recognized and not deleted."
		echo
		echo "	-full			delete full build directories, "
		echo "				e.g. build/desktop-*"
		echo
		echo "	-nocheck		don't delete temporary/backup files and don't"
		echo "				check for old lingering files" 
		echo
		echo "  -umount			don't remove anything, just umount all the"
		echo "				lingering mounts, implies '-full' and '-nocheck'"
 		echo
		exit 1 ;;
	    *)
		break ;;
	esac
done

# Remove src*
#
if [ $umount = 0 ]; then
  for x in src src.* build/*/ROCK/src.*; do
    if [ -d "$x" -o -L "$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; 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

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

# 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 proc ROCK/loop ROCK/config ROCK/download; do
			umount -d -f    "$x/$y" > /dev/null 2>&1
			umount -d -f -l "$x/$y" > /dev/null 2>&1
			if [ $umount = 0 ]; then
				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" != 0 -a $umount = 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 'lingering' 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: Neither a dir nor a regular file: %p\n'				\
  \)

exit 0