cosmic/bin/cosmic-web

173 lines
5.9 KiB
Bash
Executable File

#!/bin/sh
entry_index () {
line="$1"
index=$2
log=$(printf "%s" "$line" | awk -F'\t' '{print $2}')
loghtml=$(printf "%s" "$log" | sed 's/\.[^.]*$//')
logdir=$(dirname "$log")
title=$(printf "%s" "$line" | awk -F'\t' '{print $1}' | sed 's|^.||')
# print link in listings
if [ $index -lt 20 ]; then
printf "<a href=\"%s.html\">>> %s</a>\\n" "$loghtml" "$title" >> "${file_html}"
fi
printf "<a href=\"%s.html\">>> %s</a>\\n" "$loghtml" "$title" >> "${log_html}"
# create entry
entry_html="${html_dir}${loghtml}.html"
mkdir -p "${html_dir}${logdir}"
cat "$web_header_html" > "${entry_html}"
{
printf " <title>%s</title>\\n" "$title"
printf " <link rel=\"canonical\" href=\"https://cosmic.voyage%s.html\">\\n" "$loghtml"
printf "</head>\\n<body>\\n<div class=\"page-wrapper\"><pre class=\"inner-wrapper\">\\n"
printf "<a href=\"/log\">&lt;&lt; BACK TO RELAY ONE LOG</a>\\n\\n\\n"
sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g' "${gopher_dir}${log}"
# close up the entry footer
printf "</pre></div></body></html>"
} >> "${entry_html}"
}
run_user=$(id -u)
if [ "$run_user" -eq 0 ]; then
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
file_html="/var/www/html/index.html"
log_html="/var/www/html/log/index.html"
web_header_html="${SCRIPTPATH}/../templates/webheader.tmpl"
ships_html="/var/www/html/ships/index.html"
error_html="/var/www/html/error.html"
html_dir="/var/www/html"
gopher_dir="/var/gopher"
# Clear web folder
rm -rf "${html_dir:-/var/www/html}/*"
# Generate RSS
# shellcheck source=cosmic-rss
. "${SCRIPTPATH}/cosmic-rss"
# prep directories
mkdir -p "$(dirname "${log_html}")"
# Add standard header
cat "$web_header_html" > "${file_html}"
cat "$web_header_html" > "${log_html}"
# Custom header elements and body start
{
printf "<title>Cosmic Voyage</title>"
printf "<link rel=\"canonical\" href=\"https://cosmic.voyage\">"
printf "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Cosmic Voyage\" href=\"/rss.xml\">"
printf "</head>"
printf "<body>"
printf "<div class=\"page-wrapper\">"
printf "<pre class=\"inner-wrapper\">"
# Intro text
cat "${gopher_dir}/intro.gophermap"
# Ship listings
printf "\\n<a href=\"/log\">>> Complete Transmission Log</a>\\n"
printf "<a href=\"/ships\">>> Ships, Colonies, Outposts</a>\\n"
printf "<a href=\"/rss.xml\">>> RSS Feed</a>\\n\\n"
# Logs
printf "Most recent (20) log entries:\\n"
} >> "${file_html}"
# Custom header elements and body start
{
printf "<title>Cosmic Voyage - Transmission Log</title>"
printf "<link rel=\"canonical\" href=\"https://cosmic.voyage/log\">"
printf "</head>"
printf "<body>"
printf "<div class=\"page-wrapper\">"
printf "<pre class=\"inner-wrapper\">"
printf "<a href=\"/\">&lt;&lt; BACK TO RELAY ONE</a>\\n\\n"
# Intro text
cat "${gopher_dir}/log/intro.gophermap"
} >> "${log_html}"
# Loop through listings gophermap
loop=0
while read -r line; do
loop=$((loop+1))
entry_index "$line" $loop
done < "${gopher_dir}/listing.gophermap"
# footer
{
printf "</pre>\\n"
printf "</div>\\n"
printf "</body>\\n"
printf "</html>"
} | tee -a "${log_html}" >> "${file_html}"
# Generate ship pages
mkdir -p "${html_dir}/ships"
# Add header info to html output
cat "$web_header_html" > "${ships_html}"
# Custom header elements and body start
{
printf "<title>Cosmic Voyage - Ships</title>"
printf "<link rel=\"canonical\" href=\"https://cosmic.voyage/ships\">"
printf "</head>"
printf "<body>"
printf "<div class=\"page-wrapper\">"
printf "<pre class=\"inner-wrapper\">"
printf "<a href=\"/\">&lt;&lt; BACK TO RELAY ONE</a>\\n"
ship_intro="${gopher_dir}/ships/ships.gophermap"
if [ -f "$ship_intro" ]; then
cat "$ship_intro"
printf "\\n"
fi
} >> "${ships_html}"
# Add header info to html output
find "${gopher_dir}/" -maxdepth 1 ! -path "${gopher_dir}/" ! -path "${gopher_dir}/ships" ! -path "${gopher_dir}/log" -type d -print | sed 's|/var/gopher/||' | sort | while read -r ship
do
entry_num=$(grep -c "^0${ship}" "${gopher_dir}/listing.gophermap")
if [ "$entry_num" != "0" ]; then
printf "<a href=\"/ships/%s/\">>> %s (%s)</a>\\n" "$ship" "$ship" "$entry_num" >> "$ships_html"
# Create individual ship log page
ship_html="${html_dir}/ships/${ship}/index.html"
mkdir -p "${html_dir}/ships/${ship}"
# Add header info to html output
cat "$web_header_html" > "${ship_html}"
# Custom header elements and body start
{
printf "<title>Cosmic Voyage - %s</title>" "$ship"
printf "<link rel=\"canonical\" href=\"https://cosmic.voyage/ships/%s\">" "$ship"
printf "</head>"
printf "<body>"
printf "<div class=\"page-wrapper\">"
printf "<pre class=\"inner-wrapper\">"
# Contents
printf "<a href=\"/ships\">&lt;&lt; BACK TO RELAY ONE SHIP LIST</a>\\n\\n\\n"
desc="${gopher_dir}/${ship}/.description"
if [ -f "$desc" ]; then
cat "$desc"
printf "\\n"
fi
printf "%s - Ship Log\\n" "$ship"
grep "^0${ship}" "${gopher_dir}/listing.gophermap" | sed "s|0${ship} - ||" | awk -F"\\t" '{f=$2; gsub(".txt", ".html", f); printf "<a href=\"%s\">>> %s</a>\n", f, $1}'
printf "</div></body></html>"
} >> "${ship_html}"
fi
done
# Footer
{
printf "</div>\\n"
printf "</body>\\n"
printf "</html>"
} >> "${ships_html}"
# Print error page
cat "$web_header_html" > "${error_html}"
{
printf "</head><body><div class=\"page-wrapper\"><pre class=\"inner-wrapper\"><a href=\"/\">&lt;&lt; BACK TO RELAY ONE</a>"
printf "\\n\\n\\nERROR. TRANSMISSION NOT FOUND.</pre></div></body></html>"
} >> "${error_html}"
# copy favicon
cp "${SCRIPTPATH}/../files/favicon.ico" "${html_dir}"
else
exec sudo "$0" "$@"
fi