#!/bin/bash

# Title......: mkln
# Description: create link
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Sun May 17 08:44:55 AM CDT 2026
#----------------------------------

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

: ' Changes
Sun May 17 2026 Added -F view functions, -V view source
Tue Jul 08 2025 Added ability to handle full path and local URLs
Sat Jun 14 2025 Added hover effect to mp3 audio player
Sun Feb 23 2025 Fixed epub viewer
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
#----------------------------------

# part of my code management system
# if you want a function or peice of code from another script:
#  !!{script} -F {function}
#  will insert the code.
[[ "$1" = "-V" ]] && bat -p -l bash $0 && exit
if [[ "$1" = "-F" ]] 
    then
        [[ "$#" -eq 2 ]] && declare -f "$2" |bat -p -l bash && exit
        [[ "$#" -eq 1 ]] && declare -f |bat -p -l bash && exit
fi

# 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

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

if [[ "$1" == *"/"* ]]
then
    exec $0 $(basename $1)
fi

FILE="$1"
MDFILE="${FILE%.mp3}.md"
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) # 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="💻"
		;;
	htm|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="[👓](/apps/epubviewer/#${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

# Add comments 
if [ -f $MDFILE ]
then
    cat $MDFILE
fi

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

<figure class="wp-block-audio">
<style>audio.example-2 {width:300px;margin:10px auto;}audio.example-2:hover{transform: scale(1.1);filter: drop-shadow(2px 3px 3px #333);}</style>
<audio controls="" src="${NEW}" class="example-2">&gt;</audio></figure>

End

echo "---"
echo " "
fi

