#!/bin/sh
#
# general script to start a browser
# possible browsers:
#   graphical: firefox qutebrowser dwb luakit jumanji surf uzbl
#   text: elinks w3m links lynx
#
# ---
# source: https://github.com/lucc/shell/blob/main/browser

type=graphical
mode=normal
branch=normal
pipe=false

usage() {
  prog=${0##*/}
  echo "Usage: ${prog} [-dgptx] [url]"
  echo "       cat file.html | ${prog} -s [options]"
  echo "       ${prog} -h"
}

help() {
  usage
  echo
  printf '  %s\t%s\n' \
    -d 'use development code of the browser' \
    -g 'open a graphical browser' \
    -h 'show this help' \
    -p 'start in private browsing mode (implies -g)' \
    -s 'expect document on stdin (no url allowed)' \
    -t 'open a text browser' \
    -x 'activate debug output'
  exit
}

qutebrowser() {
  qutebrowser_${mode} "$@"
}
qutebrowser_dev() {
  #export QT_QPA_PLATFORM=wayland-egl QT_WAYLAND_DISABLE_WINDOWDECORATION=1
  export PYTHONPATH=~/vcs/qutebrowser
  python -m qutebrowser "$@"
}
qutebrowser_stable() {
  command qutebrowser "$@"
}
qutebrowser_normal() {
  qutebrowser_stable "$@"
}
qutebrowser_private() {
  tempdir=$(mktemp -d)
  cp -r ~/.config/qutebrowser "${tempdir}/config"
  mkdir "${tempdir}/data"
  cp -r ~/.local/share/qutebrowser/userscripts "${tempdir}/data/userscripts"
  #trap 'rm -rf "$tempdir"' EXIT SIGTERM SIGKILL
  qutebrowser_${branch} \
    --basedir "${tempdir}" \
    --set auto_save.session false \
    --set content.javascript.enabled false \
    :adblock-update \
    "$@"
  #--set content.headers.user_agent 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36' \
  rm -rf "${tempdir}"
}
firefox_profiles() {
  ini=${HOME}/.mozilla/firefox/profiles.ini
  grep '^Name=' "${ini}" | cut -f 2 -d =
}
firefox_mac() {
  for profile in $(firefox_profiles); do
    if [ "${profile}" = "$1" ]; then
      # TODO
      exec open -na Firefox --args -P "${profile}" -no-remote
    fi
  done
  # shellcheck disable=SC2312
  echo "$1 is not a valid profile. Try one of $(firefox_profiles)."
  exit 2
}
firefox() {
  export VIMPERATOR_RUNTIME=~/.config/vimperator
  export VIMPERATOR_INIT="source ${VIMPERATOR_RUNTIME}/vimperatorrc"
  exec firefox "$@"
}
firefox_new() {
  user=${1:-${USER}}
  shift
  firefox -new-instance -P "${user}" "$@"
}
firefox_private() {
  exec firefox --new-instance --private-window "$@"
}

text() {
  text_browser=elinks
  if [ -t 1 ] || [ -t 2 ]; then
    exec "${text_browser}" "$@"
  else
    exec term -e "${text_browser}" "$@"
  fi
}
graphical() {
  qutebrowser "$@"
}

while getopts hdgpstx opt; do
  case ${opt} in
    d) branch=dev ;;
    g) type=graphical ;;
    h) help ;;
    p) mode=private ;;
    s) pipe=true ;;
    t) type=text ;;
    x) set -x ;;
    *)
      usage >&2
      exit 2
      ;;
  esac
done
shift "$((OPTIND - 1))"

if [ "$#" -eq 1 ] && [ "$1" = - ]; then
  shift
  pipe=true
fi

if ${pipe}; then
  # save stdin to a file
  file=$(mktemp -t tmp-$$-XXXXXX) || exit 3
  trap 'trap - HUP INT TERM; rm -f "$file" "$file.html"' HUP INT TERM
  cat >"${file}" || exit 4
  case $(file --brief --mime-type "${file}") in
    text/html)
      mv "${file}" "${file}.html"
      file=${file}.html
      ;;
  esac
  ${type} "${file}"
else
  ${type} "$@"
fi
