#!/bin/bash

# Title......: f
# Description: locate and open files based on type
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Tue 28 Nov 2023 07:34:33 AM CST
#----------------------------------

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
NAME=${0##*/}                              # name of the script

# Colors - uncomment if needed
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
C=$(tput setaf 6)                          # cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
ROWS=$(tput lines)
COLS=$(tput cols)

# functions
#----------------------------------

bl(){ ## write a blank line
    # Use: bl
    [ "$DEBUG" == "1" ] && set -x
    echo ""
}

html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}

log(){ ## creates a basic log entry $LOG must be defined
    # Use: log {entry}  
    [ "$DEBUG" == "1" ] && set -x
    logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
    # Use: pause  {optional number of seconds} or "-nt" for no time out 
    [ "$DEBUG" == "1" ] && set -x
    [ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
    then
        read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
        read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

xtitle(){ ## set window title
    # Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
    vim $0
    sed -i -e "7s/.*/# Updated....: $(date)/" $0
    log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    cp $0 /var/gopher/scripts
    exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
$NAME  ${BG}A file locate and open a file${N}

f {option}

None    Default is to perform a fuzzy search and view/play file selected
-       Open last file


END
exit
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy
command -v vd >/dev/null || pip3 install visidata
command -v xdotool >/dev/null || sudo apt install xdotool -yyq
command -v bat >/dev/null || sudo apt install bat-musl -yyq
command -v lynx >/dev/null || sudo apt install lynx -yyq

# directory check
#[[ "$(pwd)" != *"foo"* ]] && echo "$NAME: directory error" && exit 1

# main
#--------------------------- 
[ "$DEBUG" == 1 ] && set -x

cd ~
if [ "$1" == "-" ]
then
    FILE=$(cat /tmp/o.last)
else
    FILE=$(fzf)
fi
EXT="${FILE##*.}"
echo "$FILE" >/tmp/o.last
case $EXT in
    csv|tsv|xls|dbf) # database
            vd $FILE
            ;;
        bmp|webp|jpg|jpeg|png|gif) # Pic
            pix $FILE
            ;;
        epub|mobi) # ebook
            /home/mitch/.local/bin/epy $FILE
            ;;
        html) # web file
            lynx $FILE
            ;;
        md) # markdown
            vi $FILE
            ;;
        mp4|webm|mkv) # video to play
            [ -z $DISPLAY ] || WINDOW=$(xdotool getactivewindow 2>/dev/null);
            xtitle "$FILE";
            echo "Playing: $FILE";
            xdotool windowminimize $WINDOW;
            xfconf-query --channel=xfwm4 --property=/general/use_compositing --type=bool --toggle;
            spinit vlc --play-and-exit $FILE 1>&2 2> /dev/null;
            xdotool windowactivate $WINDOW;
            xfconf-query --channel=xfwm4 --property=/general/use_compositing --type=bool --toggle;
            ;;
        *3) # audio
            mpv $FILE
            ;;
        *) # fallback
            bat -p $FILE
            ;;
esac

exit

# vim: nospell ft=sh