1 #!/bin/bash
 2 
 3 ##################################################################
 4 # This script builds an html page that, upon load,
 5 # will redirect to another, random html page from a list.
 6 #
 7 # The list of html pages is built dynamically on each run.
 8 ##################################################################
 9 # made with <3 by ~inthreedee ( https://tilde.town/~inthreedee/ )
10 ##################################################################
11 
12 # Variables for the built html file
13 buildFile="$HOME/public_html/random.html"
14 
15 # Variables for the random html pages
16 pagesDir="pages"
17 pagesPath="$HOME/public_html/$pagesDir"
18 
19 # Sanity check
20 if [ ! -d "$pagesPath" ]; then
21   echo "Directory not found: $pagesPath"
22   exit 1
23 fi
24 
25 # Build the first half of the file
26 echo "Building $buildFile..."
27 
28 echo "<!DOCTYPE html>
29 <html>
30 <head>
31 <meta charset=\"utf-8\">
32 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
33 <title>Redirecting...</title>
34 <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">
35 </head>
36 <body>
37   <script>
38     var pageList = [" > "$buildFile"
39 
40 # Add the .html page names to the array
41 # Expects /home/[user]/public_html/[dir]/[page].html
42 # Where [page] is the 6th column delineated by /
43 for page in "$pagesPath"/*; do
44   if [ -f "$page" ] && [ "${page##*.}" == "html" ]; then
45     echo "      \"$( echo $page | awk -F '/' '{print $6}' )\"," >> "$buildFile"
46   fi
47 done
48 
49 # Build the rest of the file
50 echo "    ];
51     var randPage = Math.floor(Math.random() * pageList.length);
52     window.location = \"$pagesDir/\" + pageList[randPage];
53   </script>
54 </body>
55 </html>" >> "$buildFile"
56 
57 echo "Done."