#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4 expandtab:

# Regression tests for ktx2check

# -------------------------------------------------------------------------
#
# Copyright 2010-2020 The Khronos Group, Inc.
# SPDX-License-Identifier: Apache-2.0
#
# -------------------------------------------------------------------------

# Depth of this script relative to the project root
depth=..

# Change dir to the testimages folder, a child of the script location...
cd $(dirname $0)/testimages
# ...and adjust depth
depth=$depth/..

# Make paths relative to the testimages directory.
ktx_root=$depth
ktx2check_vs2013=$ktx_root/build/msvs/win/vs2013/x64/Release/ktx2check.exe
ktx2check_vs2015=$ktx_root/build/msvs/win/vs2015/x64/Release/ktx2check.exe
ktx2check_cmake=$ktx_root/build/cmake/linux/Release/ktx2check
ktx2check_cmake_d=$ktx_root/build/cmake/linux/Debug/ktx2check
ktx2check_make=$ktx_root/build/make/linux/out/Release/ktx2check
ktx2check_make_d=$ktx_root/build/make/linux/out/Debug/ktx2check

declare -i numtests=0
declare -i passed=0
declare -i failed=0

if [ -n "$1" -a -x "$1" ]; then
  ktx2check="$1"
elif [ -x "$ktx2check_vs2013" ]; then
  ktx2check=$ktx2check_vs2013
elif [ -x "$ktx2check_vs2015" ]; then
  ktx2check=$ktx2check_vs2015
elif [ -x "$ktx2check_cmake" ]; then
  ktx2check=$ktx2check_cmake
elif [ -x "$ktx2check_cmake_d" ]; then
  ktx2check=$ktx2check_cmake_d
elif [ -x "$ktx2check_make" ]; then
  ktx2check=$ktx2check_make
elif [ -x "$ktx2check_make_d" ]; then
  ktx2check=$ktx2check_make_d
elif ! ktx2check=$(which ktx2check); then
  echo $0: None of $ktx2check_vs2013, $ktx2check_vs2015, $ktx2check_gmake,
  echo $ktx2check_make_d, $ktx2check_cmake or $ktx2check_cmake_d found.
  echo $0: Aborting test
  exit 1
fi

numtests=$numtests+1
if $ktx2check --help 2> /dev/null; then
  passed=$passed+1
else
  echo "--help not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
if $ktx2check --version 2> /dev/null; then
  passed=$passed+1
else
  echo "--version not recognized"
  failed=$failed+1
fi

numtests=$numtests+1
$ktx2check --foobar 2> /dev/null
status=$?
if [ $status -eq 1 ]; then
  passed=$passed+1
elif [ $status -gt 1 ]; then
  echo "wrong exit code for invalid option."
  failed=$failed+1
else
  echo "invalid option --foobar accepted"
  failed=$failed+1
fi

#function cmpktx () {
#  if diff $1 $2; then
#    passed=$passed+1
#    rm $1
#    return 0
#  else
#	failed=$failed+1
#    echo "Created ktx file differs from target $2. Bad file saved in $PWD/$1"
#    return 1
#  fi
#}

#function mktmp() {
#  # Git Bash does not include mktemp.
#  if which mktemp > /dev/null; then
#    mktemp toktx.XXXX
#  else
#    toktx.$numtests
#  fi
#}

# Generate ktx file and compare with reference
# gencmpktx <reference> <toktx args> <toktx infile> ...
#function gencmpktx() {
#  numtests=$numtests+1
#  local args
#  local reference=$1; shift
#  local tempfile=$(mktmp)
#  for i in $*; do
#	if [ ${i:0:2} == "--" ]; then
#	  args="$args $i"
#	  shift
#	fi
#  done
#  #echo $toktx $args $tempfile $*
#  if $toktx $args $tempfile $*; then
#    cmpktx $tempfile $reference
#  fi
#}

#---------------- Tests start here -----------------------

numtests=$numtests+1
if $ktx2check *.ktx2; then
    passed=$passed+1
else
    echo "Valid file(s) failed validation."
    failed=$failed+1
fi

numtests=$numtests+1
$ktx2check --quiet *.ktx2
status=$?
if [ $status -eq 0 ]; then
    if [ -z $output ]; then
        passed=$passed+1
    else
        echo "Output written with --quiet."
        failed=$failed+1
    fi
elif [ $status -eq 1 ]; then
    echo "--quiet not recognized."
    failed=$failed+1
else
    echo "Valid file(s) failed validation."
    failed=$failed+1
fi

numtests=$numtests+1
if ktx2check < color_grid_uastc_zstd.ktx2; then
    passed=$passed+1
else
    echo "Valid file read via stdin failed validation."
    failed=$failed+1
fi

numtests=$numtests+1
if cat color_grid_uastc_zstd.ktx2 | ktx2check; then
    passed=$passed+1
else
    echo "Valid file read via stdin failed validation."
    failed=$failed+1
fi

numtests=$numtests+1
cd ../badktx2
if $ktx2check invalid_face_count.ktx2 > /dev/null; then
    echo "Invalid face count not spotted."
    failed=$failed+1
else
    passed=$passed+1
fi

numtests=$numtests+1
output=$($ktx2check --quiet invalid_face_count.ktx2)
if [ $? -eq 0 ]; then
    echo "Invalid face count not spotted."
    failed=$failed+1
else
    if [ -z $output ]; then
        passed=$passed+1
    else
        echo "Output written with --quiet."
        failed=$failed+1
    fi
fi

numtests=$numtests+1
cd ../badktx2
if $ktx2check incorrect_mip_layout_and_padding.ktx2 > /dev/null; then
    echo "Invalid mip layout not spotted."
    failed=$failed+1
else
    passed=$passed+1
fi

numtests=$numtests+1
output=$($ktx2check --quiet incorrect_mip_layout_and_padding.ktx2)
if [ $? -eq 0 ]; then
    echo "Invalid mip layout not spotted."
    failed=$failed+1
else
    if [ -z $output ]; then
        passed=$passed+1
    else
        echo "Output written with --quiet."
        failed=$failed+1
    fi
fi
echo "Tests run: $numtests; passed: $passed; failed: $failed"
if [ $failed -gt 0 ]; then
  exit 1;
else
  exit 0;
fi

