2019-05-03 12:59:37 +00:00
#!/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";
2019-08-20 14:26:42 +00:00
echo -e "${LC}Powered by the \e[38;5;11mlantern \e[38;5;117mbuild \e[38;5;215mengine${RS}${LC}, v${version}${RS}";
2019-05-03 12:59:37 +00:00
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}";
2019-07-25 12:17:23 +00:00
echo -e " ${CACTION}setup${RS} - Perform initial setup";
2019-08-01 11:33:33 +00:00
echo -e " ${CACTION}ttn-listener${RS} - Execute the Node.js TTN receiver.";
echo -e " ${CACTION}process-data${RS} - Fold the data in 'DATA.TSV' into the database.";
echo -e " ${CACTION}train-ai${RS} - Train the AI(s).";
echo -e " ${CACTION}server${RS} - Start a temporary web server for the web interface.";
echo -e " ${CACTION}server-stop${RS} - Stop a temporary web server.";
2019-08-01 11:28:28 +00:00
echo -e "";
echo -e "${CSECTION}Extra development actions${RS}";
2019-08-01 11:33:33 +00:00
echo -e " ${CACTION}client${RS} - Build the client-side code.";
echo -e " ${CACTION}client-watch${RS} - Auto-rebuild the client-side code on modification.";
2019-08-01 11:28:28 +00:00
echo -e " ${CACTION}render${RS} - Render the report";
2019-07-31 14:11:40 +00:00
echo -e " ${CACTION}geojson-debug${RS} - Generate some GeoJSON from the raw readings for debugging purposes (paste into geojson.io)";
2019-05-03 12:59:37 +00:00
echo -e "";
exit 1;
fi
###############################################################################
2019-07-19 14:36:21 +00:00
# ██████ ██████ ███ ███ ███ ███ ███████ ███ ██ ████████
# ██ ██ ██ ████ ████ ████ ████ ██ ████ ██ ██
# ██ ██ ██ ██ ████ ██ ██ ████ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ██████ ██ ██ ██ ██ ███████ ██ ████ ██
2019-06-28 12:33:16 +00:00
# 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}"
}
2019-05-03 12:59:37 +00:00
task_setup() {
2019-06-28 12:33:16 +00:00
stage_begin "Setting up";
2019-05-03 12:59:37 +00:00
2019-06-28 12:33:16 +00:00
task_begin "Checking Environment";
2019-05-03 12:59:37 +00:00
check_command git true;
2019-06-28 12:33:16 +00:00
check_command awk true;
2019-07-19 14:36:21 +00:00
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
2019-07-16 14:42:24 +00:00
check_command pdflatex true optional;
2019-07-16 15:13:53 +00:00
if [[ "$?" -ne "0" ]]; then echo "${HC}pdflatex${RS} is required to render the reports."; fi
2019-07-16 15:14:32 +00:00
check_command bibtex true optional;
if [[ "$?" -ne "0" ]]; then echo "${HC}bibtex${RS} is required to render the reports."; fi
2019-06-28 12:33:16 +00:00
task_end $?;
2019-05-03 12:59:37 +00:00
2019-06-28 12:33:16 +00:00
task_begin "Initialising submodules";
2019-05-03 12:59:37 +00:00
git submodule update --init;
2019-06-28 12:33:16 +00:00
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
2019-07-16 15:14:55 +00:00
comment_toggle "${config_file_name}" "#define DISABLE_MCMD_DCAP_REQ"; # Duty cycle cap - won't work anyway 'cause we're shutting down in between
2019-06-28 12:33:16 +00:00
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 $?;
2019-05-03 12:59:37 +00:00
2019-07-04 16:18:38 +00:00
task_begin "Installing server dependencies";
npm install;
exit_code="${?}";
task_end ${exit_code};
2019-06-28 12:33:16 +00:00
stage_end 0;
2019-05-03 12:59:37 +00:00
}
2019-07-19 14:36:21 +00:00
# ██ ██ ███████ ████████ ███████ ███ ██ ███████ ██████
# ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
# ██ ██ ███████ ██ █████ ██ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ███████ ██ ███████ ██ ███████ ██ ████ ███████ ██ ██
2019-08-01 11:33:33 +00:00
task_ttn-listener() {
execute ./server.sh ttn-app-server;
}
task_train-ai() {
execute ./server.sh train-ai;
}
task_process-data() {
execute ./server.sh process-data DATA.TSV;
2019-07-04 16:18:38 +00:00
}
2019-07-19 14:36:21 +00:00
# ██████ ██ ██ ███████ ███ ██ ████████
# ██ ██ ██ ██ ████ ██ ██
# ██ ██ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ███████ ██ ████ ██
task_client() {
task_begin "Packaging Javascript";
2019-07-30 17:38:44 +00:00
execute node_modules/rollup/bin/rollup --sourcemap --config rollup.main.config.js;
execute node_modules/rollup/bin/rollup --sourcemap --config rollup.worker.config.js;
2019-07-19 14:36:21 +00:00
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";
2019-07-30 17:38:44 +00:00
# execute node_modules/rollup/bin/rollup --watch --sourcemap --config rollup.config.js &
2019-07-25 13:33:02 +00:00
2019-07-19 14:36:21 +00:00
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 :-/
2019-07-30 17:46:07 +00:00
inotifywait -qr --event modify --format '%:e %f' client_src common rollup.*.config.js;
2019-07-19 14:36:21 +00:00
2019-07-25 13:33:02 +00:00
task_begin "Copying html";
execute cp client_src/index.html "app/";
task_end $?;
2019-07-30 17:38:44 +00:00
stage_begin "Rebuilding client code";
set +e; tasks_run client; set -e;
stage_end $?;
2019-07-19 14:36:21 +00:00
done
}
2019-07-22 14:47:13 +00:00
# ██████ ███████ ██ ██ ███████ ███████ ██████ ██ ██ ███████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ █████ ██ ██ █████ ███████ █████ ██████ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ████ ███████ ███████ ██ ██ ████ ███████ ██ ██
2019-08-01 11:28:28 +00:00
task_server() {
if [ ! -f "app/index.html" ]; then
echo "No client-side code detected, running build script";
tasks_run client;
fi
2019-07-22 14:47:13 +00:00
task_begin "Starting development server";
2019-08-01 15:30:13 +00:00
php -S "[::]:40382" -t "app" &
exit_code_a=$?;
pid_a=$!;
php -S "127.0.0.1:40382" -t "app" &
exit_code_b=$?;
pid_b=$!;
[[ "${exit_code_a}" -eq "0" ]] && echo "${pid_a}" >/tmp/summer-project-dev-server-v6.pid;
[[ "${exit_code_b}" -eq "0" ]] && echo "${pid_b}" >/tmp/summer-project-dev-server-v4.pid;
2019-07-22 14:47:13 +00:00
task_end $?; # Should be 0 unless php died for some reason
sleep 1;
}
2019-08-01 11:28:28 +00:00
task_server-stop() {
2019-07-22 14:47:13 +00:00
task_begin "Stopping development server";
2019-08-01 15:30:13 +00:00
if [ ! -f "/tmp/summer-project-dev-server-v6.pid" ]; then
2019-07-22 14:47:13 +00:00
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
2019-08-01 15:30:13 +00:00
kill "$(cat /tmp/summer-project-dev-server-v4.pid)";
kill "$(cat /tmp/summer-project-dev-server-v6.pid)";
rm /tmp/summer-project-dev-server-v[46].pid;
2019-07-22 14:47:13 +00:00
task_end $?;
}
2019-07-31 15:06:58 +00:00
task_geojson-debug() {
2019-07-31 14:11:40 +00:00
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: . }'
}
2019-07-22 14:47:13 +00:00
2019-07-04 16:18:38 +00:00
###############################################################################
2019-07-19 14:36:21 +00:00
# ██████ ███████ ██████ ██████ ██████ ████████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ █████ ██████ ██ ██ ██████ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ███████ ██ ██████ ██ ██ ██ ███████
2019-05-03 12:59:37 +00:00
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 $@;