# (new)user friendlyness
alias vi='echo -e "HINT: type :q (and press enter) to quit\n[press a key]";read;vi'; 
alias mg='echo -e "HINT: type Ctrl+c Ctrl-x to quit\n[press a key]";read;mg'; 
alias nano='echo -e "HINT: type Ctrl+x to quit\n[press a key]"       ;read;nano'; 

# save space in the iso image
alias curl="wget -q -O - "
alias make="mim"

# minimalist autoenv version [https://github.com/hyperupcall/autoenv]
cd(){
  dir_cur=$(pwd)
  ignore=0
  test "${dir_cur:0:4}" = /mnt && ignore=1
  test "${1:0:4}"       = /mnt && ignore=1 
  test "$ignore" = 1 && { command cd ${1:+"$@"}; return 1; }
  test -f $(pwd)/.env.leave && source $(pwd)/.env.leave     # cleanup environment
  command cd ${1:+"$@"} && {
    hook cd "$@"
    test -f $(pwd)/.env && { # setup environment / run scripts e.g.
      test "$(cat ~/.config/autoenv/prompt_cd)" = 1 && {
        if "$(confirm ".env detected, execute?")" = false; then return 0; fi 
      }
      source $(pwd)/.env 
    }
    ls                                          # list directories
  }
}

test -f /root/.boot || {
 
  touch /root/.boot # make sure we run below once

  # add menu items
  find /root/.config/launcher -type f | while read item; do 
    js '
      let term = document.querySelector("[isoterminal]").components.isoterminal.term
      launcher.register({
        name: "'$(basename $item)'",
        cb: () => term.exec("'$item'")
      })
      return "'$item'"
    '
  done > /tmp/log
  
  # get latest xrfragments to support interactive models
  require https://xrfragment.org/dist/xrfragment.aframe.js 
  
  # start index.js if /dev/browser/js filesize is not great than 0
  if [ ! -s /dev/browser/js ]; then
    chmod +x /mnt/root/index.js && /mnt/root/index.js | grep -v undefined
  fi
  
  # start index.html if /dev/browser/html filesize is not great than 0
  if [ ! -s /dev/browser/html ]; then
    chmod +x /mnt/root/index.html && /mnt/root/index.html
  fi
  
  # uncomment to set location (3D scene file [with XR Fragments] ) 
  #require https://xrfragment.org/index.glb 
  #require https://coderofsalvation.github.io/xrsh-media/assets/background.glb
  #
}