| 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 "On a TTY, missing build dependencies are offered for automatic install." |
| 22 | echo "Without --install, prompts interactively for .deb install when stdin is a TTY." |
| 23 | exit 0 |
| 24 | ;; |
| 25 | *) echo "Unknown option: $arg"; exit 1 ;; |
| 26 | esac |
| 27 | done |
| 28 | |
| 29 | if [[ "$INSTALL" == "true" ]]; then |
| 30 | BUILD_DEB=true |
| 31 | fi |
| 32 | |
| 33 | is_interactive() { |
| 34 | [[ -t 0 ]] |
| 35 | } |
| 36 | |
| 37 | ask_yes_no() { |
| 38 | local prompt="$1" |
| 39 | if ! is_interactive; then |
| 40 | return 1 |
| 41 | fi |
| 42 | local answer |
| 43 | read -r -p "$prompt [y/N] " answer |
| 44 | [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]] |
| 45 | } |
| 46 | |
| 47 | want_install() { |
| 48 | if [[ "$INSTALL" == "true" ]]; then |
| 49 | return 0 |
| 50 | fi |
| 51 | if ! is_interactive; then |
| 52 | return 1 |
| 53 | fi |
| 54 | ask_yes_no "Install the .deb now with apt?" |
| 55 | } |
| 56 | |
| 57 | # Make rustup toolchains visible when installed but not yet on PATH. |
| 58 | if [[ -f "$HOME/.cargo/env" ]]; then |
| 59 | # shellcheck source=/dev/null |
| 60 | source "$HOME/.cargo/env" |
| 61 | fi |
| 62 | |
| 63 | ensure_rust() { |
| 64 | if command -v cargo &>/dev/null && command -v rustc &>/dev/null; then |
| 65 | return 0 |
| 66 | fi |
| 67 | |
| 68 | echo "cargo/rustc not found." |
| 69 | if ask_yes_no "Install Rust via rustup now?"; then |
| 70 | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y |
| 71 | # shellcheck source=/dev/null |
| 72 | source "$HOME/.cargo/env" |
| 73 | else |
| 74 | echo "Install Rust from https://rustup.rs (or your distro packages), then re-run." |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | if ! command -v cargo &>/dev/null || ! command -v rustc &>/dev/null; then |
| 79 | echo "Rust install finished but cargo/rustc still not on PATH. Open a new shell or: source \"\$HOME/.cargo/env\"" |
| 80 | exit 1 |
| 81 | fi |
| 82 | } |
| 83 | |
| 84 | ensure_debian_build_deps() { |
| 85 | if ! command -v apt-get &>/dev/null; then |
| 86 | return 0 |
| 87 | fi |
| 88 | |
| 89 | local pkgs=( |
| 90 | build-essential |
| 91 | pkg-config |
| 92 | libxcb-render0-dev |
| 93 | libxcb-shape0-dev |
| 94 | libxcb-xfixes0-dev |
| 95 | libxkbcommon-dev |
| 96 | libssl-dev |
| 97 | libudev-dev |
| 98 | ) |
| 99 | local missing=() |
| 100 | local pkg |
| 101 | for pkg in "${pkgs[@]}"; do |
| 102 | if ! dpkg -s "$pkg" &>/dev/null; then |
| 103 | missing+=("$pkg") |
| 104 | fi |
| 105 | done |
| 106 | |
| 107 | if [[ ${#missing[@]} -eq 0 ]]; then |
| 108 | return 0 |
| 109 | fi |
| 110 | |
| 111 | echo "Missing apt packages: ${missing[*]}" |
| 112 | if ask_yes_no "Install them with apt now?"; then |
| 113 | sudo apt-get update |
| 114 | sudo apt-get install -y "${missing[@]}" |
| 115 | else |
| 116 | echo "Install manually, then re-run:" |
| 117 | echo " sudo apt-get install -y ${missing[*]}" |
| 118 | exit 1 |
| 119 | fi |
| 120 | } |
| 121 | |
| 122 | ensure_cargo_deb() { |
| 123 | if command -v cargo-deb &>/dev/null; then |
| 124 | return 0 |
| 125 | fi |
| 126 | |
| 127 | echo "cargo-deb not found (needed for --deb)." |
| 128 | if ask_yes_no "Install cargo-deb with cargo now?"; then |
| 129 | cargo install cargo-deb --locked |
| 130 | else |
| 131 | echo "Install manually: cargo install cargo-deb --locked" |
| 132 | exit 1 |
| 133 | fi |
| 134 | } |
| 135 | |
| 136 | ensure_rust |
| 137 | ensure_debian_build_deps |
| 138 | |
| 139 | echo "Building posprint for Linux (x86_64)..." |
| 140 | |
| 141 | if [[ "$SKIP_TESTS" != "true" ]]; then |
| 142 | cargo test |
| 143 | fi |
| 144 | |
| 145 | cargo build --release |
| 146 | BIN="$ROOT/target/release/posprint" |
| 147 | echo "OK: $BIN" |
| 148 | ls -lh "$BIN" |
| 149 | |
| 150 | if [[ "$BUILD_DEB" == "true" ]]; then |
| 151 | ensure_cargo_deb |
| 152 | cargo deb |
| 153 | shopt -s nullglob |
| 154 | DEBS=("$ROOT"/target/debian/*.deb) |
| 155 | shopt -u nullglob |
| 156 | if [[ ${#DEBS[@]} -eq 0 ]]; then |
| 157 | echo "No .deb produced." |
| 158 | exit 1 |
| 159 | fi |
| 160 | echo "Debian packages:" |
| 161 | ls -lh "${DEBS[@]}" |
| 162 | |
| 163 | if want_install; then |
| 164 | echo "Installing with apt..." |
| 165 | # Relative path so apt treats it as a local package file. |
| 166 | sudo apt-get install -y "${DEBS[@]}" |
| 167 | echo "Installed." |
| 168 | fi |
| 169 | fi |