#!/bin/sh
# Smoke test: run the installed canopenterm binary in plain (non-graphical)
# mode against a trivial script and check it produces the expected output.
# This exercises startup, argument parsing and the embedded interpreters
# without requiring a real CAN interface.
set -e

WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORKDIR"

# canopenterm uses XDG_RUNTIME_DIR; missing in some autopkgtest
# environments (e.g. unshare)
if [ -z "${XDG_RUNTIME_DIR:-}" ]; then
  export XDG_RUNTIME_DIR="$WORKDIR/runtime"
  mkdir -m 700 "$XDG_RUNTIME_DIR"
fi

echo 'print("canopenterm-lua-ok")' > smoke.lua
echo 'print("canopenterm-py-ok")'  > smoke.py

echo "I: running Lua script"
output="$(canopenterm smoke.lua)"
printf '%s\n' "$output"
printf '%s\n' "$output" | grep -q "canopenterm-lua-ok"

echo "I: running Python script"
output="$(canopenterm smoke.py)"
printf '%s\n' "$output"
printf '%s\n' "$output" | grep -q "canopenterm-py-ok"

echo "I: running codb2json usage"
output="$(codb2json 2>&1)"
printf '%s\n' "$output"
printf '%s\n' "$output" | grep -q "Usage: codb2json"

echo "PASS: canopenterm smoke test"
