#!/bin/sh

# The contents of this file are subject to the MonetDB Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.monetdb.org/Legal/MonetDBLicense
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is the MonetDB Database System.
#
# The Initial Developer of the Original Code is CWI.
# Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
# Copyright August 2008-2015 MonetDB B.V.
# All Rights Reserved.

for i in /sbin /usr/sbin /bin /usr/bin /usr/local/bin /usr/local/gnu/bin /cwi/bin ; do
	if [ -d "$i" ] ; then
		PATH="$i:$PATH"
	fi
done

THISPATH=${0%/*}
THISFILE=${0##*/}
CHILD=$$
PARENT=""
LOCKFILE="$$"
CONCURRENT=''
while [ "${1#-}" != "$1" ] ; do
  if  [ "$1" = "--concurrent" ] ; then  CONCURRENT='yes'   ; shift
    elif  [ "$1" =      "-l"  ] ; then  LOCKFILE="$2"      ; shift ; shift
    elif  [ "$1" != "${1#-l}" ] ; then  LOCKFILE="${1#-l}" ; shift 
    elif  [ "$1" =      "-p"  ] ; then  PARENT="$2"        ; shift ; shift
    elif  [ "$1" != "${1#-p}" ] ; then  PARENT="${1#-p}"   ; shift 
  fi
done
LOCKFILE=/tmp/.${0##*/}.$LOCKFILE

files="`ls -d $* 2>/dev/null | egrep -v '\.KILLED\.(out|err)$'`"

if [ ! "$files" ] ; then  exit 0 ; fi

while [ -f $LOCKFILE ] ; do sleep 9 ; done
touch $LOCKFILE

function  LOG1  () { prompt="\n# `date +%H:%M:%S` >  " ; echo -en "$prompt$prompt$*$prompt\n\n" >&2 ; }
function  LOG2  () {  LOG1 $* ; LOG1 $* >&2 ; }
function  LOG1x () {  LOG1 $* ; "$@" >&2 ; }
function  LOG2x () {  LOG2 $* ; "$@" ; }

cpids=""
if [ "$PARENT" ] ; then
  cpids="`ps -ef | egrep '^ *[a-zA-Z0-9_]+ +[0-9]+ +'"$PARENT"' ' | egrep -v '^ *[a-zA-Z0-9_]+ +'"$CHILD"' +[0-9]+ | '"$0"' | '"$THISPATH/Mtest.py"' ' | sort -k 4.1 | awk '{printf  "%s " , $2}' | perl -pe 's|  *$||'`"
  if [ "$cpids" ] ; then
#	KIDS="`echo "($cpids)" | perl -pe 's/ /|/g'`"
#	gpids="`ps -ef | egrep '^ *[a-zA-Z0-9_]+ +[0-9]+ +'"$KIDS"' ' | egrep -v '^ *[a-zA-Z0-9_]+ +'"$CHILD"' +[0-9]+ | '"$0"' ' | sort -k 4.1 | awk '{printf  "%s " , $2}' | perl -pe 's|  *$||'`"
#	if [ "$gpids" ] ; then
#		cpids="$cpids $gpids"
#	fi
	cpids="$cpids "
  fi
fi

BINPATH=""
if [ ! "$CONCURRENT" ] ; then
	BINPATH="$THISPATH"
	if [    "$MONETDB_PREFIX" ] ; then BINPATH="$BINPATH    $MONETDB_PREFIX/bin" ; fi
	if [    "$TESTING_PREFIX" ] ; then BINPATH="$BINPATH    $TESTING_PREFIX/bin" ; fi
	if [    "$CLIENTS_PREFIX" ] ; then BINPATH="$BINPATH    $CLIENTS_PREFIX/bin" ; fi
	if [   "$MONETDB5_PREFIX" ] ; then BINPATH="$BINPATH   $MONETDB5_PREFIX/bin" ; fi
fi
if [   "$TEMPLATE_PREFIX" ] ; then BINPATH="$BINPATH   $TEMPLATE_PREFIX/bin" ; fi
if [        "$SQL_PREFIX" ] ; then BINPATH="$BINPATH        $SQL_PREFIX/bin" ; fi
if [       "$GEOM_PREFIX" ] ; then BINPATH="$BINPATH       $GEOM_PREFIX/bin" ; fi
if [   "$DATACELL_PREFIX" ] ; then BINPATH="$BINPATH   $DATACELL_PREFIX/bin" ; fi
if [    "$MONETDB_BUILD" ] ; then BINPATH="$BINPATH    $MONETDB_BUILD/src/tools    $MONETDB_BUILD/src/tools/.libs" ; fi
if [    "$CLIENTS_BUILD" ] ; then BINPATH="$BINPATH $CLIENTS_BUILD/src/mapiclient $CLIENTS_BUILD/src/mapiclient/.libs $CLIENTS_BUILD/src/python" ; fi
if [   "$MONETDB5_BUILD" ] ; then BINPATH="$BINPATH   $MONETDB5_BUILD/src/tools   $MONETDB5_BUILD/src/tools/.libs" ; fi
if [   "$TEMPLATE_BUILD" ] ; then BINPATH="$BINPATH   $TEMPLATE_BUILD/src/tools   $TEMPLATE_BUILD/src/tools/.libs" ; fi
if [        "$SQL_BUILD" ] ; then BINPATH="$BINPATH        $SQL_BUILD/src/tools        $SQL_BUILD/src/tools/.libs" ; fi
if [       "$GEOM_BUILD" ] ; then BINPATH="$BINPATH       $GEOM_BUILD/src/tools       $GEOM_BUILD/src/tools/.libs" ; fi
if [   "$DATACELL_BUILD" ] ; then BINPATH="$BINPATH   $DATACELL_BUILD/src/tools   $DATACELL_BUILD/src/tools/.libs" ; fi
for b in $BINPATH ; do
	files="$files `ls $b/* 2>/dev/null | egrep '/(lt-)?(mserver5|Mserver|mclient|mclient\.py|Mdiff|Mlog|Mtimeout|pf|actuator|sensor)$'`"
done

if type -p fuser >/dev/null
  then	FUSER='fuser'
  else	FUSER=''
fi

fuser_pids=''
fuser_procs=''
case `uname` in
  Linux)
	if [ "$FUSER" ] ; then
		fuser_pids="`fuser $files 2>&1`"
		pids1="`fuser $files | cut -d: -f2- | perl -pe 's|[a-z]||g'`"
	  else
		pids1=''
	fi
	pids1="`echo "$pids1" | perl -pe 's| *'"$CHILD"'||g'`"
	if [ "$PARENT" ] ; then
		pids1="`echo "$pids1" | perl -pe 's| *'"$PARENT"'||g'`"
	fi
	pids1="$cpids$pids1"
	PS="ps -f $pids1"
	if [ "$pids1" ] ; then
		fuser_procs="`$PS 2>&1`"
	fi
	if [ "$FUSER" ]
	  then	FUSER_V='fuser -v'
	  else	FUSER_V=''
	fi
	;;
  IRIX|IRIX64)
	if [ "$FUSER" ] ; then
		fuser_pids="`fuser -q $files 2>&1`"
		pids0="`fuser -q $files`"
	  else
		pids0=''
	fi
	pids0="`echo "$pids0" | perl -pe 's|,*'"$CHILD"'||g'`"
	if [ "$PARENT" ] ; then
		pids0="`echo "$pids0" | perl -pe 's|,*'"$PARENT"'||g'`"
	fi
	pids0="`echo $cpids | perl -pe 's| +|,|g'`${pids0#,}"
  	pids1="`echo $pids0 | perl -pe 's|,| |g'`"
	PS="ps -fp $pids0"
	if [ "$pids1" ] ; then
		fuser_procs="`$PS 2>&1`"
	fi
	if [ "$FUSER" ]
	  then	FUSER_V='fuser'
	  else	FUSER_V=''
	fi
	;;
  SunOS)
	if [ "$FUSER" ] ; then
		fuser_pids="`fuser $files 2>&1`"
		pids1="`fuser $files 2>/dev/null`"
	  else
		pids1=''
	fi
	pids1="`echo "$pids1" | perl -pe 's| *'"$CHILD"'||g'`"
	if [ "$PARENT" ] ; then
		pids1="`echo "$pids1" | perl -pe 's| *'"$PARENT"'||g'`"
	fi
	pids1="$cpids$pids1"
	pids0="`echo $pids1 | perl -pe 's| +|,|g'`"
	PS="ps -fp $pids0"
	if [ "$pids1" ] ; then
		fuser_procs="`$PS 2>&1`"
	fi
	if [ "$FUSER" ]
	  then	FUSER_V='fuser'
	  else	FUSER_V=''
	fi
	;;
# CYGWIN32_NT)
#	;;
esac

if [ "$pids1" ] ; then
	LOG1 "BEGIN"
	pids2="(`echo $pids1 | perl -pe 's/ +/|/g'`)"
	echo CONCURRENT=$CONCURRENT	>&2
	echo LOCKFILE=$LOCKFILE	>&2
	echo PARENT=$PARENT	>&2
	echo CHILD=$CHILD	>&2
	echo BINPATH=$BINPATH	>&2
	echo files=$files	>&2
	echo cpids=$cpids	>&2
	echo pids1=$pids1	>&2
	echo pids2=$pids2	>&2
	echo "fuser_pids=$fuser_pids"	>&2
	echo "fuser_procs=$fuser_procs"	>&2
	LOG1x	$PS
	if [ "$FUSER_V" ] ; then
		LOG1x	$FUSER_V $files
	fi
	#LOG1x	fuser -k $files
	LOG1x	kill     $pids1
	if [ "$cpids" ] ; then
		sleep 3
		LOG1x   kill $cpids
	fi
	sleep 4
	if [ "`( ps -ef | egrep -w "$pids2" | egrep -v egrep ) 2>/dev/null`" ] ; then
		LOG1x	kill -9 $pids1
	fi
	LOG1 "END"
fi

rm -f $LOCKFILE
