#!/bin/bash
# LKPLv1 - part of the FLL Project
# Copyright 2006 Kevin Day
# This program parses fss-0002 (List) based files
# This does not parse fss-0003 (Extened-List) based files
# This requires the following programs: bash, sed, more, grep
# This is an experimental version to look for problems and develop ideas or plans.

tkis_list(){

# The version of this program
local VERSION="0.2.2"

# Holds our previous line
local PREV=

# Holds the name or count to look for
# If both name and count are specified, the program will count to a certain list, and then begin to look for name
local NAME=
local COUNT=

# should we just print the listname instead of the contents?
local LIST=

# store the list of files to parse
local FILES=

# The specific line in a given list to print
local LINE=

# Should we not print and just count the size?
local SIZE=

# How many lists exist in this file.
# This overrides the count command
local TOTAL=

# allow for easy scripting
# list name will be saved in LIST_NAME
# list data will be saved in LIST_DATA
local PARAMETER=
LIST_NAME=
LIST_DATA=
LIST_SIZE=
LIST_TOTAL=

# COLORS..
# 00m = reset, 01m = bold, 31=red, 32=green, set all to '' for no color.
local BOLD="\\033[01m"
local RED="\\033[31m"
local BRED="\\033[01m\\033[1;31m"
local BYEL="\\033[01m\\033[1;33m"
local RESET="\\033[00m"
local i=

# Parse the args
for i in $* ; do
  if [[ $i == "-h" || $i == "--help" ]] ; then
    echo -e "${BOLD}list [options] <filenames>$RESET"
    echo -e "  ${RED}TODO:  Add more help information here$RESET"
    echo -e "  ${BOLD}options:$RESET"
    echo "    -n, --name          Find and print content from this list name"
    echo "    -c, --count         Find and print content from this list number"
    echo "    -f, --find          Find and print the name of the list if found"
    echo "    -l, --line          Print a specific line inside of the list"
    echo "    -s, --size          Print the total number of lines in the list"
    echo "    -t, --total         Print the total number of lists in this file"
    echo "    -v, --version       Print current version of this program"
    echo "    -p, --parameter     Store list name in LIST_NAME and list data in LIST_DATA"
    exit 0
  elif [[ $i == "-v" || $i == "--version" ]] ; then
    echo "$VERSION"
    exit 0
  elif [[ $i == "-n" || $i == "--name" ]] ; then
    PREV="-n"
  elif [[ $i == "-c" || $i == "--count" ]] ; then
    PREV="-c"
  elif [[ $i == "-f" || $i == "--find" ]] ; then
    LIST="-f"
  elif [[ $i == "-l" || $i == "--line" ]] ; then
    PREV=$i
  elif [[ $i == "-s" || $i == "--size" ]] ; then
    SIZE=$i
  elif [[ $i == "-t" || $i == "--total" ]] ; then
    TOTAL=$i
  elif [[ $i == "-p" || $i == "--parameter" ]] ; then
    PARAMETER="yes"
  elif [[ $PREV == "-n" ]] ; then
    NAME="$i:"
    PREV=
  elif [[ $PREV == "-c" ]] ; then
    COUNT=$i
    PREV=
  elif [[ $PREV == "-l" ]] ; then
    LINE=$i
    PREV=
  elif [ -f $i ] ; then
    FILES="$FILES$i "
  elif [[ $PREV == "" ]] ; then
    error "tkis_list(): $color_reset$color_notice$i$color_error is invalid, cannot be found, or cannot be accessed"
  fi
done

# Is there a file to search through?
if [[ $FILES == "" ]] ; then
  error "tkis_list(): Missing filename argument, parameters given are $color_reset$color_notice$*$color_error"
fi

# Begin the program 
for i in $FILES ; do
  local LENGTH=$(sed -n "$=" $i)
  local CONTINUE=

  if [[ $LENGTH == "" ]] ; then
    error "tkis_list(): $color_reset$color_notice\$(sed -n \"$=\")$color_error failed for some reason"
  fi
    
  # NOTE: Do not do anything for empty files
  if [ $LENGTH -gt 0 ] ; then
    local current=1
    local listcount=0
    local linecount=0

    # continue until EOF or end of list
    while [[ $current -le $LENGTH && $CONTINUE != "no" ]] ; do
      line=$(more +$current $i | grep -s -m 1 '')
      name=$(echo $line | grep ':$')

      # increment by listnames
      if [[ $name != "" ]] ; then
        let listcount=$listcount+1
      fi
 
      # if we aren't just counting lists then lets try to get more data
      if [[ $TOTAL == "" ]] ; then
        if [[ $COUNT == "" || $listcount -ge $COUNT ]] ; then 
          if [[ $name != "" && $CONTINUE != "yes" ]] ; then
            if [[ $NAME == "" || $NAME == $name ]] ; then
              if [[ $LIST == "-f" ]] ; then
                if [[ $PARAMETER == "yes" ]] ; then
                  LIST_NAME=$(echo $NAME | sed -e 's|:$||')
                else
                  echo $NAME | sed -e 's|:$||'
                fi
                CONTINUE="no"
              else
                CONTINUE="yes"
              fi
            fi
          elif [[ $name != "" && $CONTINUE == "yes" ]] ; then
            CONTINUE="no"
          elif [[ $CONTINUE == "yes" ]] ; then
            if [[ $LINE != "" || $SIZE == "-s" ]] ; then
              if [[ $SIZE != '-s' && $linecount == $LINE ]]; then
                if [[ $PARAMETER == "yes" ]] ; then
                  LIST_DATA=$line
                else
                  echo $line
                fi
                CONTINUE="no"
              else
                let linecount=$linecount+1
              fi
            else
              if [[ $PARAMETER == "yes" ]] ; then
                if [[ $line != "" && $(echo $line | sed -e 's|[[:space:]]||g') != "" ]] ; then 
                  LIST_DATA=$LIST_DATA$line
                fi
              else
                echo $line
              fi
            fi 
          fi
        fi
      fi

      let current=$current+1
    done

    # Print number of lines in the list
    if [[ $SIZE != "" ]] ; then
      if [[ $PARAMETER == "yes" ]] ; then
        LIST_SIZE=$linecount
      else
        echo $linecount
      fi
    fi

    # Print number of lists in the file
    if [[ $TOTAL != "" ]] ; then
      if [[ $PARAMETER == "yes" ]] ; then
        LIST_TOTAL=$listcount
      else
        echo $listcount
      fi
    fi
  fi
done
}

# execute the program
tkis_list $*
