#!/bin/sh
cd $(dirname $(realpath "$0"))

TESTOUTPUT="test_output"
PWD="$(pwd)/$TESTOUTPUT"
unset TPNOTE_CONFIG
unset TPNOTE_EXTENSION_DEFAULT
unset TPNOTE_LANG
unset TPNOTE_LANG_DETECTION
unset TPNOTE_USER


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

# Prepare toml
# Generate new config file.
cargo run -- -C "$PWD/tpnote.toml"
# Remove all `date` lines of all templates.
TP_NOTE_TEST_TOML="$PWD/tpnote-tmp.toml"
grep -v -e "key='fm_date'"  "$PWD/tpnote.toml" > "$TP_NOTE_TEST_TOML"
# Uncomment variables.
sed -i -e 's/^# //g' "$TP_NOTE_TEST_TOML"


## Test 001: 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"  "test001-synchronize-expected-output"
then
    echo Commandline test 001 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 001 failed.
    exit 1
fi



## Test 002: 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" "test002-new-expected-output"
then
    echo Commandline test 002 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 002 failed.
    exit 1
fi



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

INPUT_FILENAME="$PWD/test003-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 --force-lang='-' \
	    --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/test003-annotate+clipboard-input-dummy.pdf--Note.md"

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




## Test 004: 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" \
TPNOTE_LANG_DETECTION="" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

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

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



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

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

# Tp-Note can choose between `fi-Fi` and `en-US`. The latter should be 
# found.
echo 'Good morning' | \
LANG="fi_FI.UTF-8" LOGNAME="myuser" \
TPNOTE_LANG_DETECTION="en-US" \
	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" "test005-clipboard-expected-output"
then
    echo Commandline test 005 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 005 failed.
    exit 1
fi




## Test 006: Create a new note, based on a string supplied by clipboard
echo '123456' | \
LANG="en_US.UTF-8" LOGNAME="myuser" \
TPNOTE_LANG_DETECTION="" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

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

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




## Test 007: Create a new note annotating some existing file
INPUT_FILENAME="$PWD/test007-some english file to annotate.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="${INPUT_FILENAME}--Note.md"

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



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

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

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

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



## Test 009: 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_long_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"  "test009-stream-content-expected-output"
then
    echo Commandline test 009 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 009 failed.
    exit 1
fi



## Test 010: 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" "test010-new-new-expected-output"
then
    echo Commandline test 010 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$OUTPUT_FILENAME2"
else
    echo Commandline test 010 failed.
    exit 1
fi




## Test 011: Export note
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: xxx
subtitle: yyy
author:
- Jens
- Hans
sort_tag: '111'
...
# Body
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.md"

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.md.html"

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



## Test 012: 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"  "test001-synchronize-expected-output"
then
    echo Commandline test 012 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 012 failed.
    exit 1
fi



## Test 013: Add header
INPUT_FILENAME="$PWD/20220312-abc-- edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
Only body
EOF

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

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

if diff "$OUTPUT_FILENAME"  "test013-add-header-expected-output"
then
    echo Commandline test 013 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 013 failed.
    exit 1
fi


## Test 014: Copy counter and sync
INPUT_FILENAME="$PWD/20220312-abc--edf(4).md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: abc
subtitle: edf
---
Body
EOF

OUTPUT_FILENAME=$(LANG="en_US.UTF-8" LOGNAME="myuser" \
	cargo run -- --batch --add-header --config "$TP_NOTE_TEST_TOML" \
    "$INPUT_FILENAME"| tail -n 1)  >/dev/null 2>&1

if [ "$INPUT_FILENAME" = "$OUTPUT_FILENAME" ]
then
    echo Commandline test 014 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 014 failed.
    exit 1
fi



## Test 016: Export note with format attributes
INPUT_FILENAME="$PWD/01ac-Tulips--red, yellow.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: Tulips
subtitle: red, yellow
sort_tag: 01ac
---

[matters](<01ac-Tulips--red, yellow.md>)     
[matters](<01ac>)                            
[whatever](<01ac-Tulips--red, yellow.md?>)   
[whatever](<01ac?>)                          
[whatever](<01ac-Tulips--red, yellow.md?#>)  
[whatever](<01ac?#>)                         
[whatever](<01ac-Tulips--red, yellow.md?,>)  
[whatever](<01ac?,>)                         
[whatever](<01ac-Tulips--red, yellow.md?-->) 
[whatever](<01ac?-->)                        
<tpnote:01ac-Tulips--red,%20yellow.md>       
<tpnote:01ac>                                
<tpnote:01ac-Tulips--red,%20yellow.md??>      
<tpnote:01ac??>                               
<tpnote:01ac-Tulips--red,%20yellow.md??:>     
<tpnote:01ac??:>                              
<tpnote:01ac-Tulips--red,%20yellow.md??:.>    
<tpnote:01ac??:.>                             
<tpnote:01ac-Tulips--red,%20yellow.md??-:,>   
<tpnote:01ac??-:,>                            
<tpnote:01ac-Tulips--red,%20yellow.md??--:,>  
<tpnote:01ac??--:,>                           
EOF

# This is the base dir for the exporter.
:> "$PWD/.tpnote.toml"

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

OUTPUT_FILENAME="$PWD/01ac-Tulips--red, yellow.md.html"

if diff "$OUTPUT_FILENAME"  "test016-html-expected-output"
then
    echo Commandline test 016 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
    rm "$PWD/.tpnote.toml"
    
else
    echo Commandline test 016 failed.
    exit 1
fi




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

#echo -n '<html><h1>Good news</h1></html>' | xclip -selection clipboard

# Tp-Note can choose between `fi-Fi` and `en-US`. The latter should be 
# found.
:> "$PWD/03b-test.md"
echo '<html><h1>Good news</h1></html>' | \
LANG="fi_FI.UTF-8" LOGNAME="myuser" \
TPNOTE_LANG_DETECTION="en-US" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/03c-Good news--Note.md"

if diff "$OUTPUT_FILENAME" "test017-clipboard-expected-output"
then
    echo Commandline test 017 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$PWD/03b-test.md"
else
    echo Commandline test 017 failed.
    exit 1
fi



## Test 018: Create a new note annotating some existing file
INPUT_FILENAME="$PWD/test018-some english file to annotate.pdf"

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

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

OUTPUT_FILENAME="${INPUT_FILENAME}--Note.rst"

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





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

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

# Tp-Note can choose between `fi-Fi`, `en-US`, `de-DE` and `fr`.
echo -e "Parlez-vous français?\nIch spreche Französisch nur ein bisschen.\nA little bit is better than nothing." | \
LANG="fi_FI.UTF-8" LOGNAME="myuser" \
TPNOTE_LANG_DETECTION="en-US,de-DE,fr" \
	cargo run -- --batch \
    		--config  "$TP_NOTE_TEST_TOML" "$PWD" # >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/$(date +%Y%m%d)-Parlez-vous français--Note.md"

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





./run-tests2 || exit 1





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

cargo check --workspace --no-default-features >/dev/null

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



## Test 901:  Compilation --no-default-features --features renderer
cargo check --workspace --no-default-features --features renderer >/dev/null

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



## Test 902:  Compilation --no-default-features --features viewer
cargo check --no-default-features --features viewer >/dev/null

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



## Test 903:  Compilation --no-default-features --features message-box
cargo check --no-default-features --features message-box >/dev/null

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



## Test 904:  Compilation --no-default-features --features read-clipboard
cargo check --no-default-features --features read-clipboard >/dev/null

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



## Test 905:  Compilation --no-default-features --features lang-detection
cargo check --no-default-features \
            --features lang-detection >/dev/null

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






exit 0
