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

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

  Usage:
  $this: command   description and Parameter

      usage     prints this dialog while it will print the status when there are no parameters          
      v         print version information
      init      initializes ...nothing yet

      alias.create            <path> <file>
      alias.show.target       <alias>
      alias.to.link           <alias>   creates a unix symbolic link for the alias

      spotlight                use completion
  
  Examples
    $this v
    $this init
  "
}

osx.file.metadata.list() {
  mdls "$*"
}

osx.timemachine.settings.install() {
  brew install --cask timemachineeditor
} 

osx.timemachine.settings() {
  if ! [ -x "$(command -v tmectl)" ]; then
        osx.timemachine.settings.install
  fi
  tmectl "$@"
} 

osx.timemachine.list() {
 cat /System/Library/LaunchDaemons/com.apple.backupd-helper.plist
} 

osx.brew.fix() {
  sudo chown -R $(whoami) /usr/local/var/homebrew
  sudo chown -R $(whoami) /usr/local/Homebrew
}

osx.spotlight() {
  #https://metaredux.com/posts/2019/12/22/mdfind.html
  mdfind "$*"
}

osx.spotlight.completion() {
  echo -e "byName \\nbyKind \\n-onlyin \\nkMDItemKind='Alias' \\nbySize " | grep "^$1"
}

osx.spotlight.by.size() {  # <?mathExpression:1024*1024*1024>  # size larger than the result of <mathExpression>
  local mathExpression=$1 
  shift
  if [ -z "$mathExpression" ]; then
    silent.log "no mathExpression. fixing it to "
    mathExpression="1024*1024*1024" # 1 GB
    silent.log "no mathExpression. fixing it to: $mathExpression    (hardcoded 1 GB)"
  fi
  console.log "mdfilnd \"kMDItemFSSize >$[$mathExpression]\""
  mdfind "kMDItemFSSize >$[$mathExpression]"
  #return "$1"
}

osx.alias.find.all() {
  mdfind kMDItemKind="Alias"
}

osx.spotlight.by.name() { # <?name>  # files containing <name>
  mdfind -name "$*"
}

osx.spotlight.by.name.completion() {
  compgen -f | grep "^$1"
}

osx.spotlight.bykind() { # <?kind>  # files containing <kind> as extension
  mdfind  kind:"$1"
}

osx.alias.create() {
  local path=$1
  shift
  local file=$1
  shift

  alisma -a $path $file
  RETURN=$1
}

osx.alias.create.completion() {
  compgen -d "$1" | grep "^$1" | printf '%s/' "$1"
}

osx.alias.show.target() {
  local alias=$1
  shift

  create.result 0 "$(alisma -p \"$alias\" \"$file\")" "$1"
  return $(result)
}

osx.show.target.completion() {
  compgen -f "$1" | grep "^$1"
}

osx.alias.to.link() {
  target=$(alisma -p "$1")
  echo "$target"
  if [ ! -L "$1.link" ]; then
      ln -s "$target" "$1.link"
  else
      echo "$1.link   already exists."
      ls -alhF "$1.link"
  fi
}

osx.alias.collect.all.here() {
  if [ -z "$1" ]; then
    echo Usage: $0 file loopcommand
    echo "${0##*/} aliases.list.txt \"echo echo '\" \"' linkname\""
    exit 0
  fi


  file=$1
  loopcommand=$2
  #seperator=\n
  seperator=.

  if [ -n "$loopcommand" ]; then
    debug.log "loopcommand is: -$2-"
      shift
  else
    loopcommand="echo "
    debug.log "command is echo...
    "
  fi
  if [ -n "$file" ]; then
      shift
      
      while read line; do
        target=$(alisma -p "$line")
        if [ ! "$target" == "-1" ]; then
          ln -s "$target" "./${target##*/}".link
        else
          echo "  error in: $line"
          if [ -e brokenAlias.list ]; then
              touch brokenAlias.list
          fi
          echo $line >>brokenAlias.list
        fi
      done <$file
  else
    read -p "file? " file
    $0 $file $loopcommand 
  fi
}

osx.install.homebrew() {
  if ! [ -x "$(command -v brew)" ]; then
    warn.log "installing homebrew"
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  else
    important.log "homebrew already installed"
    which brew
  fi
}

osx.install.bash5() {
  osx.install.homebrew
  brew install bash
  sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'
  #chsh -s /usr/local/bin/bash
}




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

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

  this.start "$@"
}

osx.start "$@"

