#!/bin/bash

# ql_dlm ... quick look of de-limited files
# ql_dlm [-d DFS] {-s} {-h} file.dlm (or pipe)
# -d .. delimtation character (e.g "," ["\t"]..)
# -s .. show keyword:value pairs on separate lines
# displays header and checks if file is homogeneous (NF=constant)

# LAST REVISION: 27 January 2021
#-----------------------------------------------------------------------
DFS="\t"; SHOW=; CHECK=1; HELP=;
#-----------------------------------------------------------------------
TF="OUT_ql_dlm.tmp"; TFh=${TF}h; TFd=${TF}d; TFt=${TF}t;
:> $TF; :> $TFh; :>$TFd; :>$TFt
trap "rm $TF $TFh $TFd $TFt" EXIT
#-----------------------------------------------------------------------

while getopts d:sh OPTVAL
do
    case $OPTVAL in
	d) DFS=$OPTARG;;
	s) SHOW=1;;
	h) HELP=1;;
	*) echo "ql_dlm -h for help"; exit -1;;
    esac
done
shift $((OPTIND-1))

if [ $HELP ]; then
    echo -e "\n\t ql_dlm [-d DFS] {-s} {-h} ql_dlm file.dlm  (or pipe)"
    echo -e '\t -d DFS .. the  delimiting character ",", [tab], ..'
    echo -e "\t -s .. show keyword:value on each line"
    echo -e "\t by default displays header and checks if file is homogeneous"
    exit;
fi

[ $SHOW ] && CHECK=;

#-----------------------------------------------------------------------
# check to see if data is being supplied by pipe
#-----------------------------------------------------------------------

case $# in
    0) if [ -p /dev/stdin ]; then
          set -- "/dev/stdin"                     #set $1=/dev/stdin
        else
          echo "no file given nor is there a trailing pipe"
          exit -1;
       fi
       cat $1 > $TF; IF=$TF;;
    1) IF=$1;;
    *) echo "ql_dlm: accpets only one input file"; exit -1;;
esac
#-----------------------------------------------------------------------
NREC=$(($(wc -l < $IF)-1))	#number of records


#-----------------------------------------------------------------------
if [ $CHECK ]; then 

#display header
  	head -1 $IF | tr "$DFS" '\n' | nl  | tee $TFh   
  	echo -n "number of records $NREC. File is  "
  	NFIELDS=$(echo $(wc -l < $TFh))		#strip out blank char!

#see if all data records have the same number of fields as header
  	sed 1d $IF | awk  -F"$DFS" 'NF!=nf{print "bad " NR ":" NF}' nf=$NFIELDS \
	 	| tee $TFd

  	NBAD=$(wc -l < $TFd | xargs)     #another way to strip blank char!
  	if [ $NBAD -eq 0 ]; then
	   echo "OK (homogeneous)"
        else 
           echo "NOT OK. bad lines:$NBAD"
        fi
fi

#-----------------------------------------------------------------------
if [ $SHOW ]; then
    head -1 $IF | tr "$DFS" '\n' > $TFh    #stash header
    sed 1d $IF > $TFt			  #data file
   
    touch $TFd

    cat $TFt | while read line
    do
      echo "$line" | tr "$DFS" '\n' > $TFd
      paste $TFh $TFd | column -t
      echo  
    done
fi