#!/bin/sh

w3mimgdisplay_path="${w3mimgdisplay_path:-/usr/lib/w3m/w3mimgdisplay}"

action=$1 path=$2 width=$5

die () {
    printf "\033[31m$2\033[0m"
    exit "$1"
}
command_exists () {
    command -v "$1" > /dev/null 2>&1
}


view () {
    while :; do
	printf "%b\n%s;\n" "0;1;10;130;$((width*5));$((width*3));;;;;$1" 3 | "$w3mimgdisplay_path"
    done
}

case "$action" in
    start)
        command_exists "w3m" || die 3 "w3m must be installed\n"
        [ -f "$w3mimgdisplay_path" ] || die 3 "$w3mimgdisplay_path does not exist\n"
        ;;
    view) view "$path" ;;
esac
