#!/bin/bash

#BUSCA=$1

FUENTEBIN=$1
FUENTELIB=$2
BORRA=$3
FUENTEPM=$4
PACK=$5

SALIDA=

# FUENTE DEBE SER ABSOLUTO

#echo PATH $PATH

NPATH=
IFS=':'
  for DIR in $PATH; do
     if test  x"$DIR" != x/usr/bin ; then
	 NPATH="$NPATH $DIR"
	 fi
  done;
IFS=' '
NPATH="$NPATH /home/*/bin"
#echo NPATH $NPATH
mkdir -p /tmp/${PACK}moved/man/man1

# ojo puede haber cosas en man y no en fuente bin!
# pero como no pasamaos fuenteman pueden no verse y no se borran
# raro pq se instalan juntas

for FILE in $FUENTEBIN ; do
    FILEP=`echo -n $FILE|sed -e  "s/$FUENTE\///" ` ;
    ESSH=`echo $FILEP | grep '\.sh' | sed -e 's/\.sh$//'` ;
    ESPL=`echo $FILEP | grep '\.pl' | sed -e 's/\.pl$//'` ;
    DESTINO=$FILE; 
	    if test x"$ESSH" != x ; then 
	       DESTINO=$ESSH ;
            elif test x"$ESPL" != x ; then 
   	       DESTINO=$ESPL ;
            fi 
    for DIR in $NPATH ; do
     if test  -e $DIR/$DESTINO -o -e /usr/local/man/man1/$DESTINO.1.gz -o -e /usr/local/share/man/man1/$DESTINO.1.gz ; then         
	 if test x"$BORRA" = xBORRA ; then
	     mkdir -p /tmp/${PACK}moved/$DIR
	     mv -f $DIR/$DESTINO /tmp/${PACK}moved/$DIR
	     mv -f /usr/local/man/man1/$DESTINO.1.gz /tmp/${PACK}moved/man/man1
     	     mv -f /usr/local/share/man/man1/$DESTINO.1.gz /tmp/${PACK}moved/man/man1
	     #echo  "MOVIO $DIR/$DESTINO"
	 fi
	     if test x"$SALIDA" = x ; then
		   SALIDA="$DIR/$DESTINO"
		 else
		   SALIDA="${SALIDA}; $DIR/$DESTINO"
		   fi

     
     fi
    done;
done;
#LIB=sceptre.o

#echo FL: "$FUENTELIB"
for DESTINO in $FUENTELIB ; do
    for DIR in /usr/local/lib /home/*/lib ; do
	#echo LIB $DIR/$DESTINO	
     if test  -e "$DIR/$DESTINO" ; then
         #echo EEE $DIR/$DESTINO
   	 if test x"$BORRA" = xBORRA ; then
	     mkdir -p /tmp/${PACK}moved/$DIR
	     mv -f  $DIR/$DESTINO /tmp/${PACK}moved/$DIR
	     #echo  "MOVIO $DIR/$DESTINO"
	     #	 else
	     fi
  	     if test x"$SALIDA" = x ; then
 		     SALIDA="$DIR/$DESTINO"
	     else
		     SALIDA="${SALIDA}; $DIR/$DESTINO"
		     fi          
    fi
done
done
#
#findbinlib "" "" "" "`ls */* | tr "\n" " "`"
PMS=`perl -e 'print "@INC"'`;
#  echo PMS $PMS 	
#cd lib; FUENTPM=`ls */*` 
#echo "$FUENTEPM";
for DIR in $PMS ; do 
    if [[ x"$DIR" == x/usr/local/* ]] ; then 
     for MOD in $FUENTEPM ; do
      #echo BBB	$DIR/$MOD
      if test -e "$DIR/$MOD"  ; then 
	  #echo  "$DIR/$MOD"
	  DESTINO="$DIR/$MOD"
	  if test x"$SALIDA" = x ; then
 		     SALIDA="$DESTINO"
	  else
		     SALIDA="${SALIDA}; $DESTINO"
		     fi
          fi;
      done; 
     fi;
    done; 



# solo para dinamicas
#LIBS=`ldconfig -p | grep $LIBB`

#ls /home/dsa/lib


echo $SALIDA


#
