#!/usr/bin/env bash # Make sure the current directory is the location of this script to simplify matters cd "$(dirname $(readlink -f $0))"; ################ ### Settings ### ################ # The name of this project project_name="Msc Summer Project"; # The path to the lantern build engine git submodule lantern_path="./lantern-build-engine"; ### # Custom Settings ### # Put any custom settings here. ############################################################################### # Check out the lantern git submodule if needed if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi source "${lantern_path}/lantern.sh"; if [[ "$#" -lt 1 ]]; then echo -e "${FBLE}${project_name}${RS} build script"; echo -e " by Starbeamrainbowlabs"; echo -e "${LC}Powered by the lantern build engine, v${version}${RS}"; echo -e ""; echo -e "${CSECTION}Usage${RS}"; echo -e " ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ..."; echo -e ""; echo -e "${CSECTION}Available actions${RS}"; echo -e " ${CACTION}setup${RS} - Perform initial setup"; echo -e " ${CACTION}server-ttn${RS} - Execute the Node.js TTN receiver."; echo -e " ${CACTION}client${RS} - Build the client-side code."; echo -e " ${CACTION}client-watch${RS} - Auto-rebuild the client-side code on modification."; echo -e " ${CACTION}server${RS} - Start a temporary web server for the browser-based web interface."; echo -e " ${CACTION}server-stop${RS} - Stop a temporary web server."; echo -e ""; echo -e "${CSECTION}Extra development actions${RS}"; echo -e " ${CACTION}render${RS} - Render the report"; echo -e " ${CACTION}geojson-debug${RS} - Generate some GeoJSON from the raw readings for debugging purposes (paste into geojson.io)"; echo -e ""; exit 1; fi ############################################################################### # ██████ ██████ ███ ███ ███ ███ ███████ ███ ██ ████████ # ██ ██ ██ ████ ████ ████ ████ ██ ████ ██ ██ # ██ ██ ██ ██ ████ ██ ██ ████ ██ █████ ██ ██ ██ ██ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ██████ ██████ ██ ██ ██ ██ ███████ ██ ████ ██ # Toggles commenting and uncommenting lines in a file that contain a specific # substring. Checks for word boundaries either side of the substring. # From https://stackoverflow.com/a/24901636/1460422 # $1 - Filename # $2 - Search string comment_toggle() { filename="${1}"; search_string="${2}"; awk -v commentId='//' -v word="${search_string}" ' $0 ~ "(^|[[:punct:][:space:]])" word "($|[[:punct:][:space:]])" { if (match($0, "^[[:space:]]*" commentId)) $0 = substr($0, RSTART + RLENGTH) else $0 = commentId $0 } { print }' "${filename}" > tmpfile.$$ && mv tmpfile.$$ "${filename}" } task_setup() { stage_begin "Setting up"; task_begin "Checking Environment"; check_command git true; check_command awk true; check_command inotifywait true optional; if [[ "$?" -ne "0" ]]; then echo "${HC}inotifywait${RS} is required to auto-rebuild on when the client-side code changes."; fi check_command pdflatex true optional; if [[ "$?" -ne "0" ]]; then echo "${HC}pdflatex${RS} is required to render the reports."; fi check_command bibtex true optional; if [[ "$?" -ne "0" ]]; then echo "${HC}bibtex${RS} is required to render the reports."; fi task_end $?; task_begin "Initialising submodules"; git submodule update --init; task_end $?; task_begin "Preconfiguring libraries"; config_file_directory="./iot/libraries/arduino-lmic/src/lmic/"; config_file_name="config.h"; cd "${config_file_directory}"; git reset --hard; # Disable OTAA comment_toggle "${config_file_name}" "#define DISABLE_JOIN"; # Disable class b stuff comment_toggle "${config_file_name}" "#define DISABLE_PING"; comment_toggle "${config_file_name}" "#define DISABLE_BEACONS"; # Disable other misc. stuff we're not likely to use comment_toggle "${config_file_name}" "#define DISABLE_MCMD_DCAP_REQ"; # Duty cycle cap - won't work anyway 'cause we're shutting down in between comment_toggle "${config_file_name}" "#define DISABLE_MCMD_DN2P_SET"; # Receiving stuff # echo "#define DISABLE_JOIN" >>"${config_file_name}"; # echo "#define DISABLE_PING" >>"${config_file_name}"; # echo "#define DISABLE_BEACONS" >>"${config_file_name}"; cd -; task_end $?; task_begin "Installing server dependencies"; npm install; exit_code="${?}"; task_end ${exit_code}; stage_end 0; } # ██ ██ ███████ ████████ ███████ ███ ██ ███████ ██████ # ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ # ██ ██ ███████ ██ █████ ██ ██ ██ █████ ██████ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ███████ ██ ███████ ██ ███████ ██ ████ ███████ ██ ██ task_server-ttn() { execute node --experimental-modules ./server/index.mjs ttn-app-server; } # ██████ ██ ██ ███████ ███ ██ ████████ # ██ ██ ██ ██ ████ ██ ██ # ██ ██ ██ █████ ██ ██ ██ ██ # ██ ██ ██ ██ ██ ██ ██ ██ # ██████ ███████ ██ ███████ ██ ████ ██ task_client() { task_begin "Packaging Javascript"; execute node_modules/rollup/bin/rollup --sourcemap --config rollup.main.config.js; execute node_modules/rollup/bin/rollup --sourcemap --config rollup.worker.config.js; task_end $? "Error: rollup packing failed!"; task_begin "Copying html"; execute cp client_src/index.html "app/"; task_end $?; } task_client-watch() { set_title "Client Watcher"; # execute node_modules/rollup/bin/rollup --watch --sourcemap --config rollup.config.js & echo -e "Watching for changes."; while :; do # : = infinite loop # Wait for an update # inotifywait's non-0 exit code forces an exit for some reason :-/ inotifywait -qr --event modify --format '%:e %f' client_src common rollup.*.config.js; task_begin "Copying html"; execute cp client_src/index.html "app/"; task_end $?; stage_begin "Rebuilding client code"; set +e; tasks_run client; set -e; stage_end $?; done } # ██████ ███████ ██ ██ ███████ ███████ ██████ ██ ██ ███████ ██████ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ██ ██ █████ ██ ██ █████ ███████ █████ ██████ ██ ██ █████ ██████ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ██████ ███████ ████ ███████ ███████ ██ ██ ████ ███████ ██ ██ task_server() { if [ ! -f "app/index.html" ]; then echo "No client-side code detected, running build script"; tasks_run client; fi task_begin "Starting development server"; php -S "[::1]:40382" -t "app" & exit_code=$?; [[ "${exit_code}" -eq "0" ]] && echo $! >/tmp/summer-project-dev-server.pid; task_end $?; # Should be 0 unless php died for some reason sleep 1; } task_server-stop() { task_begin "Stopping development server"; if [ ! -f "/tmp/summer-project-dev-server.pid" ]; then echo -e "${HC}${FRED}Error: The development server doesn't appear to be running, so it can't be stopped. Have you tried running ./build dev-server?${RESET}"; return 1; fi kill "$(cat /tmp/summer-project-dev-server.pid)"; rm /tmp/summer-project-dev-server.pid; task_end $?; } task_geojson-debug() { sqlite3 lorawan.sqlite 'SELECT readings.latitude, readings.longitude, rssis.rssi FROM readings LEFT JOIN rssis ON readings.id = rssis.reading_id ORDER BY readings.latitude,readings.longitude;' | perl -pe 'chomp if eof' | jq --raw-input --slurp 'split("\n") | map(split("|") | map(if . == "" then null else tonumber end)) | map({type: "Feature", geometry: { type: "Point", "coordinates": [ .[1], .[0] ] }, properties: { "rssi": .[2], "marker-color": (if .[2] == null then "#dd0707" else "#04a104" end), "marker-symbol": "circle" }}) | { type: "FeatureCollection", features: . }' } ############################################################################### # ██████ ███████ ██████ ██████ ██████ ████████ ███████ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ██████ █████ ██████ ██ ██ ██████ ██ ███████ # ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ # ██ ██ ███████ ██ ██████ ██ ██ ██ ███████ task_render() { _render-latex-pdf "Reports/Initial-Report/Initial-Report.tex"; } # $1 - Location of top-level LaTeX file _render-latex-pdf() { if [[ ! -f "$1" ]]; then task_end 1 "Error: Couldn't find '$1'"; fi task_begin "Entering directory"; latex_filename="$(basename "$1")"; latex_directory="$(dirname "$1")"; execute cd "${latex_directory}"; execute echo "${PWD}"; task_end $? "Failed to enter directory (does it exist?)"; task_begin "Cleaning up"; find -iname "*.aux" -delete; # Ref: https://tex.stackexchange.com/q/381057 find -iname "*.bbl" -delete; find -iname "*.blg" -delete; find -iname "*.out" -delete; task_end $? "Error: Failed to clean up after last build"; # task_begin "Rendering images"; # # FUTURE: Do this in paralell? # for svg_filename in $(find "images/" -type f -iname "*.svg"); do # execute inkscape -e ${svg_filename%%.svg}.png ${svg_filename}; # exit_code=$?; # [[ "${exit_code}" -eq 0 ]] || break; # done # task_end "${exit_code}"; task_begin "Building Report"; set -e; execute pdflatex --output-directory=. "${latex_filename}"; execute bibtex "${latex_filename%.*}"; execute pdflatex --output-directory=. "${latex_filename}"; execute pdflatex --output-directory=. "${latex_filename}"; execute bibtex "${latex_filename%.*}"; execute pdflatex --output-directory=. "${latex_filename}"; execute pdflatex --output-directory=. "${latex_filename}"; set +e; task_end $? "Error: Failed to build report"; task_begin "Moving report"; execute mv *.pdf ..; task_end $? "Failed to move report"; cd -; } ############################################################################### tasks_run $@;