#!/bin/bash
# License: lgpl v2

main(){
  public_name="Turtle Kevux TZO Compressor"
  system_name=tzo
  called_name=$(basename $0)
  version=0.3.0

  local directories=
  local compress_paramater=--lzop
  local compress_extension=.tzo

  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
      else
        directories="$directories$(echo $i | sed -e 's|/*$||') "
      fi
    else
      grab_next=
    fi

    let count=$count+1
  done

  # support multiple compressor formats based on this scripts name
  if [[ $called_name == "tbz" ]] ; then
    public_name="Turtle Kevux TBZ Compressor"
    system_name=tbz
    compress_paramater=--bzip2
    compress_extension=.tbz
  elif [[ $called_name == "tgz" ]] ; then
    public_name="Turtle Kevux TGZ Compressor"
    system_name=tgz
    compress_paramater=--gzip
    compress_extension=.tgz
  elif [[ $called_name == "tlz" ]] ; then
    public_name="Turtle Kevux TLZ Compressor"
    system_name=tlz
    compress_paramater=--lzip
    compress_extension=.tlz
  elif [[ $called_name == "txz" ]] ; then
    public_name="Turtle Kevux TXZ Compressor"
    system_name=txz
    compress_paramater=--xz
    compress_extension=.txz
  fi

  standard_handle_colors

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

  if [[ $directories != "" ]] ; then
    for i in $directories ; do
      echo -e "${c_title}Attempting to archive and compress: $c_notice$i$c_reset"

      if [[ ! -d $i ]] ; then
        standard_error "'$c_notice$i$c_error' is not a valid directory"
        continue;
      fi

      if [[ -f $i$compress_extension ]] ; then
        standard_warning "'$c_notice$i$compress_extension$c_warning' already exists, will not overwrite"
        continue;
      fi

      tar --numeric-owner -pc $i $compress_paramater -f $i$compress_extension

      if [[ $? -eq 0 ]] ; then
        echo -e "  ${c_highlight}Archived and Compressed: $c_notice$i$c_reset"
      else
        standard_error "Failed to archive and compress: $c_notice$i"
      fi
    done
  else
    standard_error "No directories were specified"
    cleanup_main
    exit -1
  fi
}

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}directory$c_notice>$c_reset"
  echo -e "  ${c_important}directory${c_reset}   Pathname of the directories to operate on."
  echo
  standard_help_options
}

cleanup_main(){
  standard_unset

  unset show_help
  unset main
  unset cleanup_main
}

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

# 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
