#!/bin/sh

unix_types="cfront gcc"
vx_types="vx"
install_types=
main_type=cfront

for arg
do
  case $next_arg in
  -unix_types)
    unix_types=$arg
    next_arg=
    ;;
  -vx_types)
    vx_types=$arg
    next_arg=
    ;;
  -install_types)
    install_types=$arg
    next_arg=
    ;;
  -main_type)
    main_type=$arg
    next_arg=
    ;;
  esac

  case $arg in
    -unix | -unix_types | -u)
    next_arg=-unix_types
    ;;
    -vx | -vx_types | -v)
    next_arg=-vx_types
    ;;
    -install | -install_types | -i)
    next_arg=-install_types
    ;;
    -main | -main_type | -m)
    next_arg=-main_type
    ;;
  esac
done

if [ ! "$install_types" ]
then
  install_types="$unix_types $vx_types"
fi

for subdir in $unix_types;
do
  test -d $subdir || mkdir $subdir
  test -d ${INSTALL_DIR}/lib/${subdir} || mkdir ${INSTALL_DIR}/lib/${subdir}
  (cd ${INSTALL_DIR}/lib; rm -f lib${subdir}ipt.a;
   ln -s ${subdir}/libipt.a lib${subdir}ipt.a )
done

for subdir in $vx_types;
do
  test -d $subdir || mkdir $subdir
  test -d ${INSTALL_DIR}/lib/${subdir} || mkdir ${INSTALL_DIR}/lib/${subdir}
done

test -d ${INSTALL_DIR}/include/ipt || mkdir ${INSTALL_DIR}/include/ipt

(cd ${INSTALL_DIR}/include;
 rm -f tcx.h tcxP.h libc.h;
 ln -s ipt/tcx.h tcx.h; ln -s ipt/tcxP.h tcxP.h; ln -s ipt/libc.h libc.h; )

(cd ${INSTALL_DIR}/lib;
 rm -f libipt.a libtcx.a;
 ln -s ${main_type}/libipt.a libipt.a; ln -s ${main_type}/libipt.a libtcx.a)

cd config

echo "/* Composite definition file, generated by configure */" > composite.def
echo "" > composite.def
echo "MAINTYPE = $main_type" >> composite.def

for type in $unix_types;
do
  cat ${type}.def >> composite.def
  sed "s/<type>/${type}/g" unixbase.def >> composite.def
done

for type in $vx_types;
do
  cat ${type}.def >> composite.def
  sed "s/<type>/${type}/g" vxbase.def >> composite.def
done

echo "#define VxLibObject(file)  @@\\" >> composite.def
for type in $vx_types;
do
  echo "@@\\" >> composite.def
  echo "${type}_LIB_OBJS += ${type}/file.o   @@\\" >> composite.def
  echo "${type}_LIB_SRCS += file.cc  @@\\" >> composite.def
  echo "	Make${type}Object(\$(IPTVXLIB),file)   @@\\" >> composite.def
done
echo "" >> composite.def

echo "#define UnixLibObject(file)  @@\\" >> composite.def
for type in $unix_types;
do
  echo "@@\\" >> composite.def
  echo "${type}_LIB_OBJS += ${type}/file.o   @@\\" >> composite.def
  echo "${type}_LIB_SRCS += file.cc  @@\\" >> composite.def
  echo "Make${type}Object(\$(IPTLIB),file,\$(EMPTY))   @@\\" >> composite.def
done
echo "" >> composite.def

echo "#define UnixLibObjectEnv(file,env)  @@\\" >> composite.def
for type in $unix_types;
do
  echo "@@\\" >> composite.def
  echo "${type}_LIB_OBJS += ${type}/file.o   @@\\" >> composite.def
  echo "${type}_LIB_SRCS += file.cc  @@\\" >> composite.def
  echo "	Make${type}Object(\$(IPTLIB),file,env)   @@\\" >> composite.def
done
echo "" >> composite.def

echo "#define MakeIptLib()      @@\\" >> composite.def
echo "\$(IPTLIB): \$(MAINTYPE)/\$(IPTLIB)	 		@@\\" >> composite.def
for type in $unix_types;
do
  echo "	Make${type}Lib()   @@\\" >> composite.def
done
for type in $vx_types;
do
  echo "	Make${type}Lib()   @@\\" >> composite.def
done
echo "" >> composite.def

echo "#define MakeIptProgram(name,file)      @@\\" >> composite.def
echo "SRCS += file.\$(CCSUFFIX)    				@@\\" >> composite.def
echo "name: \$(MAINTYPE)/name	 		@@\\" >> composite.def
for type in $unix_types;
do
  echo "  Make${type}Program(name,file)   @@\\" >> composite.def
done
echo "" >> composite.def

echo "#define MakeIptCProgram(name,file)      @@\\" >> composite.def
echo "SRCS += file.\$(CSUFFIX)    				@@\\" >> composite.def
echo "name: \$(MAINTYPE)/name	 		@@\\" >> composite.def
for type in $unix_types;
do
  echo "	Make${type}CProgram(name,file)   @@\\" >> composite.def
done
echo "" >> composite.def

echo "clean::" >> composite.def
for type in $unix_types;
do
    echo "	\$(RM) ${type}/\$(ALL_FILES)" >> composite.def
done
for type in $vx_types;
do
    echo "	\$(RM) ${type}/\$(ALL_FILES)" >> composite.def
done
echo "" >> composite.def

for type in $install_types;
do
    echo "install:: install.$type" >> composite.def
    echo "" >> composite.def
    echo "depend:: depend.$type" >> composite.def
    echo "" >> composite.def
done

cd ..
imake -Ttemplate -Iconfig
