| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 5 | cd "$ROOT" |
| 6 | |
| 7 | BUILD_DEB=false |
| 8 | SKIP_TESTS=false |
| 9 | INSTALL=false |
| 10 | |
| 11 | for arg in "$@"; do |
| 12 | case "$arg" in |
| 13 | --deb) BUILD_DEB=true ;; |
| 14 | --skip-tests) SKIP_TESTS=true ;; |
| 15 | --install) INSTALL=true ;; |
| 16 | -h|--help) |
| 17 | echo "Usage: $0 [--deb] [--skip-tests] [--install]" |
| 18 | echo "Builds target/release/posprint (and optionally a .deb)." |
| 19 | echo " --deb also build a Debian package with cargo-deb" |
| 20 | echo " --install install the .deb with apt after building (implies --deb)" |
| 21 | echo "Without --install, prompts interactively for .deb install when stdin is a TTY." |
| 22 | exit 0 |
| 23 | ;; |
| 24 | *) echo "Unknown option: $arg"; exit 1 ;; |
| 25 | esac |
| 26 | done |
| 27 | |
| 28 | if [[ "$INSTALL" == "true" ]]; then |
| 29 | BUILD_DEB=true |
| 30 | fi |
| 31 | |
| 32 | want_install() { |
| 33 | if [[ "$INSTALL" == "true" ]]; then |
| 34 | return 0 |
| 35 | fi |
| 36 | if [[ ! -t 0 ]]; then |
| 37 | return 1 |
| 38 | fi |
| 39 | local answer |
| 40 | read -r -p "Install the .deb now with apt? [y/N] " answer |
| 41 | [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]] |
| 42 | } |
| 43 | |
| 44 | echo "Building posprint for Linux (x86_64)..." |
| 45 | |
| 46 | if [[ "$SKIP_TESTS" != "true" ]]; then |
| 47 | cargo test |
| 48 | fi |
| 49 | |
| 50 | cargo build --release |
| 51 | BIN="$ROOT/target/release/posprint" |
| 52 | echo "OK: $BIN" |
| 53 | ls -lh "$BIN" |
| 54 | |
| 55 | if [[ "$BUILD_DEB" == "true" ]]; then |
| 56 | if ! command -v cargo-deb &>/dev/null; then |
| 57 | echo "Installing cargo-deb..." |
| 58 | cargo install cargo-deb --locked |
| 59 | fi |
| 60 | cargo deb |
| 61 | shopt -s nullglob |
| 62 | DEBS=("$ROOT"/target/debian/*.deb) |
| 63 | shopt -u nullglob |
| 64 | if [[ ${#DEBS[@]} -eq 0 ]]; then |
| 65 | echo "No .deb produced." |
| 66 | exit 1 |
| 67 | fi |
| 68 | echo "Debian packages:" |
| 69 | ls -lh "${DEBS[@]}" |
| 70 | |
| 71 | if want_install; then |
| 72 | echo "Installing with apt..." |
| 73 | # Relative path so apt treats it as a local package file. |
| 74 | sudo apt-get install -y "${DEBS[@]}" |
| 75 | echo "Installed." |
| 76 | fi |
| 77 | fi |