#!/bin/sh

export TPNOTE_SCHEME=zettel

cd `realpath .`
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 101: Synchronize meta data and filename
INPUT_FILENAME="$PWD/123--abc__edf_ghi.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: xxx
keywords:
- yyy
- zzz
scheme: zettel
...
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_zzz.md"

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



## Test 102: Create a new note
:> "$PWD/03--test.md"
LANG="en_US.UTF-8" LOGNAME="myuser" \
cargo run -- --batch \
    --config "$TP_NOTE_TEST_TOML" "$PWD" >/dev/null 2>&1

OUTPUT_FILENAME="$PWD/04--${TESTOUTPUT}__note.md"

if diff "$OUTPUT_FILENAME" "test102-new-expected-output"
then
    echo Commandline test 102 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$PWD/03--test.md"
else
    echo Commandline test 102 failed.
    exit 1
fi



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

INPUT_FILENAME="$PWD/test103-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/test103-annotate+clipboard-input-dummy.pdf__note.md"

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




## Test 104: 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.

:> "$PWD/03b--test.md"
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/03c--ab_cd ef__note.md"

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



## Test 105: 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.
:> "$PWD/03b--test.md"
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/03c--Good morning__note.md"

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




## Test 106: Create a new note, based on a string supplied by clipboard
:> "$PWD/03b--test.md"
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/03c--'123456__note.md"

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




## Test 107: Create a new note annotating some existing file
INPUT_FILENAME="$PWD/test107-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" "test107-annotate-expected-output"
then
echo Commandline test 107 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 107 failed.
    exit 1
fi



## Test 108: Pin sort tag
INPUT_FILENAME="$PWD/123--abc__edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: xxx
keywords: 
- yyy
sort_tag: '111'
scheme: zettel
file_ext: md
...
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.md"

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



## Test 109: stream note content with front matter

INPUT_DATA="---
title: aaa
keywords: 
- bbb
date: 2020-01-02
lang: en
revision: '2.0'
sort_tag: '222'
file_ext: mdtxt
scheme: zettel
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"  "test109-stream-content-expected-output"
then
    echo Commandline test 109 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 109 failed.
    exit 1
fi



## Test 110: Create 2 notes with same header
:> "$PWD/03b--test.md"
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/03c--${TESTOUTPUT}__note.md"
OUTPUT_FILENAME2="$PWD/03d--${TESTOUTPUT}__note.md"
if diff "$OUTPUT_FILENAME2" "test110-new-new-expected-output"
then
    echo Commandline test 110 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$OUTPUT_FILENAME2"
    rm "$PWD/03b--test.md"
else
    echo Commandline test 110 failed.
    exit 1
fi




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



## Test 112: Do not synchronize meta data and filename
INPUT_FILENAME="$PWD/123-abc--edf.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: xxx
keywords:
- yyy
scheme: zettel
...
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"  "test112-synchronize-expected-output"
then
    echo Commandline test 112 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 112 failed.
    exit 1
fi



## Test 113: Add header
INPUT_FILENAME="$PWD/20220312--abc__edf_hij.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_hij.md"

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


## Test 114: Copy counter and sync
INPUT_FILENAME="$PWD/20220312--abc__edf(4).md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: abc
keywords:
- edf
scheme: zettel
---
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 114 succeeded.
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 114 failed.
    exit 1
fi

## Test 115: Synchronize meta data and filename
:> "$PWD/03b--test.md"
INPUT_FILENAME="$PWD/123-abc__edf_ghi.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: xxx
keywords:
- yyy
- zzz
...
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.md"

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



## Test 116: Export note with format attributes
INPUT_FILENAME="$PWD/01ac--Tulips__red_yellow.md"
cat - > "$INPUT_FILENAME" <<'EOF'
---
title: Tulips
keywords:
- red
- yellow
sort_tag: 01ac
scheme: zettel
---
[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_yellow.md>       
<tpnote:01ac>                                
<tpnote:01ac--Tulips__red_yellow.md??>      
<tpnote:01ac??>                               
<tpnote:01ac--Tulips__red_yellow.md??:>     
<tpnote:01ac??:>                              
<tpnote:01ac--Tulips__red_yellow.md??:.>    
<tpnote:01ac??:.>                             
<tpnote:01ac--Tulips__red_yellow.md??-:,>   
<tpnote:01ac??-:,>                            
<tpnote:01ac--Tulips__red_yellow.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"  "test116-html-expected-output"
then
    echo Commandline test 116 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
    rm "$PWD/.tpnote.toml"
else
    echo Commandline test 116 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" "test117-clipboard-expected-output"
then
    echo Commandline test 117 succeeded.
    rm "$OUTPUT_FILENAME"
    rm "$PWD/03b--test.md"
else
    echo Commandline test 117 failed.
    exit 1
fi





## Test 118: Create a new note annotating some existing file
INPUT_FILENAME="$PWD/test118-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" "test118-annotate-expected-output"
then
echo Commandline test 118 succeeded.
    rm "$INPUT_FILENAME"
    rm "$OUTPUT_FILENAME"
else
    echo Commandline test 118 failed.
    exit 1
fi




## Test 119: 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"
grep -v -e "sort_tag:" "$OUTPUT_FILENAME" > "${OUTPUT_FILENAME}2"

if diff "${OUTPUT_FILENAME}2" "test119-clipboard-expected-output"
then
    echo Commandline test 119 succeeded.
    rm "${OUTPUT_FILENAME}2"
else
    echo Commandline test 119 failed.
    exit 1
fi




exit 0
