#! /bin/sh
# Initialize adas tables
# 07/09/01 C.Ludescher
# May be called from Makefile "make tables"
#
if [ "$1" ]; then
  if [ $1 = "help" -o $1 = "-h" ]; then
    echo "usage: adasinit [restart] [PATH <path of adas_init>] [DATA <dir of ORNL6086.DAT> ]"
    echo "  To initialize:"
    echo "       adasinit  "     
    echo "       adasinit PATH <path of adas_init> "     
    echo "       adasinit DATA <dir of adas data> "
    echo "       adasinit DATA <dir of adas data> PATH <path of adas_init> "
    echo "  To delete/initialize:"     
    echo "       adasinit restart "
    echo "       adasinit restart PATH <path of adas_init> "
    echo "       adasinit restart DATA <dir of adas data> "
    echo "       adasinit restart DATA <dir of adas data> PATH <path of adas_init> "
      exit
  fi
fi
echo "make"
if [ "$ADASDIR" ]; then
    while [ -n "$1" ]; do
       case $1 in
        restart )
            if [ -d $ADASDIR ]; then
                rm -rf $ADASDIR
            fi
            ;;
	DATA )
	    DATA=$2
	    echo "DATA = $DATA"
            shift ;;
	PATH )
	    ppath=$2
	    echo "PATH = $ppath"
            shift ;;
       esac
       shift
    done
    
    if [ ! -d $ADASDIR ]; then
       mkdir $ADASDIR
    fi
    if [ ! -d $ADASDIR/data ]; then
	echo "make tree directory for adas data"
        if [ -n "$CODESYSDIR" ]; then
	   mktreedir $CODESYSDIR/source/adas/data $ADASDIR/data
	   echo "copy adas data"
	   cp -r $CODESYSDIR/source/adas/data/*  $ADASDIR/data/.
        elif [ -d ../data/bms ]; then
           echo "cp -r ../data $ADASDIR/"
           cp -r ../data $ADASDIR/
        elif [ -d ../../data/bms ]; then
           cp -r ../../data $ADASDIR/
        elif [ -d $DATA/bms ]; then
           cp -r $DATA $ADASDIR/
        else
          echo "   >>>>> can not find ADAS data <<<<<"
	  exit 1
        fi
    fi
    if [ ! -d $ADASDIR/tables/cx ]; then 
	echo "make tables"
        mkdir -p $ADASDIR/tables/cx
        mkdir -p $ADASDIR/tables/ii
        mkdir -p $ADASDIR/tables/ei
        mkdir -p $ADASDIR/tables/sv
    fi
    if [ "$LOCAL" ]; then
        $LOCAL/exe/adas_init
    elif [ -n "$ppath" ]; then
        $ppath/adas_init
    else
        adas_init
    fi
else
        echo "   >>>>> You must define ADASDIR <<<<<"
	echo "   >>>>> e.g.: setenv ADASDIR $HOME/adas"
fi
