#!/bin/bash
# Title......: files
# Description: create index.html for files
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Sat 17 Feb 2024 12:56:21 AM CST
#----------------------------------
# Sections: Changes variables functions setup main
# use '#' in vi/vim to jump to word under cursor
: ' Changes
Sat Feb 10 2024 Added version() to display version and changes
Sun Feb 04 2024 Cleaned up a little on the News section with single test
Fri Jan 26 2024 Code clean up and new debug settings
Thu Dec 21 2023 Added creation of include.html for use with iframes
Sun Oct 15 2023 Added tech news to news bottom of news page
'
# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1 # -D to turn on debug mode
[ "$DEBUG" == 1 ] && echo "---Variables---" && set -x
INCLUDE=/var/www/include # location of my html headers and footer
BASE=/var/www
PDIR="$(cd ..;basename $PWD)"
DIR="$(basename $PWD)"
NAME=${0##*/} # name of the script
PDOCOPT="--from=markdown+emoji+lists_without_preceding_blankline+autolink_bare_uris"
[[ $(pwd) == *"$(date +%a|tr [A-Z] [a-z])"* ]] && LOC="NEWS"
if [ -f .conf ] # check for local config file for settings
then
source .conf
else # default values
HEADER="$INCLUDE/media_header.html"
FOOTER="$INCLUDE/footer.html"
TITLE="$(echo ${PDIR}/${DIR}|cut -d'.' -f1|tr '-' ' '|sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')"
SORT="-t"
BACKGROUND=""
fi
[ "$#" -gt 0 ] && OPTIONS="$*" # if OPTIONS is set in .conf, it will use them as long as none are passed
[ "$DEBUG" == 1 ] && set +x && read -p "${N}-Continue-" -n 1 x
# functions
#----------------------------------
bl(){ ## write a blank line
# Use: bl
[ "$DEBUG" == "1" ] && set -x
echo ""
}
html(){ ## mark up code
vim -f +"syn on" +"colorscheme gruvbox" +"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 "Files" "$*"
}
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
bl
sed -n "/' Changes/,/^ *$/p" <$0 |grep -E -v 'sed -n|exit 0|}'
exit 0
}
compile(){ ## Compile
if [ "$LOC" == "NEWS" ] # Add Oped if any mp4 are here
then
[ "$(ls | grep -c mp4)" -gt 0 ] && rss2md -o >>index.md
bl >>index.md
fi
## tail end
if [ -f tail.md ] # Add if here, else do a random quote!
then
bl >>index.md
cat tail.md >>index.md
bl >>index.md
fi
if [ "$LOC" == "NEWS" ] # Add tech news to bottom
then
rss2md -t >>"index.md"
bl >>index.md
fi
if [ $(basename $PWD) != "gif-files" ]
then
bl >>index.md
echo ":scroll:">>index.md
echo "$(quote -w)" >>index.md
bl >>index.md
fi
CURRENT=$(pwd)
if [ $(basename $PWD) != "gif-files" ]
then
echo "[🔝](#)" >>index.md
echo "
" >>index.md
bl >>index.md
fi
echo "Updated: $(date)">>index.md
pandoc "$PDOCOPT" "index.md" -o $$.html # convert markdown to html
cp $$.html include.html # used in iframes, no menus
cat "$HEADER" >"index.html" # create new html file with header
cat $$.html >>"index.html" # add new content
cat "$FOOTER" >>"index.html" # add footer
rm $$.html # clean up
log "Processed: $PWD" # update system log
exit # script ends after a compile
}
# 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
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) </dev/null || sudo apt install bash -qyy
#command -v site >/dev/null || exit 1
command -v pandoc >/dev/null || sudo apt install pandoc -yyq
# create .conf file
#---------------------------
if [[ "$1" == "-conf" ]] && [[ -f "$INCLUDE/$2" ]]
then
cat "$INCLUDE/$2"
exit
fi
if [ "$1" == "-conf" ]
then
echo '
# Sort options:
# extension: -X
# time: -t
# reverse: -r
# size: -S
#--------------------------------------
HEADER="$INCLUDE/media_header.html" # list headers choices: files -l
FOOTER="$INCLUDE/footer.html"
TITLE="CRN/something" # page title
SORT=""
BACKGROUND="" # image for background of HTML (will override header setting)
OPTIONS="" # set default options
'
exit
fi
# file check
#---------------------------
if [ "$1" == "-c" ]
then
for FILE in $(ls|egrep -v 'index|header|tail') # skip matching files
do
[ -d $FILE ] && continue # ignore directories
cd "$BASE"
for SOURCE in $(find . -name '*.md' -print|egrep -v "$DIR|version")
do
if [ $(grep -c "$FILE $SOURCE") -gt 0 ]
then
echo "$FILE: $SOURCE $(grep -c $FILE $SOURCE)"
fi
done
cd - >/dev/null
done
exit
fi
# list headers
#---------------------------
# if you want a copy of the ones I use, email me.
if [ "$1" == "-l" ]
then
cd $INCLUDE
ls ${SORT} *header*
cd - >/dev/null
exit
fi
# list conf files
#---------------------------
# if you was a copy of the ones I use, email me.
if [ "$1" == "-L" ]
then
cd $INCLUDE
ls ${SORT} *conf*
cd - >/dev/null
exit
fi
# directory check
[[ "$(pwd)" != *"include"* ]] && echo "$NAME: directory error" && exit 1 # only run under include
[[ "$(pwd)" == "$INCLUDE" ]] && echo "$NAME: directory error" && exit 1 # but not in it
# main
#---------------------------
[ "$DEBUG" == 1 ] && echo "---Functions---" && typeset -F && read -p "${N}-Continue-" -n 1 x
[ "$DEBUG" == 1 ] && echo "---Main---" && set -x
## rename readme.md to header.md if found - because I changed the way it worked
[ -f readme.md ] && mv readme.md header.md
## top
bl >index.md # start a new file each run
[ ! -z "$BACKGROUND" ] && echo "" >index.md # if a background is set start with that
echo "# $TITLE" >>index.md
bl >>index.md
if [ "$LOC" == "NEWS" ]
then
echo "">>index.md
echo "## $(date "+%A %B %e, %Y")">>index.md
echo "
">>index.md
fi
[ -f header.md ] && cat header.md >>index.md
bl >>index.md
[ -f .bold ] && bl >>index.md && echo "___Bold Italics items are recommended___" >>index.md && bl >>index.md
echo "
" >>index.md
bl >>index.md
if [ "$LOC" == "NEWS" ] # Add daily Proverb to today's clip listing
then
echo -n "[:scroll: Proverb $(date "+%e")](/kjv/proverbs.html#prv-$(date +%-e)){target="blank"}">>index.md
echo " [📚 Morning And Evening](/include/faith/morning-and-evening.pdf#view=FitH&nameddest=Morning, $(date "+%B %-e")){target="blank"}
">>index.md
bl >>index.md
if [ -f head.md ]
then
cat head.md >>index.md
bl >>index.md
/home/mitch/bin/rss2md >>index.md
bl >>index.md
fi
else
[ -f head.md ] && cat head.md >>index.md
bl >>index.md
fi
## check for additional directories
for DIRS in $(\ls -d */ 2>/dev/null)
do
[[ "$OPTIONS" == *"-f"* ]] && break
DIRN=$(echo $DIRS|cut -d'.' -f1|tr '-' ' '|sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')
if [[ $DIRN == *"$(date +%a)"* ]] || [[ "$DIRN" == *"$(date +%b)"* ]]
then
echo "- [📂 ___${DIRN}___](${DIRS}) $([ -f "$DIRS.desc" ] && cat "$DIRS.desc")" >>index.md
else
echo "- [📂 ${DIRN}](${DIRS}) $([ -f "$DIRS.desc" ] && cat "$DIRS.desc")" >>index.md
if [ $(ls -d ${DIRS}*/ 2>/dev/null| wc -l) -gt 0 ]
then
for SUBD in $(\ls -d ${DIRS}*/)
do
SUBN=$(echo $(basename $SUBD)|cut -d'.' -f1|tr '-' ' '|sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')
if [[ "$SUBD" == *"$(date +%b|tr [A-Z] [a-z])"* ]] || [[ "$SUBD" == *"$(date +%a|tr [A-Z] [a-z] )"* ]]
then
echo " - [📂 ___${SUBN}___](${SUBD}) $([ -f "$SUBD.desc" ] && cat "$SUBD.desc")" >>index.md
else
echo " - [📂 ${SUBN}](${SUBD}) $([ -f "$SUBD.desc" ] && cat "$SUBD.desc")" >>index.md
fi
done
fi
fi
if [ ! -f "${DIRS}index.html" ] || [[ "$1" == *"-r"* ]]
then
cd ${DIRS}
$0 -n
cd -
fi
done
[ -n "$DIRS" ] && bl >>index.md && echo "---" >>index.md && bl >>index.md
[[ "$OPTIONS" == *"-d"* ]] && compile
## Get newest and place it at the top, for main directory.
if [[ "$OPTIONS" != *"-n"* ]] && [ $(ls *mp4 2>/dev/null|wc -l) -gt 0 ]
then
NEW=$(find . -type f -name '*mp4' -printf '%C@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
NEWF=${NEW##*/} # name of the file
if [ -f header.md ]
then
if [ $(grep -c "$NEW" header.md) -lt 1 ]
then
[ ! -f .skip ] && pi $NEWF >>index.md
[ ! -f .skip ] && echo "
" >>index.md
fi
else
[ ! -f .skip ] && pi $NEWF >>index.md
[ ! -f .skip ] && echo "
" >>index.md
fi
fi
## under construction (what to display in an empty directory)
if [ "$1" == "-u" ]
then
cp ~/Pictures/under-construction-sign.png . # You will need to change this to a place/pic of yours
else
[ -f under-construction-sign.png ] && \rm under-construction-sign.png
fi
X=Y
## main loop
for FILE in $(ls ${SORT}|egrep -v 'index|header|tail|head|include')
do
[ -d "$FILE" ] && continue # skip if it's a directory
[ "$FILE" = "$NEWF" ] && continue
if [ -f header.md ]
then
[ $(grep -c "$FILE" header.md) -gt 0 ] && continue
fi
if [ -f head.md ]
then
[ $(grep -c "$FILE" head.md) -gt 0 ] && continue
fi
if [ -f tail.md ]
then
[ $(grep -c "$FILE" tail.md) -gt 0 ] && continue
fi
echo -ne "\033[2K" # clear to the beginning of line
echo -ne "\r$FILE" # move cursor to beginning of line, print file, no new line ;)
EXT="${FILE##*.}"
NAME=$(echo $FILE|cut -d'.' -f1|sed -e 's/[-_]/ /g' -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')
case $EXT in
mp4|webm|mkv) # video to play
bl >>index.md
pi $FILE >>index.md
bl >>index.md
echo "[🔝](#) ">>index.md
echo "
>">>index.md
;;
bmp|webp|jpg|jpeg|png|gif) # Pic
bl >>index.md
FILENAME="${FILE%.*}"
if [ $(ls $FILENAME.* |wc -l) -lt 2 ]
then
pi $FILE |tr -d '\n' >>index.md
fi
;;
md) # markdown
FILENAME="${FILE%.*}"
if [ $(ls $FILENAME.* |wc -l) -lt 2 ]
then
mkln $FILE >>index.md
fi
;;
*) # fallback
mkln $FILE >>index.md
;;
esac
done
compile