#!/bin/sh
# Run a paper test
#
# Copyright (c) 2021-2024 Reuben Thomas <rrt@sc3d.org>
#
# This file is part of libpaper.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see
# <https://www.gnu.org/licenses/lgpl-2.1.html>.

set -e

# Skip test if sysconfdir does not start with prefix, as then relocation won't
# work. Typical example: prefix=/usr, sysconfdir=/etc
case "$sysconfdir" in
    "$prefix/"*)
        ;;
    *)
        echo prefix: "$prefix", sysconfdir: "$sysconfdir"
        echo "sysconfdir does not start with prefix, cannot run test!"
        exit 0
        ;;
esac

curr_dir="$(pwd)"
test_file="$curr_dir/$1"
name="${1%.sh}"
basename=$(basename "$name")
test_dir="$curr_dir/$basename.$$"
echo test_dir: "$test_dir"
expected_exit=0
expected_file="$abs_srcdir/$basename-expected.txt"

# Test runner.
# Arguments are command to run.
paper_test() {
    exit_code=0
    paper "$@" > "$basename-output.txt" 2>&1 || exit_code=$?
    if [ $exit_code != $expected_exit ]; then
        echo "Expected exit code $expected_exit but was $exit_code"
        exit 1
    fi
    # Fix variation in output:
    # 1. Convert backslashes to forward slashes
    # 2. Compress double slashes (from relocated paths on some systems)
    # 3. Remove prefix of name of 'paper' executable
    sed -e 's|\\|/|g' -e 's|//|/|g' -e 's|^.*/paper:|paper:|g' < "$basename-output.txt" > "$basename-output-fixed.txt"
    $DIFF -u "$expected_file" "$basename-output-fixed.txt"
}

# Test functions.
no_size() {
    paper_test --no-size
}

no_PAPERSIZE () {
    unset PAPERSIZE
    no_size
}

no_user_papersize() {
    rm -f "$XDG_CONFIG_HOME/papersize"
    no_PAPERSIZE
}

no_locale() {
    export LC_ALL=invalid_locale
    no_user_papersize
}

no_system_papersize() {
    rm -f "./$sysconfdir/papersize"
    no_locale
}

no_system_paperspecs() {
    rm -f "./$sysconfdir/paperspecs"
    no_system_papersize
}

no_user_paperspecs() {
    expected_exit=1
    rm -f "$XDG_CONFIG_HOME/paperspecs"
    no_system_paperspecs
}

no_home() {
    unset XDG_CONFIG_HOME
    unset HOME
    no_user_paperspecs
}

# Make a test installation of paper
cd ..
${MAKE} install DESTDIR="$test_dir"
export PATH="$test_dir/$bindir:$PATH"
export DYLD_LIBRARY_PATH="$test_dir/$libdir"
cd "$test_dir"

# Set up paper configuration
export PAPERSIZE=environment
export LC_PAPER=C
# LC_PAPER in C locale is A4 in glibc, and we rely on glibc to read the
# paper size, so the expected value when the locale is used is "A4".
export XDG_CONFIG_HOME="$test_dir/config"
mkdir "$XDG_CONFIG_HOME"
echo user_papersize > "$XDG_CONFIG_HOME/papersize"
TEST_PAPERSPECS=$abs_srcdir/test-paperspecs
cp "$TEST_PAPERSPECS" "$XDG_CONFIG_HOME/paperspecs"
echo system_papersize > "./$sysconfdir/papersize"
printf "first_system_paperspecs,55,550,mm\nA4,210,297,mm" > "./$sysconfdir/paperspecs"

# Remove the test directory on exit.
# Change directory back to $abs_srcdir first so we don't try to remove a
# directory above the cwd, which is forbidden on some systems.
trap 'cd "$abs_srcdir" && rm -rf "$test_dir"' EXIT

# Run the test script.
. "$test_file"
