#!/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>"

### new.method

myId.create() # <sshConfigName> # lists the ids you have on this user
{
  echo create
  create.result 0 "That myID create worked well"
  export RESULT
}

myId.get() # <sshConfigName:marcel.iMac> # lists the ids you have on this user
{
  echo create
  create.result 0 "That myID create worked well"
  export RESULT
}


myId.list() # <?id> # lists the ids you have on this user
{
  tree -L 1 ~/.ssh/ids
}

myId.noop() #  # just a test TDODO to be removed
{
  important.log "myId.noop went well"
}


myId.parameter.completion.id() {
  ls ~/.ssh/ids
}

myId.create.github.deploy.key() # <sshConfigName> <githubUrl> <idName> # 
{
  local sshConfigName="$1"
  if [ -z "$sshConfigName" ]; then
    error.log "No sshConfigName provided."
    return 1
  else
    shift
  fi

  local githubUrl="$1"
  if [ -z "$githubUrl" ]; then
    error.log "No githubUrl provided."
    return 1
  else
    shift
  fi

  local idName="$1"
  if [ -z "$idName" ]; then
    error.log "No idName provided."
    return 1
  else
    shift
  fi

  source line

  local gitHubFolder="$(user get basehome)/shared/dev/Workspaces/GitHub"
  mkdir -p "$gitHubFolder"
  cd "$gitHubFolder"

  ossh id.create "$idName.$sshConfigName"
  ossh config.create "$sshConfigName.$idName" "$githubUrl" "$idName.$sshConfigName"
  ossh get.public.id "$idName.$sshConfigName"

  local project="$(echo $githubUrl | line.split : | line.unquote | line.select 2)"
  echo "check out with
  
  git clone \"$sshConfigName.$idName:$project\""

}

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

  Usage:
  $this: command   Parameter and Description"
  this.help
  echo "
  
  Examples
    $this v
    $this init
    ----------
  "
}

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

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

  this.start "$@"
}

myId.start "$@"

