#!/bin/sh
set -e

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

# Sanity check: binary starts and reports its version
ripmime --version

# Functional check: extract a base64-encoded attachment from a minimal
# multipart MIME message
cat > "$AUTOPKGTEST_TMP/sample.eml" <<'EOF'
From: sender@example.com
To: recipient@example.com
Subject: test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="BOUNDARY"

--BOUNDARY
Content-Type: text/plain

This is the body.
--BOUNDARY
Content-Type: text/plain; name="attachment.txt"
Content-Disposition: attachment; filename="attachment.txt"
Content-Transfer-Encoding: base64

aGVsbG8gd29ybGQK
--BOUNDARY--
EOF

mkdir -p "$AUTOPKGTEST_TMP/out"
ripmime -i "$AUTOPKGTEST_TMP/sample.eml" -d "$AUTOPKGTEST_TMP/out"

test -f "$AUTOPKGTEST_TMP/out/attachment.txt"
grep -q "hello world" "$AUTOPKGTEST_TMP/out/attachment.txt"

echo "OK: attachment extracted correctly"
