#!/bin/sh

FILE_PATH_DIRS="
${SYSDEP_DIR_RELATIVE}/../
${SYSDEP_DIR_RELATIVE}/
"
file_get_by_path()
{
  targ_file="$1"
  orig_text="$2"
  for dir in ${FILE_PATH_DIRS}
  do
    if [ -f "${dir}/${targ_file}" ]
    then
      tmp=`head -n 1 "${dir}/${targ_file}"`
      if [ ! -z "${tmp}" ]; then echo "${tmp}"; return 0; fi
    fi
  done
  echo "${orig_text}"
  return 1
}
file_exists_by_path()
{
  targ_file="$1"
  for dir in ${FILE_PATH_DIRS}
  do
    if [ -f "${dir}/${targ_file}" ]; then return 1; fi
  done
  return 0
}
file_show_by_path()
{
  targ_file="$1"
  for dir in ${FILE_PATH_DIRS}
  do
    if [ -f "${dir}/${targ_file}" ]; then echo "${dir}/${targ_file}"; return 1; fi
  done
  return 0
}

if [ $# -lt 2 ]
then
  echo "usage: command file [...]"
  exit 111
fi

command=$1
file=$2
origtext=$3

case "${command}" in
  get)
    file_get_by_path "${file}" "${origtext}"
    exit $?
    ;;
  exists)
    file_exists_by_path "${file}"
    exit $?
    ;;
  show)
    file_show_by_path "${file}"
    exit $?
    ;;
  *)
    echo '${SYSDEP_MODULE}: fatal: unknown command'
    exit 111
    ;;
esac
