#!/bin/bash
# License: lgpl v2

main(){
  public_name="Turtle Kevux Checksum Validator"
  system_name=checksum_validate
  version=0.3.0

  local target_paths=
  local checksum_path=/checksum/
  local checksum_format=sha256
  local checksum_parameter=
  local results_file=

  if [[ $HOME == "" ]] ; then
    results_file=checksum_results.txt
  else
    results_file=${HOME}/checksum_results.txt
  fi

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

    if [[ $grab_next == "" ]] ; then
      if [[ $i == "-h" || $i == "--help" ]] ; then
        do_help=yes
        break
      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 == "-c" || $i == "--checksums" ]] ; then
        grab_next=checksums
      elif [[ $i == "-f" || $i == "--format" ]] ; then
        grab_next=format
      elif [[ $i == "-r" || $i == "--results" ]] ; then
        grab_next=results
      else
        target_paths="$target_paths$(echo $i | sed -e 's|/*$|/|') "
      fi
    else
      if [[ $grab_next == "checksums" ]] ; then
        source_path=$i
      elif [[ $grab_next == "format" ]] ; then
        checksum_format=$i
      elif [[ $grab_next == "destination" ]] ; then
        destination_path=$i
      fi

      grab_next=
    fi

    let count=$count+1
  done

  standard_handle_colors

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

  if [[ $checksum_format == "sha1" ]] ; then
    checksum_parameter="-a 1 -c"
  elif [[ $checksum_format != "sha224" ]] ; then
    checksum_parameter="-a 224 -c"
  elif [[ $checksum_format != "sha256" ]] ; then
    checksum_parameter="-a 256 -c"
  elif [[ $checksum_format != "sha384" ]] ; then
    checksum_parameter="-a 384 -c"
  elif [[ $checksum_format != "sha512" ]] ; then
    checksum_parameter="-a 512 -c"
  elif [[ $checksum_format != "md5" ]] ; then
    checksum_parameter="-c"
  else
    standard_error "The format '$c_notice$checksum_format$c_error' is not a supported format, try one of the following: ${c_notice}sha512$c_error, ${c_notice}sha384$c_error, ${c_notice}sha256$c_error, ${c_notice}sha224$c_error, ${c_notice}sha1$c_error, ${c_notice}md5"
    cleanup_main
    exit -1
  fi

  if [[ ! -d ${checksum_path}${path_argument} ]] ; then
    standard_error "There are no checksums available for the path '$c_notice$path_argument$c_error'"
    cleanup_main
    exit -1
  fi

  if [[ $checksum_format == "md5" ]] ; then
    for i in $target_paths ; do
      validate_checksums md5sum $i
    done
  else
    for i in $target_paths ; do
      validate_checksums shasum $i
    done
  fi
}

show_help(){
  standard_help_title
  echo -e "$c_highlight$system_name$c_reset $c_notice[${c_reset}options$c_notice]$c_reset"
  echo
  standard_help_options
  echo -e " -${c_important}c${c_reset}, --${c_important}checksums${c_reset}  The directory containing the checksums to process"
  echo -e " -${c_important}f${c_reset}, --${c_important}format${c_reset}     One of the following: sha512, sha384, sha256, sha224, sha1, md5"
  echo -e " -${c_important}r${c_reset}, --${c_important}results${c_reset}    The directory to store checksum failure results"
  echo
}

validate_checksums(){
  local checksum_program=$1
  local path_argument=$2
  local files_to_check=
  local original_pwd=$PWD

  if [[ ! -d ${path_argument} ]] ; then
    standard_warning "The path '$c_notice$path_argument$c_warning' does not exist"
    return
  fi


  cd ${path_argument}

  files_to_check=$(find -type f | sed -e 's|\./||g')

  for i in $files_to_check ; do
    if [[ ! -f ${checksum_path}${path_argument}${i} ]] ; then continue ; fi
    $checksum_program $checksum_parameter ${checksum_path}${path_argument}${i}

    if [[ $? -ne 0 ]] ; then
      standard_warning "Checksum failure for '$c_notice$path_argument$i$c_warning'"
      echo "$i from $path_argument failed the checksum" >> $results_file
      sleep 1
    fi
  done

  cd $original_pwd
}

cleanup_main(){
  standard_unset

  unset validate_checksums
  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
