#!/bin/sh
#Find file in root aufs
#VERSION 4.4

if [ -f $(dirname $0)/pfs ] ;  then
    . $(dirname $0)/pfs
else 
    . $(which pfs) || exit 13 
fi    

TMPFS=tmpfs
usage () {
	echo -e "$(basename $0) util, find file (\$1) in modules mounted to root aufs  and in $changesDir\n" 
	echo "Usage:" 
	echo "	$(basename $0)  filename        - find \"filename\"  (human readable format)"
	echo "	$(basename $0)  filename --raw  - find \"filename\"  (output separated by spaces)"
	echo "	$(basename $0)  filename --raw  '"'$n $module'"'   - return only needed values"
	echo "available values:"
	echo '  $n - layer number'
	echo '  $module - modulename'
    echo '  $submodule - submodule name (one of module sources)'
    echo '  $path - path to file'
    echo '  $bundle - bundle (module mount point)'
    
}

header () {
echo "Find file in root aufs"
echo ""
printf %3s "N "; printf %-50s "Module " ; printf %-30s "SubModule" ; printf  "PATH\n"
}

listing (){
[ -d $p ] || exit 0
f="$(basename "$1")"
list1=$(aufs-n --raw '$bundle' |sed 1d |while read bdl ; do   find "$bdl"  \( -type f -o -type l \) -name "$f" |sed 's#^'$(dirname $bdl)/'## ; s/'$f'$//' ; done)
list2=$(find "$changesDir" \( -type f -o -type l \) -name "$f" |sed 's#^'$changesDir'#$TMPFS/# ; s/'$f'$//')
list="$list1 $list2"
aufs-n --raw '$n $bundle $dname_source $bname_source' | while read a; do
	for b in $list ; do
		if [ "$(echo $a |awk '{print $NF}')" = "$(echo $b |awk -F/ '{print $1}' )" ]  ; then
			n="$(echo $a |awk '{print $1}')"
			module="$(echo $a |awk '{print $3 "/" $4}')" 
			bundle=$(echo $a | awk '{print $2}')
			path="$(echo $b | sed "s:"$(echo $a |awk '{print $NF}')"::")"
			modPFSDIR=$bundle${PFSDIR}
			if [ -d $modPFSDIR ] ; then
				submodule=$(find $modPFSDIR   -mindepth 3  -type f -name 'pfs.files' | while read listfiles; do   
				cat "${listfiles}" | sed "s:^:$( dirname $(echo $listfiles |sed -e "s:${SYSMNT}.*${PFSDIR}/mount/::" -e 's:/submod/mount::g')) :"
				done | grep ${path}${findname}$ | cut -f1 -d' ' |sort |uniq |tr '\n' ' ' )
				for a in $submodule ; do  
						[ $(echo $submodule |sed 's/ /\n/g' | grep ^${a}  | wc -l) -eq 1 ]  && listing="$a $listing"
				done 
				submodule=$(echo $listing |sed 's/ /;/g')
			else
				submodule="is_not_contaner"
			fi
			if [ $human ] ; then 			
				printf %3s "$n "
				printf %-50s "$module" 
				printf %-30s "$submodule" 
				printf  "$path\n"
			else
				eval echo "$returnMe"
			fi
		fi
	done
done
}

case "$1" in 
  "") usage; exit 1;;
  "-"*[A-Za-z]*) usage; exit 1;;
  *) findname="$1";;
esac

if  echo $@ |grep -q "\-\-raw" ; then
	returnMe="$(echo $@ |sed 's/^.*\-*raw//')"
	[ "$returnMe" ] || returnMe='$n $bundle $module $submodule  $path'
fi

if [ ! "$2" ] ;then 
	human=yes
	header
	listing $1
	exit
elif [ "$2" = "--raw" ] ; then
	listing $1
	exit
else 
	usage
fi
exit 0
