#!/bin/bash

# The tests here mainly check that errors are caught and that
# execution is successful in normal cases. For some cases the output
# is compared with reference output, but the correctness of
# calculations is generally handled by the unit tests.

# Making this a .in-file was necessary to get 'make distcheck' to work

cli=../src/freesasa
datadir=@top_srcdir@/tests/data
sharedir=@top_srcdir@/share/
dump=tmp/test-cli.dump # will only contain the latest output
nofile=nonexistent-file
nodir=nonexistent-dir/file
smallpdb=$datadir/rsa/GLY.pdb
#global errors
errors=0
use_xml=0
if [[ "x@USE_XML@" = "xyes" ]] ; then
    use_xml=1
fi
use_xmllint=0
if [[ "x@XMLLINT@" = "xxmllint" ]] ; then
    use_xmllint=1
fi
use_json=0
if [[ "x@USE_JSON@" = "xyes" ]] ; then
    use_json=1
fi
use_jsonlint=0
if [[ "x@JSONLINT@" = "xjsonlint" ]] ; then
    use_jsonlint=1
fi


function assert_pass
{
    eval $1
    if [[ $? -ne 0 ]]; then
        echo Error: \"$1\" failed
        let errors=errors+1
    #else
    #    echo \"$1\" successful
    fi
}

function assert_fail
{
    eval $1 2>/dev/null
    if [[ $? -eq 0 ]]; then
        echo Error: \"$1\" did not fail as expected
        let errors=errors+1
    #else
    #    echo \"$1\" failed as expected;
    fi
}

function assert_equal_opt
{
    tmp1=tmp/tmp1
    tmp2=tmp/tmp2
    eval $1 $2 > $tmp1 2>/dev/null
    eval $1 $3 > $tmp2 2>/dev/null
    diff -B $tmp1 $tmp2
    if [[ $? -ne 0 ]]; then
        echo Error: \"$1\" gives different results with arguments \"$2\" and \"$3\"
        let errors=errors+1
    #else
    #    echo \"$1\" gives same result with arguments \"$2\" and \"$3\". Success.
    fi
}

function assert_equal_total
{
    tmp1=tmp/tmp1
    tmp2=tmp/tmp2

    eval $1 $2 | grep "Total" > $tmp1 2>/dev/null
    if [[ $? -ne 0 ]]; then
        echo Error: \"$1 $2\" fails
        let errors=errors+1
    fi

    eval $1 $3 | grep "Total" > $tmp2 2>/dev/null
    if [[ $? -ne 0 ]]; then
        echo Error: \"$1 $3\" fails
        let errors=errors+1
    fi

    diff $tmp1 $tmp2
    if [[ $? -ne 0 ]]; then
        echo Error: \"$1\" gives different results with arguments \"$2\" and \"$3\"
        let errors=errors+1
    #else
    #    echo \"$1\" gives same result with arguments \"$2\" and \"$3\". Success.
    fi
}

if [[ ! -d tmp ]]; then mkdir tmp; fi

rm -f tmp/*
echo
echo "== Basic file errors =="
assert_fail "$cli > $dump"
assert_fail "$cli $nofile > $dump"
assert_fail "$cli < $nofile > $dump"
assert_fail "$cli -c $nofile < $smallpdb > $dump"
assert_fail "$cli < $smallpdb > $nodir"
assert_fail "$cli -e $nodir $smallpdb"
assert_fail "$cli -o $nodir $smallpdb"
# can only have one output
assert_fail "$cli -o $dump -o $dump $smallpdb"
# deprecated
assert_fail "$cli --B-value-file $nodir < $smallpdb > $dump"
assert_fail "$cli --residue-type-file $nodir < $smallpdb > $dump"
assert_fail "$cli --residue-file $nodir < $smallpdb > $dump"

echo
echo "== General options =="
# unknown options
assert_fail "$cli --blablabla $smallpdb"
assert_fail "$cli -x $smallpdb"
# no option argument
assert_fail "$cli $smallpdb -o"
# help message
assert_pass "$cli -h > $dump 2> /dev/null"
assert_pass "$cli --deprecated > $dump 2> /dev/null"
# check that it's a valid version number
version=`$cli -v | head -n 1`
assert_pass "echo $version | perl -ne 'if (m/FreeSASA (\d+\.)*\d(-beta)?$/) {exit 0} else  {exit 1}'"
# check that running with several output options doesn't crash
assert_pass "$cli -o $dump -e tmp/err -n 1 $smallpdb"

echo
echo "== Testing S&R =="
assert_pass "$cli -S <  $datadir/1ubq.pdb > $dump"
echo
#check that output message has required components, also verifies that basics of computation still work
assert_pass "grep 'source\s\s*: stdin' $dump"
$cli -S  $datadir/1ubq.pdb > $dump
assert_pass "grep 'source\s\s*: $datadir/1ubq.pdb' $dump"
assert_pass "grep 'atoms\s\s*: 602' $dump"
assert_pass "grep 'probe-radius\s\s*: [[:digit:]]' $dump"
assert_pass "grep 'threads\s\s*: [[:digit:]]' $dump"
assert_pass "grep 'testpoints\s\s*: [[:digit:]]' $dump"
assert_pass "grep 'algorithm\s\s*: \w' $dump"
assert_pass "grep 'Total\s\s*:\s\s*4834.72' $dump"
assert_pass "grep 'Polar\s\s*:\s\s*2515.82' $dump"
assert_pass "grep 'Apolar\s\s*:\s\s*2318.90' $dump"
# The above formulation of log output gives freedom in ordering and
# whitespace, and also version. Allows non-essential details to change
# without breaking the test. Will not be performed for variant
# parameter values, those should be covered by tests.
assert_pass "$cli -S -Y $datadir/1d3z.pdb -w > $dump"
assert_pass "grep 'atoms\s\s*: 1231' $dump"
assert_pass "grep 'Total\s\s*:\s\s*5035.61' $dump"
assert_pass "$cli -S -H -w $datadir/1ubq.pdb > $dump" # suppress warnings here
assert_pass "grep 'atoms\s\s*: 660' $dump"
assert_pass "grep 'Total\s\s*:\s\s*5656.65' $dump"
assert_pass "$cli -S -n 50 < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -n 0 < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -n \"-1\" < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -t 1000 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -S -t 16 < $smallpdb > $dump"

echo
echo "== Testing -m -M and -C options =="
# using flags -S and -n 10 to speed things up
assert_pass "$cli -n 2 -S -M $datadir/1d3z.pdb > $dump"
n_mod=`grep 1d3z.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 10"
assert_pass "$cli -n 2 -S -C $datadir/1d3z.pdb > $dump"
n_mod=`grep 1d3z.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 1"
assert_pass "$cli -n 2 -S -M -C $datadir/1d3z.pdb > $dump"
n_mod=`grep 1d3z.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 10"
assert_pass "$cli -n 2 -S -M $datadir/2jo4.pdb > $dump"
n_mod=`grep 2jo4.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 10"
assert_pass "$cli -n 2 -S -C $datadir/2jo4.pdb > $dump"
n_mod=`grep 2jo4.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 4"
assert_pass "$cli -n 2 -S -M -C  $datadir/2jo4.pdb > $dump"
n_mod=`grep 2jo4.pdb $dump | wc -l`
assert_pass "test $n_mod -eq 40"
assert_fail "$cli -mM $datadir/2jo4.pdb > $dump"

echo
echo "== Testing L&R =="
assert_pass "$cli -L < $smallpdb > $dump"
assert_pass "$cli -L -n 10 < $smallpdb > $dump"
assert_fail "$cli -L -n 0 < $smallpdb > $dump"
assert_fail "$cli -L -n \"-1\" < $smallpdb > $dump"
assert_fail "$cli -L -t 1000 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -L -t 16 < $smallpdb > $dump"

echo
echo "== Testing option --chain-groups =="
assert_pass "$cli -g S -S -n 10 $smallpdb > $dump"
assert_fail "$cli -g B -S -n 10 $smallpdb > $dump"
assert_pass "$cli -g AB -S -n 10 $datadir/2jo4.pdb > $dump"
assert_fail "$cli -g E -S -n 10 $datadir/2jo4.pdb > $dump"
assert_pass "$cli -g AB+CD -S -n 10 $datadir/2jo4.pdb > $dump"
assert_fail "$cli -g A-B -S -n 10 $datadir/2jo4.pdb > $dump"
assert_fail "$cli -g AB -S -n 10 $smallpdb > $dump"
assert_fail "$cli -g A+B -S -n 10 $smallpdb > $dump"

echo
echo "== Testing probe radius =="
assert_fail "$cli -S -n 10 -p=-1 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -S -n 10 -p 1 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -S -p 1.4 --format=pdb < $datadir/1ubq.pdb | grep -v REMARK > tmp/bfactor.pdb"
assert_pass "diff tmp/bfactor.pdb $datadir/1ubq.B.pdb"

echo
echo "== Testing option --unknown =="
assert_pass "$cli --unknown=guess -Y -w -n 2  $datadir/1d3z.pdb > $dump"
assert_pass "grep 1231 $dump"
assert_pass "$cli --unknown=skip -Y -w -n 2 $datadir/1d3z.pdb > $dump"
assert_pass "grep 602 $dump"
assert_fail "$cli --unknown=halt -Y -w -n 2 $datadir/1d3z.pdb > $dump"
# misspelling should fail
assert_fail "$cli --unknown=haltt -Y -w -n 2 $datadir/1d3z.pdb > $dump"

echo
echo "== Testing option --cif =="
assert_equal_total "$cli" "$datadir/1ubq.pdb" "$datadir/1ubq.cif --cif"
assert_equal_total "$cli" "$datadir/1d3z.pdb" "$datadir/1d3z.cif --cif"
assert_equal_total "$cli" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_equal_total "$cli" "$datadir/5hdn.pdb" "$datadir/5hdn.cif --cif"
assert_equal_total "$cli" "$datadir/3bkr.pdb" "$datadir/3bkr.cif --cif"
assert_equal_total "$cli -w" "$datadir/3gnn.pdb" "$datadir/3gnn.cif --cif"
assert_equal_total "$cli --join-models" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_equal_total "$cli --chain-groups AB+CD -S -n 10" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_pass "$cli --cif --separate-models --format=cif $datadir/1ubq.cif > $dump"
assert_pass "$cli --cif --format=cif $datadir/1ubq.cif > $dump"
assert_pass "$cli --cif --format=cif --separate-chains $datadir/1ubq.cif > $dump"
assert_pass "$cli --cif --format=cif --separate-chains --separate-models $datadir/1ubq.cif > $dump"
assert_fail "$cli --cif --format=pdb $datadir/1ubq.cif > $dump"
assert_fail "$cli --format=cif $datadir/1ubq.pdb > $dump"

echo
echo "== Testing --separate-chains and --separate-models output are equal between cif and pdb"
assert_equal_total "$cli --separate-chains -S -n 10" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_equal_total "$cli --separate-chains -S -n 10" "$datadir/2isk.pdb" "$datadir/2isk.cif --cif"
assert_equal_total "$cli --separate-chains -S -n 10" "$datadir/1sui.pdb" "$datadir/1sui.cif --cif"
assert_equal_total "$cli --separate-chains -S -n 10" "$datadir/5dx9.pdb" "$datadir/5dx9.cif --cif"

# need input files with multiple models
assert_equal_total "$cli --separate-models -S -n 10" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_equal_total "$cli --separate-models -S -n 10" "$datadir/2isk.pdb" "$datadir/2isk.cif --cif"
assert_equal_total "$cli --separate-models -S -n 10" "$datadir/1sui.pdb" "$datadir/1sui.cif --cif"
assert_equal_total "$cli --separate-models -S -n 10" "$datadir/1d3z.pdb" "$datadir/1d3z.cif --cif"
# need input files with multiple models
assert_equal_total "$cli --separate-chains --separate-models -S -n 10" "$datadir/2jo4.pdb" "$datadir/2jo4.cif --cif"
assert_equal_total "$cli --separate-chains --separate-models -S -n 10" "$datadir/2isk.pdb" "$datadir/2isk.cif --cif"
assert_equal_total "$cli --separate-chains --separate-models -S -n 10" "$datadir/1sui.pdb" "$datadir/1sui.cif --cif"
assert_equal_total "$cli --separate-chains --separate-models -S -n 10" "$datadir/1d3z.pdb" "$datadir/1d3z.cif --cif"

echo
echo "== Testing user-configurations =="
assert_pass "$cli -c $sharedir/naccess.config -n 3 < $smallpdb > $dump"
assert_pass "$cli -c $sharedir/oons.config -n 3 < $smallpdb > $dump"
assert_fail "$cli -c $datadir/err.config -n 3 < $smallpdb > $dump"
# can't combine these options
assert_fail "$cli -c $datadir/naccess.config -n 3 -O < $smallpdb > $dump"
assert_fail "$cli -c $datadir/naccess.config -n 3 --radii=naccess < $smallpdb > $dump"

echo
echo "== Testing --radii" ==
assert_pass "$cli --radii=naccess -n 3 < $datadir/1ubq.pdb > tmp/static.dat"
assert_pass "$cli -c $sharedir/naccess.config -n 3 < $datadir/1ubq.pdb > tmp/from_config.dat"
assert_pass "diff tmp/static.dat tmp/from_config.dat"
assert_pass "$cli --radii=protor -n 3 < $datadir/1ubq.pdb > tmp/static.dat"
assert_pass "$cli -c $sharedir/protor.config -n 3 < $datadir/1ubq.pdb > tmp/from_config.dat"
assert_pass "diff tmp/static.dat tmp/from_config.dat"
assert_fail "$cli --radii=bla -n 3 < $datadir/1ubq.pdb > $dump"

echo
echo "== Testing res format =="
assert_pass "$cli -S --format=res -o tmp/restype -e $dump < $datadir/1ubq.pdb"
assert_pass "diff tmp/restype $datadir/restype.reference"
#deprecated
assert_pass "$cli -S -r < $datadir/1ubq.pdb > tmp/restype"
assert_pass "diff tmp/restype $datadir/restype.reference"

echo
echo "== Testing seq format =="
assert_pass "$cli -S --format=seq -o tmp/seq < $datadir/1ubq.pdb"
assert_pass "diff tmp/seq $datadir/seq.reference"
#deprecated
assert_pass "$cli -S -R < $datadir/1ubq.pdb > tmp/seq"
assert_pass "diff tmp/seq $datadir/seq.reference"

echo
echo "== Testing PDB output format =="
assert_pass "$cli -S --format=pdb < $datadir/1ubq.pdb | grep -v REMARK > tmp/bfactor.pdb"
assert_pass "diff tmp/bfactor.pdb $datadir/1ubq.B.pdb"
#deprecated
assert_pass "$cli -S -B < $datadir/1ubq.pdb | grep -v REMARK > tmp/bfactor.pdb"
assert_pass "diff tmp/bfactor.pdb $datadir/1ubq.B.pdb"

echo
echo "== Testing RSA format =="
for r in protor naccess
do
    for p in $(ls $datadir/rsa/*.pdb)
    do
        rel=$($cli -L -n 1000 $p --format=rsa --radii=$r | grep "S   2" | sed "s/[[:space:]]\{1,\}/ /g" | cut -f 6,8,10,12,14 -d ' ')
        if [[ $p == *"GLY"* ]]
        then
            assert_pass "test '$rel' = '100.0 N/A 100.0 100.0 100.0' && test $p"
        else
            assert_pass "test '$rel' = '100.0 100.0 100.0 100.0 100.0' && test $p"
        fi
    done
done
assert_fail "$cli --format=rsa -C $smallpdb"
assert_fail "$cli --format=rsa -M $smallpdb"
assert_pass "$cli -L -n 1000 --format=rsa -O -w $datadir/rsa/ALA.pdb > tmp/no_rel"
rel=$(grep "S   2" tmp/no_rel | sed "s/[[:space:]]\{1,\}/ /g" | cut -f 6,8,10,12,14 -d ' ')
assert_pass "test '$rel' = 'N/A N/A N/A N/A N/A'"
# deprecated
assert_pass "$cli --rsa $datadir/1ubq.pdb > $dump"

echo "== Verify idempotency of CIF format =="
assert_equal_opt "$cli --cif --format=cif $datadir/1ubq.cif" "" " | $cli --cif --format=cif"
assert_equal_opt "$cli --cif --format=cif --separate-chains $datadir/2jo4.cif" "" " | $cli --separate-chains --cif --format=cif"
assert_equal_opt "$cli --cif --format=cif --separate-models $datadir/2jo4.cif" "" " | $cli --separate-models --cif --format=cif"
assert_equal_opt "$cli --cif --format=cif --separate-models --separate-chains $datadir/2jo4.cif" "" \
    " | $cli --separate-models --separate-chains --cif --format=cif"

# XML // very basic testing, just to make sure XML is valid and that the right tags are present
if [[ use_xml -eq 1 ]] ; then
    echo
    echo "== Testing XML =="
    xml_file=tmp/test.xml

    assert_pass "$cli --format=xml $smallpdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi

    assert_pass "$cli --format=xml --depth=atom $smallpdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_pass "grep '<atom' $xml_file > $dump"
    assert_pass "grep '<residue' $xml_file > $dump"
    assert_pass "grep '<chain' $xml_file > $dump"

    assert_pass "$cli --format=xml --depth=residue $smallpdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_fail "grep '<atom' $xml_file > $dump"
    assert_pass "grep '<residue' $xml_file > $dump"
    assert_pass "grep '<chain' $xml_file > $dump"

    assert_pass "$cli --format=xml --depth=chain $smallpdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_fail "grep '<atom' $xml_file > $dump"
    assert_fail "grep '<residue' $xml_file > $dump"
    assert_pass "grep '<chain' $xml_file > $dump"

    assert_pass "$cli --format=xml --depth=structure $smallpdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_fail "grep '<atom' $xml_file > $dump"
    assert_fail "grep '<residue' $xml_file > $dump"
    assert_fail "grep '<chain' $xml_file > $dump"

    assert_pass "$cli --format=xml -C $datadir/2jo4.pdb > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_fail "grep '<selection' $xml_file > $dump"

    assert_pass "$cli --format=xml -C $smallpdb --select=\"ala, resn ala\" --select=\"C, name C\" > $xml_file"
    if [[ use_xmllint -eq 1 ]] ; then assert_pass "xmllint $xml_file > $dump"; fi
    assert_pass "grep '<selection' $xml_file > $dump"

    # misspelling
    assert_fail "$cli --format=xml --depth=structures $smallpdb > $xml_file"
fi
# JSON // very basic testing, just to make sure JSON is valid and that the right tags are present
if [[ use_json -eq 1 ]] ; then
    echo
    echo "== Testing JSON =="
    json_file=tmp/test.json
    assert_pass "$cli --format=json $smallpdb > $json_file"
    if [[ use_jsonlint -eq 1 ]] ; then assert_pass "jsonlint $json_file > $dump"; fi

    assert_pass "$cli --format=json --depth=atom $smallpdb > $json_file"
    if [[ use_jsonlint -eq 1 ]] ; then assert_pass "jsonlint $json_file > $dump"; fi
    assert_pass "grep 'atom' $json_file > $dump"
    assert_pass "grep 'residue' $json_file > $dump"
    assert_pass "grep 'chain' $json_file > $dump"

    assert_pass "$cli --format=json -C $datadir/2jo4.pdb > $json_file"
    if [[ use_jsonlint -eq 1 ]] ; then assert_pass "jsonlint $json_file > $dump"; fi
    assert_fail "grep 'selection' $json_file > $dump"

    assert_pass "$cli --format=json $smallpdb --select=\"ala, resn ala\" --select=\"C, name C\" > $json_file"
    if [[ use_jsonlint -eq 1 ]] ; then assert_pass "jsonlint $json_file > $dump"; fi
    assert_pass "grep 'selection' $json_file > $dump"
fi

echo
echo "== Testing option --select =="
assert_pass "$cli -S --select \"s1, resn ala\" --select \"s2, resn arg\" --select \"s3, resi 1\" $datadir/1ubq.pdb > tmp/select"
sel_ala=$(grep s1 tmp/select | cut -f 2 -d ':' | sed 's/\ //g')
res_ala=$(grep ALA tmp/restype | cut -f 2 -d ':' | sed 's/\ //g')
if [ "$res_ala" != "$sel_ala" ];
then
    let errors=errors+1
    echo "Error: --format=res and --select don't give same result for ALA ('$res_ala' and '$sel_ala')"
fi
sel_arg=$(grep s2 tmp/select | cut -f 2 -d ':' | sed 's/\ //g' | sed s/A2//)
res_arg=$(grep ARG tmp/restype | cut -f 2 -d ':' | sed 's/\ //g' | sed s/A2// | sed s/ARG//)
if [ "$res_arg" != "$sel_arg" ]
then
    let errors=errors+1
    echo "Error: --format=res and --select don't give same result for ARG ('$res_arg' and '$sel_arg')"
fi
sel_res1=$(grep s3 tmp/select | cut -f 2 -d ':' | sed 's/\ //g' | sed s/A2//)
seq_res1=$(grep " 1 " tmp/seq | cut -f 2 -d ':' | cut -f 2 -d 'T' | sed 's/ //g')
if [ "$seq_res1" != "$sel_res1" ]
then
    let errors=errors+1
    echo "Error: --format=seq and --select don't give same result for first residue in 1ubq.pdb ('$seq_res1' and '$sel_res1')"
fi

echo
echo
echo "== Testing multithreading =="
assert_pass "$cli -t 1 -S -n 10 < $smallpdb > $dump"
assert_pass "$cli -t 2 -S -n 10 < $smallpdb > $dump"
assert_pass "$cli -t 10 -S -n 10 < $smallpdb > $dump"
assert_pass "$cli -t 1 -L -n 3 < $smallpdb > $dump"
assert_pass "$cli -t 2 -L -n 3 < $smallpdb > $dump"
assert_pass "$cli -t 10 -L -n 3 < $smallpdb > $dump"
assert_fail "$cli -t 0 < $smallpdb > $dump"

echo
echo "== Testing input with residue insertions ==="
assert_pass "test $($cli -n 2 --format=seq < $datadir/icode.pdb | grep ^SEQ | wc -l) -eq 5"

echo
echo "== Testing conflicting options =="
assert_fail "$cli -m -M $smallpdb > $dump"
assert_fail "$cli -g A+B -C $smallpdb > $dump"
assert_fail "$cli -c $sharedir/naccess.config --radii=naccess $smallpdb > $dump"
assert_fail "$cli -O --radii=naccess $smallpdb > $dump"
assert_fail "$cli -c $sharedir/naccess.config -O $datadir/1ubq.pdb > $dump"

echo
echo "== Testing long-options =="
assert_equal_opt "$cli $smallpdb" "-h" "--help"
assert_equal_opt "$cli $smallpdb" "-v" "--version"
assert_equal_opt "$cli $smallpdb" "-S -n 10" "--shrake-rupley -n 10"
assert_equal_opt "$cli $smallpdb" "-L -n 3 " "--lee-richards -n 3 "
assert_equal_opt "$cli $smallpdb" "-p 1.5 -n 3" "--probe-radius=1.5 -n 3"
assert_equal_opt "$cli $smallpdb" "-L -n 3 " "-L --resolution=3 "
assert_equal_opt "$cli $smallpdb" "-n 5" "--resolution=5"
assert_equal_opt "$cli $smallpdb" "-t 4" "--n-threads=4"
assert_equal_opt "$cli $smallpdb" "-c $sharedir/naccess.config" "--config-file=$sharedir/naccess.config"
assert_equal_opt "$cli $smallpdb" "-H -w" "--hetatm -w"
assert_equal_opt "$cli $datadir/1d3z.pdb" "-Y -n 2" "--hydrogen -n 2"
assert_equal_opt "$cli $datadir/1d3z.pdb" "-m -n 2" "--join-models -n 2"
assert_equal_opt "$cli $datadir/1d3z.pdb" "-C -n 2" "--separate-chains -n 2"
assert_equal_opt "$cli $datadir/1d3z.pdb" "-M -n 2" "--separate-models -n 2"
assert_equal_opt "$cli $smallpdb" "-w -n 2" "--no-warnings -n 2"
assert_equal_opt "$cli $datadir/2jo4.pdb" "-g AB+CD -n 2" "--chain-groups AB+CD -n 2"
assert_equal_opt "$cli $datadir/2jo4.pdb" "-g AB+CD -n 2" "-g AB -g CD -n 2"
assert_equal_opt "$cli $datadir/1ubq.B.pdb" "-O -n 2" "--radius-from-occupancy -n 2"
assert_equal_opt "$cli $smallpdb" "-f xml -n 2" "--format=xml -n 2"
# deprecated, just check that the program doesn't choke
assert_equal_opt "$cli $smallpdb" "-r -n 2" "--foreach-residue-type -n 2"
assert_equal_opt "$cli $smallpdb" "-R -n 2" "--foreach-residue -n 2"
assert_equal_opt "$cli $smallpdb" "-B -n 2" "--print-as-B-values -n 2"
assert_equal_opt "$cli $smallpdb" "-l -n 2" "--no-log -n 2"

rm -f tmp/*

echo
echo "There where $errors errors."
echo
if [ $errors -gt 0 ]; then
    exit $errors
else
    exit 0
fi
