#!/bin/bash

#
# 
# Copyright (c) 2007-2012,
#   Lingxiao Jiang         <lxjiang@ucdavis.edu>
#   Ghassan Misherghi      <ghassanm@ucdavis.edu>
#   Zhendong Su            <su@ucdavis.edu>
#   Stephane Glondu        <steph@glondu.net>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
#

check_or_create_dir()
{
  if [[ ! -d "${!1}" ]]; then
  if [[ -e "${!1}" ]]; then
    echo "Error: $1 should be a directory. Check your config"
    exit 1
  else
    mkdir "${!1}"
    if [[ $? -ne 0 ]]; then
      echo "Error: $1 doesn't exist and cannot be created. Check your config"
      exit 1
    fi
  fi
fi
}

check_file()
{
	if [[ ! -f $1 ]]; then
		echo "Error: missing file $1. Check your config"
		exit 1
	fi
}

check_var()
{
	if [[ -z ${!1} ]];
	then
		echo "Error: $1 not set in config. Check your config"
		exit 1
	fi

	export $1
}

check_exe()
{
	if (which "$1" >& /dev/null); then
		return 0
	else
		echo "Error: missing required program: $1"
		exit 1
	fi
}

if [[ -e config ]];
then
	. config
else
	echo "Error: no config file in current directory"
	exit 0
fi

check_var "DECKARD_DIR"
check_var "FILE_PATTERN"
check_var "SRC_DIR"

check_var "VECTOR_DIR"
check_or_create_dir "VECTOR_DIR"
check_var "CLUSTER_DIR"
check_or_create_dir "CLUSTER_DIR"
check_var "TIME_DIR"
check_or_create_dir "TIME_DIR"

check_var "VGEN_EXEC"
check_file "$VGEN_EXEC"
check_var "GROUPING_EXEC"
check_file "$GROUPING_EXEC"
check_var "CLUSTER_EXEC"
check_file "$CLUSTER_EXEC"
check_var "POSTPRO_EXEC"
check_file "$POSTPRO_EXEC"

check_var "MIN_TOKENS"
check_var "STRIDE"
#check_var "DISTANCE"
check_var "SIMILARITY"
check_var "GROUPING_S"
#check_var "GROUPING_D"
#check_var "GROUPING_C"

check_exe "which"
check_exe "env"
check_exe "python"
check_exe "awk"
check_exe "sed"
check_exe "grep"
check_exe "find"
check_exe "bash"
check_exe "cat"
check_exe "touch"
check_exe "head"
check_exe "tail"
#check_exe "time"



