#!/usr/bin/env bash

PID=$$
export ID=$PID
DIR=/tmp/ueberzug_$PID
export UFIFO=$DIR/ufifo
# MAX_COLUMNS=60
IMAGE=$1

function cleanup {
    # remove_image
    stop_ueberzug
    if [ -d "$DIR" ];then
        rm -r $DIR
    fi
    exit
}


if [ ! -d "$DIR" ];then
    mkdir $DIR
fi

source $(ueberzug library)

function start_ueberzug {
    mkfifo $UFIFO
    ImageLayer --silent <"$UFIFO" &
    exec 3>"$UFIFO"
}

function stop_ueberzug {
    exec 3>&-
    rm $UFIFO &> /dev/null
}

# function set_image {
#     x_size=$(tput cols)
#     y_size=$(tput lines)
#     MAX_COLUMNS=$(($x_size/5))
#     x=$(($x_size-$MAX_COLUMNS-5))
#     y=$(($y_size-5))
#     echo $x $y
#     ImageLayer::add [identifier]="$ID" [x]="${x}" [y]="${y}" \
#                     [max_width]="${MAX_COLUMNS}" \
#                     [path]="$@" \
#                     >"$UFIFO"
# }

# function remove_image {
#     ImageLayer::remove [identifier]="$ID" \
#                     >"$UFIFO"
# }

trap cleanup EXIT

start_ueberzug
newsboat
cleanup
