#!/bin/sh
# $Id: copy_gen,v 6.4 2001/06/23 08:02:49 cher Exp $

# Copyright (C) 1998-2001 Alexander Chernov <cher@ispras.ru>

#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This program 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

werror() {
  echo $@ >&2
  exit 1
}

checked_copy () {
  for j in $files
  do
    [ -f $j ] && cp $j ../${destdir} && echo "$j -> ../$destdir"
  done
}

if [ x"${PROJECT_ENV_PREFIX}" = x ]
then
  if [ -f ../env_prefix.sh ]
  then
  . ../env_prefix.sh
  elif [ -f ../../env_prefix.sh ]
  then
  . ../../env_prefix.sh
  else
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
  if [ x"${PROJECT_ENV_PREFIX}" = x ]
  then
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
fi

home_var_name=${PROJECT_ENV_PREFIX}'_HOME'
config_var_name=${PROJECT_ENV_PREFIX}'_CONFIG'
mode_var_name=${PROJECT_ENV_PREFIX}'_MODE'

eval L_PRJ_HOME='$'${home_var_name}
eval L_PRJ_CONFIG='$'${config_var_name}
eval L_PRJ_MODE='$'${mode_var_name}

export L_PRJ_HOME L_PRJ_CONFIG L_PRJ_MODE

if [ x"${REALG}" = x -o x"${GENG}" = x ]
then
  echo "REALG or GENG environment variable is not set" >&2
  exit 1
fi

destdir=${REALG}

if [ x"${GENG}" = x"${REALG}" ]
then
  echo "Cannot remake a pregenerated file" >&2
  exit 1
fi

if [ -d ${GENG} ]
then
  cd ${GENG}
elif [ -d ../${GENG} ]
then
  cd ../${GENG}
else
  werror "Directory with pregenerated files is not found"
fi

while [ x"$1" != x-- ]
do
  tool=$1
  shift
done
shift

for i
do
  [ x"$i" = x-- ] && break
  if [ x"$tool" = xast -o x"$tool" = xpuma ]
  then
    suffix=`echo $i | sed 's/^.*\.\([^.]*\)$/\1/g'`
    [ x"$suffix" = x ] && werror "File suffix is empty"
    echo "AST: file suffix is $suffix"
    case x"$suffix" in
      xw)
        base=`echo $i | sed 's/^.*yy\([A-Za-z0-9_][A-Za-z0-9_]*\)\.w$/\1/g'`
        ;;
      xc|xh)
        base=`basename $i .$suffix`
        ;;
      xTS)
        [ x"$tool" = xpuma ] && werror "$tool never generates such file"
        base=`basename $i .$suffix`
        ;;
      *)
        werror "$tool never generates such file"
        ;;
    esac
    [ x"$base" = x ] && werror "File base is empty"
    echo "AST: file base is $base"
    files="$base.c $base.h yy$base.w"
    [ x"$tool" = xast ] && files="$files $base.TS"
    checked_copy
  else
    cp `basename $i` ../${destdir}
  fi
done

exit 0

# Do not add stuff after this line
