#!/bin/bash

# Title......: mkln
# Description: create link
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Fri 03 Jan 2025 04:32:33 AM CST
#----------------------------------

# Sections: Changes variables functions setup main
# use '#' in vi/vim to jump to word under cursor

: ' Changes
Wed Jan 17 2024 Added support for epub viewer, more to come.
'

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
DIR="$(basename $PWD)"
TITLE="$(echo $DIR|cut -d'.' -f1|tr '-' ' '|sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')"
NAME=${0##*/}                       # name of the script

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

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

html(){ ## mark up code
    vim -f +"syn on" +"colorscheme termschool" +"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" "$*"
}

version(){ ## display version and change history
    grep -E '^# Updated' $0
    echo " "
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# 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
    exit
fi

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

Generate an markdown link to file.

END
exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy

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


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

FILE=$1
EXT="${FILE##*.}"
NAME=$(echo $FILE|cut -d'.' -f1|sed -e 's/[-_]/ /g' -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')

case $EXT in
    m4v|mp4|mpv|avi) # videos
        ICO="🎬"
        ;;
    pdf|epub|mobi|azw3|html) # ebooks
        ICO="📚"
        ;;
    wav|mp3|midi) # audio
        ICO="🎧"
        ;;
    jpg|jpeg|png|gif) # Pic
        ICO="🎨"
        ;;
    iso|gz|zip|tar|tgz|7z) # archive
        ICO="📦"
        ;;
    exe) # Windows or DOS program
        ICO="💾"
        ;;
    deb) # Linux file installable
        ICO="💻"
        ;;
    html|txt|md) # text file
        ICO="📄"
        ;;
    ppt|pptx) # text file
        ICO="🎦"
        ;;
    json|db|csv|tsv) # text file
        ICO="📇"
        ;;
    *) # fallback
        ICO="🚧"
        ;;
esac

# set path/name
cd /var/www/
NEW="$(find . -name $1 -print |cut -d'.' -f2-)"
SIZE=$(ls -sh /var/www/${NEW}|cut -d' ' -f1)           # Get size in human readable format
cd - >/dev/null

case "$EXT" in
    pdf)  NEW="${NEW}#view=FitH"
        ;;
    epub) VIEW="[👓](/epubviewer#/include${NEW})"
        ;;
    mobi) VIEW="__DL 1st__, [👓](/apps/foliate/)"
        ;;
    azw3) VIEW="__DL 1st__, [👓](/apps/foliate/)"
        ;;
    *) VIEW=" "
        ;;
esac

if [ -f .bold ] && [ $(grep -c "$FILE" .bold) -gt 0 ]
then
    echo "- [${ICO} ___${NAME}.${EXT} ${SIZE}___](${NEW}) $VIEW"
else
    echo "- [${ICO} ${NAME}.${EXT} ${SIZE}](${NEW}) $VIEW"
fi

if [ "$EXT" == "mp3" ] || [ "$EXT" == "wav" ]
then
cat <<End

<audio controls="1" preload="none">
  <source src="${NEW}"  data-external="1" type="audio/mpeg">
</source>
</audio>

End

echo "---"
echo " "
fi