#!/usr/bin/env bash

if [ -z "$DISPLAY" ] && [ -x "$(command -v startx)" ] && ! pgrep herbstluftwm >/dev/null; then
  # start x if it's available and we're not already in it
  startx
else
  if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ linux ]] && [ -z "$TMUX" ]; then
    # otherwise start tmux if it's available and we're not already in it
    exec tmux
  else
    # otherwise robot quote
    if [ -x "$(command -v botsay)" ]; then
      len=1642 #$(jq '. | length' ~/skynet/quotes.json)
      idx=$(( RANDOM % len))
      quote=()
      while read -r value; do
        quote+=("$value")
      done < <(jq -r ".[$idx] | .quoteText, .quoteAuthor" ~/skynet/shell/quotes.json)
      quoteText="${quote[0]}"
      if [ -n "${quote[1]}" ]; then
        quoteText="$quoteText  --${quote[1]}"
      fi
      echo "$quoteText" | botsay -c -
    fi
  fi
fi
