#! /bin/sh
#
# cfg-sdk -- Copyright (c) 2002 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# epocemx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

die()
{
  echo "*** $@"
  exit 1
}

usage()
{
  echo 'Usage:'
  echo '  cfg-sdk -set PATH'
  echo '  cfg-sdk -show'
  echo '  cfg-sdk -unset'
  exit 1
}

# $1    = SDK directory
# $2    = parent directory
# $3... = equivalent spellings of subdirectory

dirfind()
{
  sdk="$1"
  parent="$2"
  shift 2
  found=
  for x in $*
  do
    if [ -d $parent/$x ]
    then
      found=$x
      break
    fi
  done
  test -n "$found" || die "$sdk: does not contain ER5 C++ SDK (missing: $parent/$1)"
  dir=$found
}

# $1    = sdk
# $2    = directory
# $3... = files
check()
{
  sdk="$1"
  dir=$2
  shift 2
  ok=
  for x
  do
    test -f $dir/$x && ok=yes
  done
  test -n "$ok" || die "$sdk: does not contain ER5 C++ SDK (missing: $dir/$1)"
}

do_set()
{
  test $# = 2 || usage
  test -d "$2" || die "$2: not a directory"
  dirfind $2 $2 epoc32 Epoc32 EPOC32
  e=$2/$dir
  dirfind $2 $e include Include INCLUDE
  i=$e/$dir
  dirfind $2 $e release Release RELEASE
  t=$e/$dir
  dirfind $2 $t marm MARM
  t=$t/$dir
  dirfind $2 $t rel Rel
  l=$t/$dir
  check $2 $i e32def.h E32DEF.H
  check $2 $l euser.lib EUSER.LIB
  rm -f SDK-inc SDK-lib
  ln -s $i SDK-inc || die "ln failed"
  ln -s $l SDK-lib || die "ln failed"
  echo OK
}

do_show()
{
  test $# = 1 || usage
  test -d SDK-inc || die 'No SDK path set'
  test -d SDK-lib || die 'No SDK path set'
  ls -ld SDK-inc SDK-lib
}

do_unset()
{
  test $# = 1 || usage
  rm -f SDK-inc SDK-lib
  echo OK
}

case "$1" in
  -set)   do_set "$@";;
  -show)  do_show "$@";;
  -unset) do_unset "$@";;
  *)     usage
esac
