gadgetyWebServer/espify.sh
2018-09-14 12:49:36 +02:00

74 lines
1.6 KiB
Bash
Executable file

#!/bin/bash
print_usage()
{
echo "Usage: ./espify.sh <dir> [<find-filter-args...>]"
echo "Example: ./espify.sh /home/user/mywebsite"
echo "Example: ./espify.sh dist/ -not -name foo.html"
}
get_var_name()
{
echo -e "static_$(echo -e $1 | tr -d '_' | tr '/.~-' '_')"
}
if [ "$1" == "" ]; then
echo "Error: missing content path." >&2
print_usage >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "Error: \`$1' is not a directory." >&2
print_usage >&2
exit 2
fi
prefix="$(echo $1 | sed -e 's/\/*$//')"
prefix_len=${#prefix}
shift 1 # Eat first argument
flist="$(find -L $prefix -type f \( -name '*.js' -or -name '*.html' -or -name '*.css' -or -name 'favicon.ico' \) $@ | cut -c $[prefix_len+2]-)"
cat <<EOF
#include "gadgety.h"
#include <ESP8266WebServer.h>
EOF
for i in $flist; do
echo -e "unsigned char PROGMEM $(get_var_name $i)[] = {"
gzip -c "$prefix/$i" | xxd -i -c 16
echo -e "};\n"
done
cat <<EOF
static ESP8266WebServer *_server = 0;
#define SERVE_STATIC(p) static void serve_ ## p (void) { \\
_server->sendHeader("Content-Encoding", "gzip"); \\
_server->send_P(200, "text/html", (const char*)p, sizeof(p)); \\
}
static void redirect_index(void)
{
_server->sendHeader("Location", "/");
_server->send(301);
}
EOF
for i in $flist; do
echo -e "SERVE_STATIC($(get_var_name $i))"
done
echo -e "\nvoid gadgety_static_init_server(ESP8266WebServer* server)\n{"
echo -e " _server = server;"
echo -e " server->onNotFound(redirect_index);"
echo -e " server->on(\"/\", serve_static_index_html);"
for i in $flist; do
echo -e " server->on(\"/$i\", serve_$(get_var_name $i));"
done
echo -e "}"