#!/bin/sh

case "$1" in
  # First check common extensions
  *.png|*.PNG|*.jpg|*.JPG|*.bmp|*.jpeg|*.gif|*.xpm|*.webp|*.WEBP) exiftool "$1" || identify "$1" ;;
  *.wav|*.mp3|*.flac|*.m4a|*.ape|*.ac3|*.oga|*.ogg|*.ogx|*.spx|*.dsf|*.opus|*.dff|*.wma|*.wv|*.wvc) exiftool "$1" ;;
  *.avi|*.mp4|*.wmv|*.dat|*.3gp|*.vob|*.ogv|*.mkv|*.mpeg|*.mpg|*.fli|*.flc|*.flv|*.m2v|*.webm|*.mts|*.qt|*.divx|*.asf|*.asx|*.m4v|*.mov)
    exiftool "$1" || fprobe -pretty "$1" 2>&1 ;;
  *.pdf) mutool draw -F txt -i -- "$1" 1-10 || pdftotext -l 10 -nopgbrk -q -- "$1" - || exiftool "$1" ;;
  *.ps) pstotext "$1" || ps2ascii "$1" ;;
  *.epub|*.fb2) pandoc -s -t markdown -- "$1" ;;
  *.djvu) djvutxt "$1" || exiftool "$1" ;;
  *.html|*.xhtml|*.htm) pandoc -s -t markdown -- "$1" || lynx -dump -- "$1" ;;
  *.mkd|*.md|*.markdown) mdcat "$1" || glow -s dark "$1" ;;
  *.ipynb) notedown --from notebook "$1" --to markdown | pandoc -f markdown -t plain ;;
  *.torrent) dumptorrent -v "$1" || transmission-show -- "$1" ;;
  *.zip|*.jar|*.war|*.ear|*.oxt) zip -sf "$1" || zipinfo "$1" || atool -l -q "$1" ;;
  *.tar) tar -tvf "$1" || atool -l -q "$1" ;;
  *.tgz|*.tar.gz) tar -tvf "$1" || atool -l -q "$1" ;;
  *.tbz2|*.tar.bz2) tar -tvjf "$1" || atool -l -q "$1" ;;
  *.tar.txz|*.txz) xz --list "$1" || atool -l -q "$1" ;;
  *.rar) unrar lt -p- -- "$1" || atool -l -q "$1" ;;
  *.7z) 7z l -p -- "$1" || atool -l -q "$1" ;;
  *.iso) isoinfo -l -i "$1" ;;
  *.doc) catdoc -- "$1" || pandoc -s -t markdown -- "$1" ;;
  *.docx) pandoc -s -t markdown -- "$1" ;;
  *.odt|*.ott|*.stw|*.sxc) pandoc -s -t markdown -- "$1" || odt2txt "$1" ;;
  *.xls) xls2csv -- "$1" ;;
  *.xlsx) xlsx2csv -- "$1" ;;
  *.ods|*.odp|*.sxw) pandoc -s -t markdown -- "$1" ;;
  *.json|*.jsonc) jq --color-output . "$1" || python -m json.tool -- "$1" ;;
  # Then fallback to mimetypes
  *)
    mimetype=$(file --dereference --brief --mime-type -- "$1")
    case "$mimetype" in
      inode/directory)
        exa -1a --sort=type --color always --icons "$1" ;;
      application/zip|application/x-tar*|application/*-compressed-tar\
        |application/vnd.rar|application/x-7z-compressed|application/x-xz*\
        |application/x-bzip|application/gzip|application/zstd|application/zlib\
        |application/x-lzma|application/x-lzip|application/x-lha|application/x-lhz\
        |application/x-lzop|application/x-lz4|application/x-lrzip)
        atool -l -q "$1" ;;
      image/vnd.djvu*)
        djvutxt "$1" || exiftool "$1" ;;
      image/*)
        exiftool "$1" || identify "$1" ;;
      video/*)
        exiftool "$1" || fprobe -pretty "$1" 2>&1 ;;
      text/*)
        bat --style plain --color=always "$1" ;;
      *)
        echo '----- File Type Classification -----' && file --dereference --brief -- "$1" ;;
    esac ;;
esac
