#!/bin/bash
# bldidx.sh - build a web site in $BLDDIR ready for export to live web server
# 20180429gnl - Glenn Neil Lyons
# Vers 3.0
#
# This script builds an index.html with the top level topics using the folders (under Amatuer-Radio)
# as the main menu with links to the individual topic content section. The contect section lnk to
# the individual content pages.
#
#
################################
# Check Enviroment
#################################
# Check we are root
if [ "$(id -u)" = "0" ];then
echo "This script does not require to be run as root" 1>&2
echo "Please login as the user that maintains the LyonsComputer Web Site."
exit 1
fi
command -v linkchecker >/dev/null 2>&1 || { echo >&2 "'linkchecker' is not installed. Try 'sudo apt install linkchecker'"; exit 1; }
command -v sitecopy >/dev/null 2>&1 || { echo >&2 "'sitecopy' is not installed. Try 'sudo apt install sitecopy'"; exit 1; }
# must have $ARADIO as base folder of Amateur Radio html files
if [ -z $ARADIO ]
then
echo $0: "Enviorment Variable $ARADIO is not set".
exit 1
fi
cd $ARADIO # must start here # Change to the working directory
if [ "$1x" == "x" ];then
operation="build" # do the full build
else
operation=$1
fi
# operation="components" # check components
echo "Running in $operation mode".
##################################
# Define Varaibles and Initialise
##################################
INDEX="index.html"
BLDDIR="/home/glenn/www-build"
OUTFILE=$BLDDIR/$INDEX
dirname=${file%/*} # directory name
LOGDIR="00-logs"
LOG=$LOGDIR/"bldidx.log"
DATE=`date`
start=`date +%s`
MISFILE=$ARADIO/$LOGDIR/"Missing-Files.txt"
MISCNT=0 # count of missing file required to be loaded
SKIPPED_PAGES="$ARADIO/$LOGDIR/Skipped_pages.txt"
skipped_cnt=0 # html pages without the "
VK4PK" string
SP=" "
SP2="$SP$SP"
SP4="$SP$SP$SP$SP"
INDENT="" # increment with SP4 for each level down
cnt=0 # count the html pages processed
COMCNT=0 # count the comonent files required
#########################################################################################
# Functions
#########################################################################################
################################################################################################
# Are we ready to go?
################################################################################################
initialise_build() {
echo -n "/home/glenn/www-build will be overwritten! PROCEED? [Y|y]"
read ans
if [ "$ans" != "Y" -a "$ans" != "y" ]
then
echo "Quitting."
exit 1
fi
echo Removing $BLDDIR and all its files
rm -rf $BLDDIR
if [ ! -d $BLDDIR ];then
mkdir $BLDDIR
fi
# Copy stylesheet and setting permissions
if [ -f stylesheet-for-amateur-radio.css ]
then
cp stylesheet-for-amateur-radio.css $BLDDIR
else
echo "WARNING - canot find file: stylesheet-for-amateur-radio.css"
fi
# Copy images files
if [ -f images/LyonsLogo.png ]
then
mkdir $BLDDIR/images
cp images/LyonsLogo.png $BLDDIR/images/LyonsLogo.png
else
echo "WARNING - cannot find file: images/LyonsLogo.png"
fi
# Initialise logs files
cat /dev/null > $MISFILE
cat /dev/null > $SKIPPED_PAGES
}
#############################################################################################
# Rotate Logs
#############################################################################################
rotate_log() {
# rotate the log file
echo "Rotating the Log file $LOG"
for n in {8..1}
do
if [ -f $LOG.$n ]
then
mv -f "$LOG.$n" "$LOG.$((n+1))"
fi
done
if [ -f $LOG ]
then
mv $LOG $LOG.1
fi
echo "-----------------------------------------------------------------------------" >>$LOG
echo "bldidx.sh $DATE">$LOG # Initialise a new log file
return
}
################################################################################################
#
################################################################################################
build_header() {
cat > $OUTFILE << 'EOF' # first output to file so reset file
Amateur Radio Station VK4PK - Glenn
<link rel="stylesheet" type="text/css" href="stylesheet-for-amateur-radio.css">
<link rel="icon" type="image/png" href="images/LyonsLogo.png">
<h1 id="home">VK4PK Menu
EOF
}
################################################################################################
# Build the top level index Section. Add ".ignore" file to exclude the folder and it's contents
################################################################################################
build_top_index() {
cat >> $OUTFILE <<'EOF'
EOF
for folder in `ls`
do
if [ ! -d $folder ];then # only want folders
continue
fi
if [ -f $folder/.ignore ];then # we do not want this folder
continue
fi
echo "- <a href="""#${folder}""">"${folder}"
" >>$OUTFILE
done
echo "
" >>$OUTFILE
echo "
" >>$OUTFILE
echo "
" >>$OUTFILE
}
###########################################################################
# Copy file Componects to the temporary build folder (www-build)
###########################################################################
copy_components() {
component=""
dirname=${line%/*} # directory name
# NOTE linkchecker only report missing links so this script relies on the the build directory not
# having the linked files in place untill after the procedure is run
#
# tell linkchecker "-ocvs" to output in csv format
# "2>" dump error messages
# "sed '1,4d ; /^# /d' delete all lines 1 to 4 and all "#" comment lines
#| cut -d';' -f1` set the delimiter to ";" and select the second field
# linkchecker --no-warnings --no-status -ocsv $BLDDIR/$line 2>/dev/null |sed '/^#/d;/^urlname/d' | cut -d';' -f1
#
# for component in `linkchecker -ocsv $BLDDIR/$line 2>/dev/null| sed '1,5d ; /^# /d' | cut -d';' -f1`
for component in `linkchecker --no-warnings --no-status -ocsv $BLDDIR/$line 2>/dev/null |sed '/^#/d;/^urlname/d' | cut -d';' -f1`
do
NEEDS=$dirname/$component
echo " requires-->"$NEEDS
echo " requires-->"$NEEDS >>$LOG
let "COMCNT=COMCNT+1"
cp --parent $NEEDS $BLDDIR # --parents creates parent folder same as source
if [ $? -ne 0 ]
then
let "MISCNT=MISCNT+1"
echo "$line --> $NEEDS" >> $MISFILE
fi
done
}
########################################################################
# Build the contents part of the menu system to link html pages
########################################################################
build_contents() {
declare -a array
cnt=0
indent=""
cd $ARADIO
for line in `find * -name "*.html" -print | sort `
do
if [ "$line" = "./index.html" ];then
continue # skip
fi
if [ -f $line ] && [ ${line: -5} == ".html" ];then
if ! grep -q "VK4PK" "$line" ;then
if [ "$operation" == "build" ];then
echo $line >> $SKIPPED_PAGES
fi
((skipped_cnt++))
continue # skip
fi
fi
# Build an array of directories and files
list=`echo $line | sed 's/\// /g'`
item=""
((i=-1))
for item in $list
do
((i++))
array[$i]=$item
done
# remove repeat last heading if same as html filename
# DO not change $i or $item before this section
html_file=${item%.*} # remove the extension part
second_last_item="${array[(($i-1))]}"
if [[ $second_last_item == $html_file ]];then
unset array[$i] # remove the last
array[(($i-1))]=$item
unset last_array[$i] # remove the last
last_array[(($i-1))]=$item
fi
# Loop through the array and output the appropriate menu lines
item=""
str=""
length=${#array[@]}
((length++)) # Start at 1 not 0
for (( j=0; j<${length}; j++ ));
do
item=${array[$j]}
# Strip off the heading of the section
if [[ $j == 0 ]];then
# gnl20220114 Special case with one html file/page in the top level directory
# The array structure needs to be rewritten but this will work around the poor design
# Print a section heding and then a link for the html page
if [[ ${array[$j]} = ${last_array[$j]} ]];then
html_file=${item%.*} # remove the extension part
extension="${array[$j]##*.}"
if [ $extension == "html" ];then
item_no_ext=${item%.*} # Remove extension in this case
echo "$item_no_ext$SP4[Home]
" >>$OUTFILE
echo "$SP4$html_file
" >>$OUTFILE
cp --parent $line $BLDDIR
echo $line
echo $line >>$LOG
copy_components
fi
fi
# gnl20220114 - end
if [[ ${array[$j]} != ${last_array[$j]} ]];then
if [ "$operation" == "build" ];then
echo "" >>$OUTFILE
fi
fi
fi
if [ $j -gt 0 ];then
indent=$indent$SP4
# This is the link so copy file to www-build and get it's dependant files
if [[ "$item" == *".html" ]]; then
html_file=${item%.*} # remove the extension part
if [ "$operation" == "build" ];then
echo "$indent$html_file
" >>$OUTFILE
fi
cp --parent $line $BLDDIR
echo $line
echo $line >>$LOG
copy_components
else
# Just a heading not a link
if [[ ${array[$j]} != ${last_array[$j]} ]];then
if [ "$operation" == "build" ];then
echo "$indent$item
" >>$OUTFILE
fi
fi
fi
fi
done
((cnt++))
#echo "$cnt $line"
### copy this array to be last_array in next iteration ###
last_array=("${array[@]}")
unset array # need a clean array fo rnext iteration
indent=""
# for Testing Only
# if [ $cnt -gt 50 ];then
# exit
# fi
done
}
###################################
# Footer
###################################
build_footer() {
echo " " >> $OUTFILE
echo "" >> $OUTFILE
}
install_build() {
echo "Setting permissions"
chown -R glenn:glenn $BLDDIR
chmod -R a+rw $BLDDIR
echo "Returning a fresh copy on index.html to `pwd`"
cp $BLDDIR/index.html .
end=`date +%s`
secs=$((end-start))
echo "-----------------------------------------------------------------------------"
echo $DATE
echo "HTML Files:" $cnt
echo "Component Files:" $COMCNT
echo "Missing Files:" $MISCNT
echo "Missing File List:" $MISFILE
echo "Skipped Files: $skipped_cnt (html pages without the \"VK4PK\" string)"
echo "Skipped File List:" $SKIPPED_PAGES
echo -n "Procedure Complete in "
printf '%dh:%dm:%ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))
#
# Write to log
#
echo $DATE >>$LOG
echo "HTML Files:" $cnt >>$LOG
echo "Component Files:" $COMCNT >>$LOG
echo "Missing Files:" $MISCNT >>$LOG
echo "Missing File List:" $MISFILE >>$LOG
echo "Skipped Files:" $skipped_cnt >>$LOG
echo "Skipped File List:" $SKIPPED_PAGES >>$LOG
echo -n "Procedure Complete in " >>$LOG
printf '%dh:%dm:%ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60)) >>$LOG
}
#################################################################################
# Print Info
#################################################################################
print_info() {
if [ ${MISCNT} -gt 0 ]
then
echo "Missing File List:"
cat $MISFILE
fi
echo "-----------------------------------------------------------------------------"
if [ ${skipped_cnt} -gt 0 ]
then
echo "Skipped Pages File List:"
cat $SKIPPED_PAGES
fi
echo "-----------------------------------------------------------------------------"
}
upload_build_to_web() {
echo "sitecopy lyonscomputer.com.au # to list changes to live site"
echo "sitecopy --update lyonscomputer.com.au # to sync the live site to local files"
while true
do
read -p "Do you wish to run \"sitecopy --update lyonscomputer.com.au?\"" ANS
case $ANS in
[Yy]* ) sitecopy --update lyonscomputer.com.au ; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
}
###################################################################################
# End of fuction section
########################################################################################
if [ "$operation" == "build" ];then initialise_build ;fi
if [ "$operation" == "build" ];then rotate_log ;fi
if [ "$operation" == "build" ];then build_header;fi
if [ "$operation" == "build" ];then build_top_index;fi
if [ "$operation" == "build" ];then build_contents;fi
if [ "$operation" == "build" ];then build_footer;fi
if [ "$operation" == "build" ];then install_build;fi
# print_info # Print the detailed logged files
if [ "$operation" == "build" ];then upload_build_to_web; fi
#################### End of File #########################