about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorPadeso <padeso@duck.com>2024-11-12 13:20:19 +0000
committerPadeso <padeso@duck.com>2024-11-12 13:20:19 +0000
commitb672d10b67204ddd9271f4cb8582dae57d14032e (patch)
tree8a6fc19057927f6b550316a0edbe6ecd3376cde3
downloadnon-empty-sites-b672d10b67204ddd9271f4cb8582dae57d14032e.tar.gz
Initial commit
-rw-r--r--README.md19
-rwxr-xr-xnon-empty-sites.cgi31
-rw-r--r--non-empty-sites.css10
-rwxr-xr-xsite-scan35
4 files changed, 95 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d566408
--- /dev/null
+++ b/README.md
@@ -0,0 +1,19 @@
+# Non-empty-sites
+
+List apparently non-empty sites at tilde.institute.
+
+## site-scan
+```
+Usage: site-scan [outfile] [backup-count]
+```
+A Korn Shell script that looks at each directory in `/var/www/users` and writes its name into a file if it is not empty. 
+
+The output file is named `sites` by default, but may be given a different name on the command line. 
+
+By default, the script makes up to five backup copies of the output file. The number of backups can also be specified on the command line.
+
+## non-empty-sites.cgi
+A Python script that generates a web page with links to the sites in the file generated by `site-scan.` As written, this script expects the list of sites to be in a file named `sites.`
+
+## non-empty-sites.css
+A stylesheet used by `non-empty-sites.cgi`. 
diff --git a/non-empty-sites.cgi b/non-empty-sites.cgi
new file mode 100755
index 0000000..75d60a5
--- /dev/null
+++ b/non-empty-sites.cgi
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import sys
+import os
+from datetime import datetime
+
+sites_file = "./sites"
+mtime = datetime.fromtimestamp(os.path.getmtime(sites_file)).strftime("%Y-%m-%d %H:%M")
+
+print("Content-Type: text/html; charset=utf-8\r\n");
+print("\r\n\r\n");
+
+print("<!doctype html>");
+print('<html lang="en">');
+print("<head>");
+print("<meta charset=\"utf-8\">");
+print("<title>Non-Empty Sites</title>");
+print("<link href='non-empty-sites.css' rel='stylesheet' type='text/css'");
+print("</head>");
+print("<body>");
+print("<h1>Apparently non-empty sites at tilde.institute</h1>");
+print('<p>As of {}, the following <b>tilde.institute</b> users had at least one file in their <i>public_html</i> folder, so you might find some sort of web site there.</p>'.format(mtime))
+print("<ul class='sites'>")
+with open(sites_file) as file:
+    for line in file:
+        l = line.rstrip()
+        print("<li><a href='https://% s.tilde.institute/'>%s</a></li>" % (l, l))
+print("</ul>")
+print("<p>I put this together to learn something about writing shell and CGI scripts on tilde.institue.</p>")
+print("</body>");
+print("</html>");
diff --git a/non-empty-sites.css b/non-empty-sites.css
new file mode 100644
index 0000000..857c1fc
--- /dev/null
+++ b/non-empty-sites.css
@@ -0,0 +1,10 @@
+:root {
+  font-family: sans-serif;
+}
+body {
+  background-color: aliceblue;
+}
+.sites {
+  column-width: 10rem;
+}
+
diff --git a/site-scan b/site-scan
new file mode 100755
index 0000000..b4b8657
--- /dev/null
+++ b/site-scan
@@ -0,0 +1,35 @@
+# This script looks at each directory in /var/www/users and
+# writes its name into a file name `sites` if it is not empty.
+
+# Helper function to make a rolling backup of a file.
+function roll_files {
+    BASE=$1
+    INDEX=$(($2-1))
+    while [[ $INDEX -ge 1 ]]; 
+    do 
+        if [ -f "$BASE.$INDEX" ]; then
+            NEXT=$((INDEX+1))
+            mv "$BASE.$INDEX" "$BASE.$NEXT"
+        fi
+        INDEX=$((COUNT-1))
+    done
+    if [ -f $BASE ]; then
+        mv "$BASE" "$BASE.1"
+    fi
+}
+
+# Helper function to determine if a directory is empty
+function is_empty {
+    [ -z "$(find $1/. -maxdepth 1 -name . -o -print 2>/dev/null | head -n 1)" ]
+}
+
+FILE=${1:-sites}
+BU_COUNT=${2:-5}
+
+roll_files $FILE $BU_COUNT 
+
+for d in /var/www/users/*; do
+    if [ -d $d ] && $( ! is_empty $d ); then 
+        print $( echo $d | sed -e 's?^.*/??' ) >> $FILE
+    fi
+done