#!/usr/bin/env bash
#clear
#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x

#echo "starting: $0 <LOG_LEVEL=$1>"

currentUser.basicInfo()     # <nickname> <name> <?birthday> <role> # Asking for the basic info of the current user to print to a file # an example
{
 local nickname="$1"
 if [ -n "$nickname" ]; then
   shift
   create.result 0 "nickname is set to $nickname"
 else
  create.result 1 "nickname missing. Using: $nickname"
  echo “Nickname has to be given”
  return $(result)
 fi
 info.log "nickname is set to $nickname"

 local name="$1"
 if [ -n "$name" ]; then
   shift
   create.result 0 "name is set to $name"
 else
  create.result 1 "name missing. Using: $name"
  echo “name has to be given”
  return $(result)
 fi
 info.log "name is set to $name"

 local birthday="$1"
 if [ -n "$birthday" ]; then
    if [[ ${#birthday} == 10 ]] &&
     [[ ${birthday:0:2} -gt 0 && ${birthday:0:2} -le 31 ]] &&
       [[ ${birthday:3:2} -gt 0 && ${birthday:3:2} -le 31 ]] &&
        [[ ${birthday:6:4} -gt 1900 ]] &&
          [[ ${birthday:0:2} -le 12 || ${birthday:3:2} -le 12 ]]; then
            shift
            create.result 0 "birthday is set to $birthday"
            info.log "birthday is set to $birthday"
    else
      create.result 1 "Birthday does not have a valid format. Using: $birthday"
      echo “$birthday not a valid date format. Try dd-mm-yyyy or mm-dd-yyyy.”
      return $(result)
    fi
 else
  create.result 1 "role missing. Using: $1"
  echo “role has to be given”
  return $(result)
 fi

 if [[ "$birthday" == "$1" ]]; then
  unset birthday
 fi

 local role="$1"
 if [ -n "$role" ]; then
   shift
   create.result 0 "role is set to $role"
 else
  create.result 1 "role missing. Using: $role"
  echo “role has to be given”
  return $(result)
 fi
 info.log "role is set to $role"

  echo “Nickname: $nickname” > ~/userBasicInfo.txt
  echo “Name: $name” >> ~/userBasicInfo.txt
  echo “Birthday: $birthday” >> ~/userBasicInfo.txt
  echo “Role: $role” >> ~/userBasicInfo.txt

  mustPara=(“nickname” “name” “role”)
  optionalPara=("birthday")
  local paraCount=0

  while IFS= read -r line
  do
    IFS=' '
    read -ra parts <<< "$line"
    local label="${parts[0]}"
    label="${prt1/:/}" # remove last character
    label="${prt1,,}" # to lowercase
    if [[ " ${mustPara[*]} " =~ "$label" && -n "${parts[1]}"  ]]; then
      paraCount=$paraCount+1
    elif [[ " ${optionalPara[*]} " =~ "$label" ]]; then
      paraCount=$paraCount+1
    fi
  done< ~/userBasicInfo.txt

  local totalPara=$((${#mustPara[@]} + ${#optionalPara[@]}))

  if [[ $paraCount -eq $totalPara ]]; then
    echo "File userBasicInfo.txt has been written correctly"
	  create.result 0 "File userBasicInfo.txt has been written correctly"
  else
    echo "File userBasicInfo.txt contain errors or has not been written"
    create.result 1 "File userBasicInfo.txt contain errors or has not been written"
  fi
  
 return $(result)
}

### new.method

currentUser.usage()
{
  local this=${0##*/}
  echo "You started" 
  echo "$0

  Usage:
  $this: command   Parameter and Description"
  this.help
  echo "
  
  Examples
    $this v
    $this init
    $this basicInfo Billy Bob 12-31-1999 user
    ----------
  "
}

currentUser.start()
{
  #echo "sourcing init"
  source this

  # if [ -z "$1" ]; then
  #   status.discover "$@"
  #   return 0
  # fi

  this.start "$@"
}

currentUser.start "$@"

