#!/bin/sh # Bundle www/ into one self-contained installer: dist/install-cabinchat.sh # Works on Linux, macOS and Termux. Usage: sh build.sh set -e cd "$(dirname "$0")" VERSION=$(tr -d ' \r\n' < www/VERSION) OUT=dist/install-cabinchat.sh DELIM=__CABINCHAT_EOF__ PAYLOAD=dist/.payload mkdir -p dist (cd www && find . -type f ! -path './img/*' ! -name '.*' | sort) | while IFS= read -r f; do f=${f#./} dir=$(dirname "$f") [ "$dir" = . ] || echo " mkdir -p \"\$STAGE/$dir\"" case $f in *.png|*.jpg|*.ico) # binary: octal escapes for the shell's printf builtin, 16 bytes per line echo " {" od -An -v -to1 "www/$f" | awk 'NF { s = ""; for (i = 1; i <= NF; i++) s = s "\\" $i; print "\tprintf '\''" s "'\''" }' echo " } > \"\$STAGE/$f\"" continue ;; esac if grep -q "^$DELIM\$" "www/$f"; then echo "build: $f contains the line $DELIM - cannot embed it" >&2 exit 1 fi echo " cat > \"\$STAGE/$f\" <<'$DELIM'" cat "www/$f" [ -z "$(tail -c 1 "www/$f")" ] || echo echo "$DELIM" done > "$PAYLOAD" awk -v v="$VERSION" -v p="$PAYLOAD" ' $0 == "@@PAYLOAD@@" { while ((getline l < p) > 0) print l; next } { gsub(/@@VERSION@@/, v); print } ' installer.sh.in > "$OUT" rm -f "$PAYLOAD" chmod 755 "$OUT" sh -n "$OUT" echo "built $OUT (Cabin Chat $VERSION, $(wc -c < "$OUT" | tr -d ' ') bytes)"