aboutsummaryrefslogtreecommitdiff
path: root/platformio/relayctl/make_static.sh
blob: 879beb4668a309039038a47876903bc86708cced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash

#set -x
#set -e

DIR="$(dirname "$(realpath "$0")")"

fw_version="$(cat "$DIR/src/config.def.h" | grep "^#define FW_VERSION" | awk '{print $3}')"
header="$DIR/src/static.h"
source="$DIR/src/static.cpp"

[ -f "$header" ] && rm "$header"
[ -f "$source" ] && rm "$source"

is_minifyable() {
  local ext="$1"
  [ "$ext" = "html" ] || [ "$ext" = "css" ] || [ "$ext" = "js" ]
}

minify() {
  local ext="$1"
  local bin="$(realpath "$DIR"/../../tools/minify.js)"
  "$bin" --type "$ext"
}

# .h header
cat <<EOF >> "$header"
/**
 * This file is autogenerated with make_static.sh script
 */

#pragma once

#include <stdlib.h>

namespace homekit::files {

typedef struct {
    size_t size;
    const uint8_t* content;
} StaticFile;

EOF

cat <<EOF >> "$source"
/**
 * This file is autogenerated with make_static.sh script
 */

#include "static.h"

namespace homekit::files {

EOF

# loop over files
for ext in html js css ico; do
  for f in "$DIR"/static/*.$ext; do
    filename="$(basename "$f")"
    echo "processing ${filename}..."
    filename="${filename/./_}"

    # write .h
    echo "extern const StaticFile $filename;" >> "$header"

    # write .c
    {
      echo "static const uint8_t ${filename}_content[] PROGMEM = {"

      cat "$f" |
      ( [ "$ext" = "html" ] && sed "s/{version}/$fw_version/" || cat ) |
      ( is_minifyable "$ext" && minify "$ext" || cat ) |
      gzip |
      xxd -ps -c 16 |
      sed 's/.\{2\}/0x&, /g' |
      sed 's/^/    /' |
      sed 's/[ \t]*$//'

      echo "};"
      echo "const StaticFile $filename PROGMEM = {(sizeof(${filename}_content)/sizeof(${filename}_content[0])), ${filename}_content};"
      echo ""
    } >> "$source"
  done
done

# end of homekit::files
( echo ""; echo "}" ) >> "$header"
echo "}" >> "$source"