#!/bin/sh

TESTOUTPUT="test_output"
PWD="$(pwd)/$TESTOUTPUT"

# Tidy up
rm -r "$PWD"
mkdir -p "$PWD"

# Pepare toml
TP_NOTE_TEST_TOML="$PWD/tp-note-tmp.toml"
grep -v -e ^date:  "tp-note.toml" > "$TP_NOTE_TEST_TOML"


## Test 1: Synchronize meta data and filename
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: "xxx"
subtitle: "yyy"
...
EOF

LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME"  >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/123-xxx--yyy.md"

if diff "$OUTPUT_FILENAME"  "test1-synchronize-expected-output"
then
    echo Commandline test 1 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 1 failed.
    exit 1
fi



## Test 2: Create a new note

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch \
    --config "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/$(date +%Y%m%d)-$TESTOUTPUT--Note.md"

if diff "$OUTPUT_FILENAME" "test2-new-expected-output"
then
    echo Commandline test 2 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 2 failed.
    exit 1
fi



## Test 3: Create a new note annotating some existing file

INPUT_FILENAME="$PWD/test3-annotate+clipboard-input-dummy.pdf"

# Input file can be empty
:>"$INPUT_FILENAME"

echo '[ab:cd"ef](https://getreu.net)' | \
LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch \
	    --config "$TP_NOTE_TEST_TOML" \
	    "$INPUT_FILENAME" >/dev/null 2>&1

# the + is not considered to be a secure char, therefor ommitted
OUTPUT_FILENAME="$PWD/test3-annotate clipboard-input-dummy.pdf--URL.md"

if diff "$OUTPUT_FILENAME" "test3-annotate+clipboard-expected-output"
then
echo Commandline test 3 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 3 failed.
    exit 1
fi




## Test 4: Create a new note, based on a markdown link supplied by clipboard

# Tp-Note ignores the clipboard in batch mode, we pass it as
# environment variable instead.

echo '[ab:cd"ef](https://getreu.net)' | \
LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/$(date +%Y%m%d)-ab_cd ef--URL.md"

if diff "$OUTPUT_FILENAME" "test4-clipboard-expected-output"
then
    echo Commandline test 4 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 4 failed.
    exit 1
fi



## Test 5: Create a new note, based on a string supplied by clipboard

#echo -n 'Good morning' | xclip -selection clipboard

echo 'Good morning' | \
LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/$(date +%Y%m%d)-Good morning--Note.md"

if diff "$OUTPUT_FILENAME" "test5-clipboard-expected-output"
then
    echo Commandline test 5 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 5 failed.
    exit 1
fi




## Test 6: Create a new note annotating some existing file
INPUT_FILENAME="$PWD/test6-annotate-input-dummy.pdf"

# Input file can be empty
:>"$INPUT_FILENAME"

LANG="en_US.UTF-8" LOGNAME="myuser" \
    cargo run -- --batch \
    --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/test6-annotate-input-dummy.pdf--Note.md"

if diff "$OUTPUT_FILENAME" "test6-annotate-expected-output"
then
echo Commandline test 6 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 6 failed.
    exit 1
fi



## Test 7: Pin sort tag
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: "xxx"
subtitle: "yyy"
sort_tag: "111-"
file_ext: "mdtxt"
...
EOF

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME"  >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/111-xxx--yyy.mdtxt"

if diff "$OUTPUT_FILENAME"  "test7-tag-expected-output"
then
    echo Commandline test 7 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 7 failed.
    exit 1
fi



## Test 8: stream note content with front matter

INPUT_DATA="---
title: 'aaa'
subtitle: 'bbb'
date: '2020-01-02'
lang: 'en'
revision: '2.0'
sort_tag: '222-'
file_ext: 'mdtxt'
my_own_var: 'foo'
...
EOF
"

echo "$INPUT_DATA" | \
LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch --config "$TP_NOTE_TEST_TOML" \
    "$PWD"  >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/222-aaa--bbb.mdtxt"

if diff "$OUTPUT_FILENAME"  "test8-stream-content-expected-output"
then
    echo Commandline test 8 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 8 failed.
    exit 1
fi



## Test 9: Create 2 notes with same header

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch \
    --config "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch \
    --config "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/$(date +%Y%m%d)-$TESTOUTPUT--Note.md"
OUTPUT_FILENAME2="$PWD/$(date +%Y%m%d)-$TESTOUTPUT--Note(1).md"
if diff "$OUTPUT_FILENAME2" "test9-new-new-expected-output"
then
    echo Commandline test 9 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$OUTPUT_FILENAME2"
else
    echo Commandline test 9 failed.
    exit 1
fi




## Test 10: Export note
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: "xxx"
subtitle: "yyy"
sort_tag: "111-"
file_ext: "mdtxt"
...
EOF

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME"  >/dev/null 2>&1

INPUT2_FILENAME="$PWD/111-xxx--yyy.mdtxt"

LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch --config "$TP_NOTE_TEST_TOML" \
     --export "" \
    "$INPUT2_FILENAME"  >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/111-xxx--yyy.mdtxt.html"

if diff "$OUTPUT_FILENAME"  "test10-html-expected-output"
then
    echo Commandline test 10 succeeded.
    rm "$INPUT2_FILENAME"
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 10 failed.
    exit 1
fi




## Test 11:  Compilation --no-default-features --features viewer

cargo check --no-default-features --features viewer >/dev/null 2>&1

if [ $? -eq 0 ]
then
    echo Commandline test 11 succeeded.
else
    echo Commandline test 11 failed.
    exit 1
fi




## Test 12:  Compilation --no-default-features

cargo check --no-default-features >/dev/null 2>&1

if [ $? -eq 0 ]
then
    echo Commandline test 12 succeeded.
else
    echo Commandline test 12 failed.
    exit 1
fi




## Test 13: Do not synchronize meta data and filename
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: "xxx"
subtitle: "yyy"
...
EOF

LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch --no-sync --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME"  >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/123-abc--edf.md"

if diff "$OUTPUT_FILENAME"  "test1-synchronize-expected-output"
then
    echo Commandline test 13 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 13 failed.
    exit 1
fi

exit 0
