export HOME=/root 
export PATH=~/bin:$PATH:/etc

set +m # mute job control notifications

# source URL data from v86 (file created by v86 during boot)
test -f /mnt/profile.browser && source /mnt/profile.browser

# source xrsh env
source /etc/profile.xrsh

# source shell functions
source /etc/profile.sh

# source user shell defaults
test -f ~/.profile && source ~/.profile

# source js functions 
test -f /etc/profile.js && chmod +x /etc/profile.js && /etc/profile.js | grep -v undefined

# source user js functions 
test -f ~/.profile.js && chmod +x ~/.profile.js && ~/.profile.js | grep -v undefined

## forward not-found commands to javascript (via jsh)
command_not_found_handle(){
  echo "[!] '$1' not found, did you mean $1(...) (javascript?)"
  test -n "$ONBOARDING" && echo "[i] type 'help' for handy commands"
  test -n "$ONBOARDING" || help_tips
}

help_tips(){
  echo ""
  echo 'TIPS'
  echo '----'
  echo 'text editors          ' "type '<codemirror|vi|mg|nano> <textfile>'"
  echo 'programming languages ' "type 'ls ~/bin/helloworld*'"
  echo 'run 64bit linux app   ' "type 'blink <x86_64 binary>'"
  echo 'paste text            ' "ctrl/cmd+shift+v'"
  echo 'paste text to file    ' "ctrl/cmd+v'"
  echo 'import file to scene  ' "drag-drop file or ctrl/cmd+v or type 'upload'"
  echo 'import remote URL     ' "type 'require <https://foo.org/scene.glb>'"
  echo 'reset scene & shell   ' "type 'reset'"
  echo 'js run                ' "type 'js 'alert(\"hello\")'"
  echo 'js console.log:       ' "type 'console document.baseURI"
  echo 'js function as cmd:   ' "type 'alias $1=\"jsh $1\"' to run '$1 yo' as $1('yo')"
  echo 'js inspect:           ' "type 'js \"return document.baseURI\"'"
  echo 'js capture console:   ' "type 'tail -f /dev/browser/console'"
  echo 'js<->filesystem hooks:' "type 'see [executable] files in ~/hook.d'"
  echo 'include file into page' "type 'require <url.js|css>'"
  echo 'say something         ' "type 'say hello'"
  echo ''
  echo 'type "help" or "man xrsh" to read the full manual'
  ONBOARDING=1
}

export TERM=xterm-256color
export PS1="\[\e[36m\]> \[\e\[0m\]"

# aliases
alias ls='ls -ha -w100'

# interactive login
which screen &> /dev/null && {
  [[ -t 0 ]] && {
    test -n "$STY" || {
      resize # call twice
      resize # otherwise COLUMNS/ROWS is 0
      # add URL-hash as extra screen session
      test -z "$HASH" || {
        grep 'screen -t #' /root/.screenrc || {
          echo "screen -t xrsh+URL 3 /bin/sh -c '${HASH}; sh'" | sed "s/'#/'/g"  >> /root/.screenrc
        }
      }
      screen -Aa -R -T screen-256color -c /root/.screenrc
    }
    test -n "$STY" && { 
      test -f /root/motd && cat /root/motd || cat /etc/motd; 
      say "welcome to eex r, shell. For an optimal user experience, connect a Bluetooth keyboard." &
    }
  }
}

cd $HOME