#!/usr/bin/env sh

# opout: "open output": A general handler for opening a file's intended output,
# usually the pdf of a compiled document.  I find this useful especially
# running from vim.
#
# from https://github.com/LukeSmithxyz/voidrice/tree/master/.local/bin

basename="$(echo "$1" | sed 's/\.[^\/.]*$//')"

openCompiledDoc() {
  if [ -f "$basename".pdf ]; then
    setsid "$FILEREADER" "$basename.pdf"
  elif [ -f "$(echo "$basename" | sed 's/ /-/g')".pdf ]; then
    setsid "$FILEREADER" "$(echo "$basename" | sed 's/ /-/g').pdf"
  elif [ -f "$(echo "$basename" | sed 's/ /_/g')".pdf ]; then
    setsid "$FILEREADER" "$(echo "$basename" | sed 's/ /_/g').pdf"
  elif [ -f "$basename".html ]; then
    setsid "$BROWSER" "$basename.html"
  fi
}

case "$1" in
	# *.tex|*.md|*.rmd|*.ms|*.me|*.mom) setsid "$FILEREADER" "$basename".pdf >/dev/null 2>&1 & ;;
  *.tex|*.md|*.[Rr]md|*.ms|*.me|*.mom) openCompiledDoc >/dev/null 2>&1 & ;;
	*.[0-9]) openCompiledDoc >/dev/null 2>&1 & ;;
	*.html) openCompiledDoc >/dev/null 2>&1 & ;;
esac