#!/usr/bin/env sh

# Description: Open hovered or current directory in image viewer.
#              Generates media thumbnails with optional dependencies.
#
# Dependencies:
#   - imv (https://github.com/eXeC64/imv) or,
#   - sxiv (https://github.com/muennich/sxiv) or,
#   - nsxiv (https://codeberg.org/nsxiv/nsxiv) or,
#   - ucollage (https://github.com/ckardaris/ucollage) or,
#   - lsix (https://github.com/hackerb9/lsix), or
#   - viu (https://github.com/atanunq/viu), or
#   - catimg (https://github.com/posva/catimg), or
#   - optional: ffmpeg for audio thumbnails (album art)
#   - optional: ffmpegthumbnailer for video thumbnails
#
# Shell: POSIX compliant
# Author: Arun Prakash Jana, Luuk van Baal
#
# Consider setting NNN_PREVIEWDIR to $XDG_CACHE_HOME/nnn/previews
# if you want to keep media thumbnails on disk between reboots.
NNN_PREVIEWDIR="${NNN_PREVIEWDIR:-${TMPDIR:-/tmp}/nnn/previews}"

exit_prompt() {
    [ -n "$1" ] && printf "%s\n" "$1"
    printf "%s" "Press any key to exit..."
    cfg=$(stty -g); stty raw -echo; head -c 1; stty "$cfg"
    clear
    exit
}

make_thumbs() {
    mkdir -p "$NNN_PREVIEWDIR$dir" || return
    if [ "$1" = "viu" ] || [ "$1" = "catimg" ]; then
        [ -d "$target" ] && exit_prompt "$1 can only display a single image"
        mime="$(file -bL --mime-type -- "$target")"
        case "$mime" in
          audio/*) ffmpeg -i "$target" "$NNN_PREVIEWDIR$target.jpg" -y >/dev/null 2>&1
              ret="$NNN_PREVIEWDIR/$target.jpg" ;;
          video/*) ffmpegthumbnailer -i "$target" -o "$NNN_PREVIEWDIR$target.jpg" 2> /dev/null
              ret="$NNN_PREVIEWDIR/$target.jpg" ;;
          *) ret="$target" ;;
        esac
    fi
    for file in "$dir"/*; do
        if [ ! -f "$NNN_PREVIEWDIR$file.jpg" ]; then
            case "$(file -bL --mime-type -- "$file")" in
                audio/*) [ "$1" != "sxiv" ] &&
                    ffmpeg -i "$file" "$NNN_PREVIEWDIR$file.jpg" -y >/dev/null 2>&1 ;;
                video/*) [ "$1" != "ucollage" ] &&
                    ffmpegthumbnailer -i "$file" -o "$NNN_PREVIEWDIR$file.jpg" 2> /dev/null ;;
            esac
        fi
    done
    for file in "$NNN_PREVIEWDIR$dir"/*; do
        filename="$(basename "$file" .jpg)"
        [ ! -e "$dir/$filename" ] && rm -- "$file" 2>/dev/null
    done
}

listimages() {
    find -L "$dir" "$NNN_PREVIEWDIR$dir" -maxdepth 1 -type f -print0 2>/dev/null | sort -z
}

view_files() {
    [ -f "$target" ] && count="-n $(listimages | grep -a -m 1 -ZznF "$target" | cut -d: -f1)"
    case "$1" in
        nsxiv) listimages | xargs -0 nsxiv -a "${count:--t}" -- ;;
        sxiv) listimages | xargs -0 sxiv -a "${count:--t}" -- ;;
        imv*) listimages | xargs -0 "$1" "${count:-}" -- ;;
    esac
}

target="$(readlink -f "$1")"
[ -d "$target" ] && dir="$target" || dir="${target%/*}"
if uname | grep -q "Darwin"; then
    [ -f "$1" ] && open "$1" >/dev/null 2>&1 &
elif type lsix >/dev/null 2>&1; then
    if [ -d "$target" ]; then
        cd "$target" || exit_prompt
    fi
    make_thumbs lsix
    clear
    lsix
    cd "$NNN_PREVIEWDIR$dir" && lsix
    exit_prompt
elif type ucollage >/dev/null 2>&1; then
    type ffmpeg >/dev/null 2>&1 && make_thumbs ucollage
    UCOLLAGE_EXPAND_DIRS=1 ucollage "$dir" "$NNN_PREVIEWDIR$dir" || exit_prompt
elif type sxiv >/dev/null 2>&1; then
    type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs sxiv
    view_files sxiv >/dev/null 2>&1 &
elif type nsxiv >/dev/null 2>&1; then
    type ffmpegthumbnailer >/dev/null 2>&1 && make_thumbs sxiv
    view_files nsxiv >/dev/null 2>&1 &
elif type imv >/dev/null 2>&1; then
    make_thumbs imv
    view_files imv >/dev/null 2>&1 &
elif type imvr >/dev/null 2>&1; then
    make_thumbs imv
    view_files imvr >/dev/null 2>&1 &
elif type viu >/dev/null 2>&1; then
    clear
    make_thumbs viu
    viu -n "$ret"
    exit_prompt
elif type catimg >/dev/null 2>&1; then
    make_thumbs catimg
    catimg "$ret"
    exit_prompt
else
    exit_prompt "Please install sxiv/nsxiv/imv/viu/catimg/lsix."
fi
