#!/bin/sh
#
# viewlog
#
# Shows the contents of the files in the directory specified
# on the command line.
#
# Copyright (C) 2000 Felipe Eduardo Sanchez Diaz Duran
# This software is released under the terms of
# the GNU  General Public License
#

WD=`pwd`
DEFAULT_LOG_DIR="/var/log/startup"

if ! [ "$1" ]; then
  LOG_DIR=$DEFAULT_LOG_DIR
else
  LOG_DIR=$1
fi

until [ $? -gt 0 ]; do

   # Hacemos que LOG_DIR diga el nombre absoluto del directorio en 
   # lugar de algo como /var/log/startup/../../..
   cd $LOG_DIR
   LOG_DIR=`pwd`
   cd $WD

   count=0
   echo > /tmp/viewlog.list.$$
   for file in ${LOG_DIR}/{.*,*}; do 
      count=`expr $count + 1`
      if [ -f $file ]; then
   #      FILETYPE=`file $file |cut -f2 -d:|awk '{ printf $1" "$2" "$3; }'| tr ',' ' '`
         echo "`basename $file` Archivo" >> /tmp/viewlog.list.$$
      fi
      [ -d $file ] && echo "`basename $file` <DIR>" >> /tmp/viewlog.list.$$
   done
   dialog  --title "Viewlog" --menu "Estos son los archivos en el directorio $LOG_DIR" 17 70 7 `cat /tmp/viewlog.list.$$`  "-" "-----" "S" "Salir" 2> /tmp/viewlog.file.$$
   RET=$?
   CHOICE=`cat /tmp/viewlog.file.$$`
   FILE=${LOG_DIR}/${CHOICE}
   
   if [ -d "$FILE" ]; then
      LOG_DIR=$FILE
   else
      if [ -s "$FILE" ]; then
         if gzip -t $FILE &> /dev/null; then
            zcat $FILE > /tmp/viewlog.showfile.$$
            SHOWFILE=/tmp/viewlog.showfile.$$
         else
            SHOWFILE=$FILE
         fi
         col < $SHOWFILE | linecut > /tmp/viewlog.t.$$
         dialog --title "<`basename $FILE`> -- Fue modificado en `ls -l $FILE | awk '{ print $6 " " $7 " " $8; }'`" --textbox /tmp/viewlog.t.$$  0 0
      else
         if [ -f "$FILE" ]; then dialog --msgbox "El archivo $FILE esta vacio." 0 0; fi
      fi
   fi
   if [ "$CHOICE" = "S" -o $RET -gt 0 ]; then false; fi
   
done
rm -f /tmp/viewlog*.$$ 
clear
