#!/bin/bash
# License: lgpl v2

main(){
  public_name="Turtle Kevux Setup SSH Host Keys"
  system_name=setup_ssh_host_keys
  version=0.3.0

  local target_path=/home/targets/ssh/settings/
  local group=t_ssh

  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 == "-g" || $i == "--group" ]] ; then
        grab_next=group
      fi
    else
      if [[ $grab_next == "group" ]] ; then
        group=$i
      fi

      grab_next=
    fi

    let count=$count+1
  done

  standard_handle_colors

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

  if [[ -d $target_path ]] ; then
    echo -e "${c_highlight}Generating ${c_notice}RSA-1$c_highlight Host Key$c_reset"
    ssh-keygen -t rsa1 -f ${target_path}ssh_host_key -N ""
    echo
    echo -e "${c_highlight}Generating ${c_notice}DSA$c_highlight Host Key$c_reset"
    ssh-keygen -t dsa -f ${target_path}ssh_host_dsa_key -N ""
    echo
    echo -e "${c_highlight}Generating ${c_notice}RSA$c_highlight Host Key$c_reset"
    ssh-keygen -t rsa -f ${target_path}ssh_host_rsa_key -N ""
    echo
    echo -e "${c_highlight}Generating ${c_notice}ECDSA$c_highlight Host Key$c_reset"
    ssh-keygen -t ecdsa -f ${target_path}ssh_host_ecdsa_key -N ""

    chown 0:$group ${target_path}ssh_host_*key*
    chmod o-r ${target_path}ssh_host_*key*
  else
    standard_error "'$c_notice$target_path$c_error' is not a directory or does not exist"
    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"
  echo
  standard_help_options
  echo -e " -${c_important}g${c_reset}, --${c_important}group$c_reset  Specify the ssh group name or group id"
  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
