Skip to content

Instantly share code, notes, and snippets.

@twilight-sparkle-irl
Last active July 14, 2019 16:27
Show Gist options
  • Save twilight-sparkle-irl/569d28988daec3d919a5d881548cb2e2 to your computer and use it in GitHub Desktop.
Save twilight-sparkle-irl/569d28988daec3d919a5d881548cb2e2 to your computer and use it in GitHub Desktop.
i went too deep. creates index files for a directory on the drive, and continually updates them
#!/bin/bash
# requires inotify-tools and tree be installed
# --CONFIG--
INDEXDIR="$(pwd)" # change this to where you have the directory you want to index mounted
URL="https://generichost.example/~/person/assets" # change this to where the urls go
TITLE="stuff - %s" # %s represents the current directory
# below is the css to apply to the files. you can touch it if you wanna
# but i'd just recommend replacing the directory listing creator entirely lol
read -d '' listingCSS <<EOV
a{color:blue!important;}
a:hover{color:skyblue!important;background-color:transparent!important;}
.NORM:first-of-type,.NORM:first-of-type + br{display:none;}
.UP{font-size:90%;float:right;}
.UP:before {margin-right:0.5ch; padding-left:1ch;content:url(data:image/gif;base64,R0lGODlhEAAQAKL/ACEhITExMVpaWoSEhIyMjMDAwAAAAAAAACH5BAEAAAUALAAAAAAQABAAAANGWLrcDcBJYEyUi1aLi9AVIHTEZhBdUW4oabbDoMjKWo1ELAitEpiegDAwWghMg6HQYSIqHSDRsLewnYRFx3EzyEoIYHAjAQA7) }
.DIR:before { margin-right:0.5ch; content:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAMAAAD3n0w0AAAAElBMVEX/////zJnM//+ZZjMzMzMAAADCEvqoAAAAA3RSTlP//wDXyg1BAAAASElEQVR42s3KQQ6AQAhDUaXt/a/sQDrRJu7c+NmQB0e99B3lnqjT6cYx6zSIbV40n3D7psYMoBoz4w8/EdNYQsbGEjNxYSljXTEsA9O1pLTvAAAAAElFTkSuQmCC) }
.NORM:not(:first-child):before { margin-right:0.5ch; content:url(data:image/gif;base64,R0lGODlhFAAWAMIAAP///8z//5mZmTMzMwAAAAAAAAAAAAAAACH+TlRoaXMgYXJ0IGlzIGluIHRoZSBwdWJsaWMgZG9tYWluLiBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NQAh+QQBAAABACwAAAAAFAAWAAADUDi6vPEwDECrnSO+aTvPEddVIriN1wWJKDG48IlSRG0T8kwJvIBLOkvvxwoCfDnjkaisIIHNZdL4LAarUSm0iY12uUwvcdArm3mvyG3N/iUAADs=) }
.CHAR:before,.BLOCK:before,.LINK:before,.SOCK:before,.EXEC:before,.FIFO:before{ margin-right:0.5ch; content:url(data:image/gif;base64,R0lGODlhFAAWAMIAAP///8z//5mZmTMzMwAAAAAAAAAAAAAAACH+TlRoaXMgYXJ0IGlzIGluIHRoZSBwdWJsaWMgZG9tYWluLiBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NQAh+QQBAAABACwAAAAAFAAWAAADaDi6vPEwDECrnSO+aTvPEQcIAmGaIrhR5XmKgMq1LkoMN7ECrjDWp52r0iPpJJ0KjUAq7SxLE+sI+9V8vycFiM0iLb2O80s8JcfVJJTaGYrZYPNby5Ov6WolPD+XDJqAgSQ4EUCGQQEJADs=) }
.VERSION{display:none
body{max-width:666px}
EOV
# --/CONFIG--
listingCSS=$(echo $listingCSS|sed -e ':a;N;$!ba;s/\n/\\n/g')
URL=$(echo $URL | sed 's:/*$::')
INDEXDIR=$(echo -n $INDEXDIR | sed 's:/*$::';echo -n '/')
if [ ! -x "$(command -v tree)" ]; then
echo 'Error: tree is not installed.' >&2
exit 1
fi
if [ ! -x "$(command -v inotifywait)" ]; then
echo 'Error: inotify-tools is not installed.' >&2
exit 1
fi
function create_listing {
# you can run a sed script after tree or modify its code entirely if you're not happy with it
# this however is...hackageddon. i recommend replacing it with a better file directory lister
# seriously, don't bothter trying to figure out what this does. you will be hurt and upset
relative_dir=$(echo "$1"|sed -e "s~^$INDEXDIR~~;s~\$~/~;s~^~/~;s~^//\$~/~")
tree -T "$(printf "${TITLE}" $relative_dir)" --noreport --dirsfirst -H "." -S -C -L 1 --charset utf-8 "$1" -I "index.html" |
hexdump -v -e '/1 " %02X"'|sed -e "s/ E2 94 [89][0-9A-F]/ 2D/g;s/ 2D 2D 2D 20 3C/E2 80 A2 20 3C/g;s/ 3C 70 3E 0A 09 3C 62 72 3E 3C 62 72 3E 0A 09 3C 2F 70 3E//g;s/ 3C 62 72 3E 3C 62 72 3E/ 3C 62 72 3E/g"|xxd -r -ps |
sed -e '/\.NORM/,/<\/style>/{/--/!{/style/!d}}' |
awk "/-->/{\$0=\"${listingCSS}\\n\"\$0};1" |
cat > "$1/index.html"
if [ ! "${INDEXDIR}" == "$1" ]; then
sed -i -e "s$<hr>$<a class='UP' href='${URL}/'>[ROOT]</a> <a class='UP' href='..'>[..]</a><hr>$" "$1/index.html"
fi
}
echo "Doing initial startup index..."
find "${INDEXDIR}" -type d |
while read folder; do
echo "Indexed ${folder}"
create_listing "$folder"
done
echo "Done. Daemon is now running."
inotifywait -e modify -e attrib -e move \
-e create -e delete \
-r -m "${INDEXDIR}" \
--exclude "index.html$" \
--format "%w%f" |
while read event; do
echo "${event}"
if [ -d $event ]; then
create_listing "$event"
echo "Updated ${event}/index.html"
fi
create_listing "$(dirname $event)"
echo "Updated $(dirname $event)/index.html"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment