#!/bin/sh

set -e
set -u

file="$1"
width="${2:-80}"
height="${3:-24}"
case "$(file -Lb --mime-type -- "$file")" in
image/*)
  chafa -f sixel -s "${width}x${height}" --animate off --polite on -t 1 --bg black "$file"
  ;;
text/*)
  cat -v "$file"
  ;;
application/pdf)
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT
  pdftoppm -png -singlefile -r 144 "$file" "$tmpdir/output"
  chafa -f sixel -s "${width}x${height}" "$tmpdir/output.png"
  ;;
*)
  echo "Unsupported file type: $file" >&2
  exit 1
  ;;
esac
