engine

website and gopher hole static engine
git clone git://kh1b.org/engine
Log | Files | Refs | README

indexof.sh (1977B)


      1 #!/bin/bash
      2 
      3 generate_index() {
      4     local dir="$1"
      5     local rel_path="$2"
      6 
      7     local output="$dir/index.html"
      8     echo "Generating $output"
      9     {
     10         echo "<!DOCTYPE html>"
     11         echo "<html>"
     12         echo "<head>"
     13         echo "<title>Index of $(basename "$dir")</title>"
     14         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"${rel_path}style.css\" />"
     15         echo "</head>"
     16         echo "<body>"
     17         echo "<hr>"
     18         echo "<h1>Index of $(basename "$dir")</h1>"
     19         echo "<pre>"
     20         printf "%-50s %-20s %-10s\n" "Name" "Last Modified" "Size"
     21         echo "<hr>"
     22 
     23         if [ "$dir" != "." ]; then
     24             printf "<a href=\"../\">%-48s</a> %-20s %-10s\n" "../" "-" "-"
     25         fi
     26 
     27         for item in "$dir"/*; do
     28             local base_item=$(basename "$item")
     29             if [ "$base_item" = "index.html" ]; then
     30                 continue
     31             fi
     32             if [ "$dir" = "." ] && { [ "$base_item" = "Makefile" ] || [ "$base_item" = "config.mk" ] || [ "$base_item" = "style.css" ]; }; then
     33                 continue
     34             fi
     35 
     36             if [ -d "$item" ]; then
     37                 local date=$(stat -c '%y' "$item" | cut -d. -f1)
     38                 printf "<a href=\"%s/\">%-48s</a> %-20s %-10s\n" "$base_item" "$base_item/" "$date" "-"
     39             elif [ -f "$item" ]; then
     40                 local date=$(stat -c '%y' "$item" | cut -d. -f1)
     41                 local size=$(stat -c '%s' "$item")
     42                 printf "<a href=\"%s\">%-48s</a> %-20s %-10s\n" "$base_item" "$base_item" "$date" "$size"
     43             fi
     44         done
     45 
     46         echo "</pre>"
     47         echo "<hr>"
     48         echo "</body></html>"
     49     } > "$output"
     50 
     51     for subdir in "$dir"/*; do
     52         if [ -d "$subdir" ]; then
     53             generate_index "$subdir" "$rel_path../"
     54         fi
     55     done
     56 }
     57 
     58 if [ $# -eq 0 ]; then
     59     echo "Usage: $0 <directory>"
     60     exit 1
     61 fi
     62 
     63 if [ ! -d "$1" ]; then
     64     echo "Error: $1 is not a directory"
     65     exit 1
     66 fi
     67 
     68 generate_index "$1" "./"
     69