#!/bin/sh

# evaluate the given files as images and handle them according to their
# classification:
# Lists "unique", "interesting", "equivalent" and "problem" are presented
# by esimv. "unique" is presented with -double_check:off to increase speed.
# The files in "inferior", "trash" and "unreadble" are deleted without notice.
#

# determine the installation directory of stic_std_variables and execute inline
if test -n "$STIC_MAIN_DIR"
then
  stic_init_dir=$STIC_MAIN_DIR
elif test -r $HOME/.stic_main_dir
then
  stic_init_dir=$(cat $HOME/.stic_main_dir)
fi
if test "$stic_init_dir" = ""
then
  stic_init_dir=$(dirname "$0")/
else
  stic_init_dir="$stic_init_dir"/scripts/
fi
if test -r "$stic_init_dir"stic_std_variables
then
  . "$stic_init_dir"stic_std_variables
elif test  -z "$STIC_NO_BASH" -a -n "$(type -p stic_std_variables)"
then
  . stic_std_variables
fi

# Wether to remove the lists after the run. If lists are kept in the
# working directory, they may be considered unreadable and deleted anyway.
export remove_lists_auto=no

# Common name of the result lists. Usually they are in /tmp but if the
# file similar_importer_create_lists_local exists in the working directory
# or in its parent, then the lists are kept in working directory without
# distinguishing process numbers. So they cannot accumulate.
#
pwd=$(pwd)
if test -e "$pwd"/similar_importer_create_lists_local \
        -o -e "$(dirname $(pwd))"/similar_importer_create_lists_local
then
  listname="_similar_split_"
  remove_lists_auto=no
else
  listname="/tmp/_similar_split_$$_"
  remove_lists_auto=yes 
fi


# --- functions


file_exists() {
# tests wether the first argument is a valid file address
  if test -e "$1"
  then
    return 0
  fi
  return 1
}

remove_lists() {
  if file_exists ${listname}*
  then
    rm ${listname}*
  fi
}

ask_user () {
 echo "$1"
 export inp
 read inp
 if test "$inp" = "Y" -o "$inp" = "y" -o "$inp" = "" -o \
         "$inp" = "j" -o "$inp" = "J" -o "$inp" = "1"
 then
   echo : "$inp" = yes
   return 0
 else
   echo : "$inp" = no
   return 1
 fi
}


final_procedure() {
  echo
  if test "$remove_lists_auto" = "yes"
  then
    remove_lists
  fi
}


# --- main

if test "$1" = "-old_lists"
then
  old_lists=1
  shift 1
fi

trap final_procedure EXIT

"$stic_bin_dir"similar -match_par:4:1:4:2:color_diff
if test -z "$old_lists"
then 
  "$stic_scripts_dir"similar_splitter "$listname" "$@"
fi

wc -l "$listname"*
echo

if ask_user "start processing of result ?"
then
  dummy=dummy
else
  exit 1
fi

echo ; echo '+++ UNIQUE +++' ; echo
if test -s "$listname"unique
then
  "$stic_scripts_dir"simv_start -tcltk -double_check:off \
                                -addfile_list:"$listname"unique
else
  echo list empty
fi

echo ; echo '+++ INTERESTING +++' ; echo
if test -s "$listname"interesting
then
  "$stic_scripts_dir"simv_start -tcltk -addfile_list:"$listname"interesting
else
  echo list empty
fi

echo ; echo '=== EQUIVALENT ===' ; echo
if test -s "$listname"equivalent
then
  "$stic_scripts_dir"simv_start -tcltk -addfile_list:"$listname"equivalent
else
  echo list empty
fi

if test -s "$listname"problem
then
  echo ; echo '??? PROBLEM ???' ; echo
  "$stic_scripts_dir"simv_start -tcltk -addfile_list:"$listname"problem
fi

rm -f $(cat "$listname"inferior)
rm -f $(cat "$listname"trash)
rm -f $(cat "$listname"unreadable)


