#!/bin/bash
# License: lgpl v2

main(){
  public_name="Turtle Kevux Version Viewer"
  system_name=version
  version=0.3.0

  local color_command=always
  local results_only=
  local program_names=
  local parameter_operation=
  local reverse=
  local version_file=/etc/turtle-versions
  local approximate=

  while [[ $count -lt $main_size ]] ; do
    i=${main_input[$count]}

    if [[ $grab_next == "" ]] ; then
      if [[ $i == "-h" || $i == "--help" ]] ; then
        do_help=yes
      elif [[ $i == "+n" || $i == "++no_color" ]] ; then
        do_color=none
      elif [[ $i == "+l" || $i == "++light" ]] ; then
        do_color=light
      elif [[ $i == "+v" || $i == "++version" ]] ; then
        echo $version
        return
      elif [[ $i == "-f" || $i == "--file" ]] ; then
        grab_next=file
      elif [[ $i == "-r" || $i == "--reverse" ]] ; then
        reverse="reverse"
      elif [[ $i == "-e" || $i == "--either" ]] ; then
        reverse="either"
      elif [[ $i == "-o" || $i == "--only_matching" ]] ; then
        results_only="yes"
      elif [[ $i == "-a" || $i == "--approximate" ]] ; then
        approximate="[[:alnum:],+._-]*"
      else
        program_names="$program_names$(echo $i | sed -e 's|/*$||') "
      fi
    else
      if [[ $grab_next == "file" ]] ; then
        version_file=$i
      fi

      grab_next=
    fi

    let count=$count+1
  done

  standard_handle_colors

  if [[ $do_help != "" ]] ; then
    show_help
    return
  fi

  for i in $program_names ; do
    if [[ $reverse == "" || $reverse == "either" ]] ; then
      if [[ $results_only == "" ]] ; then
        grep -h --color=$color_command "^$approximate$i$approximate[[:space:]]" $version_file
      else
        grep -h --color=never "^$approximate$i$approximate[[:space:]]" $version_file | sed -e "s|^$approximate$i$approximate[[:space:]]||"
      fi
    fi

    if [[ $reverse == "reverse" || $reverse == "either" ]] ; then
      if [[ $results_only == "" ]] ; then
        grep -h --color=$color_command "[[:space:]]$approximate$i$approximate$" $version_file
      else
        grep -h --color=never "[[:space:]]$approximate$i$approximate$" $version_file | sed -e "s|[[:space:]]$approximate$i$approximate$||"
      fi
    fi
  done
}

show_help(){
  standard_help_title
  echo -e "${c_highlight}$system_name$c_reset $c_notice[${c_reset}options$c_notice]$c_reset $c_notice<${c_reset}package name$c_notice>$c_reset"
  echo -e " This is will return the version number of one or more packages installed on this system"
  echo
  standard_help_options
  echo -e " -${c_important}o${c_reset}, --${c_important}only_matching${c_reset}  Show only the found version number(s)"
  echo -e " -${c_important}f${c_reset}, --${c_important}file${c_reset}           Read version information from the given file"
  echo -e " -${c_important}r${c_reset}, --${c_important}reverse${c_reset}        Look up program by version number instead of version by name"
  echo -e " -${c_important}e${c_reset}, --${c_important}either${c_reset}         Look up by program name or by version number"
  echo -e " -${c_important}a${c_reset}, --${c_important}approximate${c_reset}    Include inexact matches in search"
  echo
}

cleanup_main(){
  standard_unset

  unset show_help
  unset main
  unset cleanup_main
}

PATH=/tkis//tools/lib/scripts/turtle_kevux/:$PATH
source $(type -p turtle_kevux_scripts-standard)

# populate main_input variable
main_input=
main_size=$#
c1=0
c2=

while [[ $c1 -lt $main_size ]] ; do
  let c2=$c1+1
  main_input[$c1]=${!c2}
  let c1=$c1+1
done

unset c1 c2

# execute the program
standard_main_execute=main
standard_main
cleanup_main
unset main_input main_size
