#!/bin/bash

# fffd
# Sat Dec 02 09:57:44 MST 2017 
# find files for date
# jorgericarte@asuni.com.mx
# Ultimo cambio: Sat 02 Dec 2017 01:20:09 PM MST

# set -x

lVER=0.1

lOPCIONES=" -type f"

if [ "$1" = "-h" ]; then
  echo "fffd $lVER"
  echo "Usa: $0 [/d*] -fi [\"2010-01-20 00:00:00\"] -ff [\"2010-03-20 23:59:59\"] [-d]"
  echo "Donde:"
  echo " /d* Trayectoria inicial desde el directorio raiz todos los archivos que empiezar con d"
  echo " -fi Fecha y hora inicial"
  echo " -ff Fecha y Hora final"
  echo " -d  Depurar"
  exit 1
fi

if [ $# -lt 5 ]; then
  echo "Se requieren al menos cinco parametros."
  echo "Usa: "
  echo "  fffd -h"
  exit 1
fi
 
if [ "$6" == "-d" ]; then
  echo "Depurar...."
  echo "Parametros "$#
fi

lOPCIONES=$1$lOPCIONES

if [ "$2" == "-fi" ]; then
  touch --date "$3" /tmp/inicio
  lOPCIONES="$lOPCIONES -newer /tmp/inicio "
fi

if [ "$4" == "-ff" ]; then
  touch --date "$5" /tmp/fin
  lOPCIONES="$lOPCIONES -not -newer /tmp/fin "
fi

lOPCIONES="$lOPCIONES -printf '%M\t%g\t%u\t%Am/%Ad/%AY_%AH:%AM:%AS\t%s\t%P\f\r'"

if [ "$6" == "-d" ]; then
  echo "find $lOPCIONES "
fi

find $lOPCIONES 
if [ "$6" == "-d" ]; then
  echo "Depurar...."
  ls -l /tmp
else
  rm /tmp/inicio
  rm /tmp/fin
fi
 
exit 0

