#! /bin/sh

#
# Script generating Makefiles in the project directories.
#
# Copyright (C) 1999-2000, by
#
#   Carlo Wood, Run on IRC <carlo@alinoe.com>
#   RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
#   Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
#
# All rights reserved.
#
# See the file LICENSE for copyright information.
#
# This file may be distributed under the terms of the Q Public License 1.0
# as defined by Troll Tech AS of Norway and appearing in the file LICENSE.QPL
# included in the packaging of this file.
#

if [ -z "$PROTODIR" -o -z "$CPPEXT" ]; then
  if [ -z "$PROTODIR" ]; then
    echo "The environment variable PROTODIR is not set!"
    echo "Please set it to point to the prototype directory."
    echo "For example: export PROTODIR=/usr/local/share/prototype"
  fi
  if [ -z "$CPPEXT" ]; then
    echo "The environment variable CPPEXT is not set!"
    echo "Please set it to one of \".cc\", \".cxx\", \".C\" or \".cpp\"."
  fi
  exit 255
fi

if [ -e makefile -a "$1" = "maintainer" ]; then
  echo "There is already a makefile in the current directory!"
  exit 255
fi

if [ "$1" = "maintainer" ]; then
  if [ ! -e Makefile.am -a ! -e Makefile.in -a ! -e Makefile ]; then
    echo "Missing Makefile[.in|.am]: Please finish the package first."
    exit 255;
  fi
  if [ ! -e configure.in ]; then
    echo "Missing configure.in: Please finish the package first."
    exit 255;
  fi
  if [ -e Makefile.am ] ; then
    PACKAGE=`grep AM_INIT_AUTOMAKE configure.in | cut -d, -f1 | cut -d'(' -f2 | sed -e 's% %%g'`
    if [ -z "$PACKAGE" ] ; then
      echo "configure.in does not contain a AM_INIT_AUTOMAKE?"
      echo "Please finish the package first."
      exit 255;
    fi
    cp $PROTODIR/maintainer/makefile makefile
  else
    PACKAGE=`pwd | awk -F/ '{ printf("%s\n", $(NF)); }'`
    sed -e s%'^PACKAGE=.*$'%PACKAGE=$PACKAGE% \
	-e s%'^VERSION=.*'%VERSION=1.0% \
	$PROTODIR/$maintainer/makefile > makefile
  fi
  echo "Package: $PACKAGE"
  exit 0;
fi

if [ -e Makefile ]; then
  echo "There is already a Makefile in the current directory!"
  exit 255
fi

if [ ! -e ../Makefile -a -n "$1" -a "$1" != "base" -a "$1" != "basemain" ]; then
  if [ "$1" = "main" ]; then
    echo "Warning: There seems to be no ../Makefile."
    echo "You probably want to first use \"makeproto base\"."
    echo "If you are sure you want a 'main' Makefile without"
    echo "base, then use \"makeproto basemain\"."
  else
    echo "Please use \"makeproto base\" first."
  fi
  exit 255
fi

# Search for a base Makefile
BASEDIR=""
ABSBASEDIR=""
for i in .. ../.. ../../.. ../../../.. ../../../../..; do
  if [ -z "$ABSBASEDIR" -a -e $i/Makefile ]; then
    TMP=`grep '^BASEDIR *=' $i/Makefile | sed -e 's/BASEDIR *=//' -e 's/[ 	]//g'`
    if [ -n "$TMP" ]; then
      BASEDIR=$i/$TMP
      ABSBASEDIR=`(cd $i/$TMP; pwd)`
      break
    fi
    if [ -n "`grep 'base/PTMakefile' $i/Makefile`" ]; then
      BASEDIR=$i
      ABSBASEDIR=`(cd $i; pwd)`
    fi
  fi
done

if [ -n "$1" ]; then
  WHAT=$1
else
  if [ -z "$ABSBASEDIR" ]; then
    WHAT="base"
  elif expr match `pwd` '.*test[^/]*' > /dev/null; then
    WHAT="test"
  fi
fi

if [ "$WHAT" = "base" ]; then
  echo "New project"
  echo "Creating Makefile and Makedefs.h..."
  cp $PROTODIR/base/Makefile .
  cp $PROTODIR/base/Makedefs.h .
  exit 0
fi

if [ "$WHAT" = "basemain" ]; then
  echo "Single executable project"
  ABSBASEDIR="`pwd`"
  BASEDIR="."
  echo "Creating Makedefs.h..."
  cp $PROTODIR/base/Makedefs.h .
fi

NAME=`echo "$ABSBASEDIR" | sed -e 's%^.*/%%g'`
LIBNAME=`echo "$NAME" | sed -e 's%^lib%%'`

if [ "$WHAT" = "basemain" ]; then
  WHAT="main"
elif [ "$WHAT" = "main" ]; then
  echo "New executable"
  if [ "$BASEDIR" = ".." ]; then
    echo "Adjusting SUBDIRS in ../Makefile"
    SUBDIRS=`grep '^SUBDIRS *=' ../Makefile | sed -e 's%^SUBDIRS *=%%' -e 's%^ *%%' -e 's% +% %g' -e 's% *$%%'`
    LIBDIR=`pwd | sed -e 's%^.*/%%g'`
    if [ -z "$SUBDIRS" ]; then
      SUBDIRS="$LIBDIR"
    else
      SUBDIRS="$SUBDIRS $LIBDIR"
    fi
    mv ../Makefile ../Makefile.bak
    sed -e s%'^SUBDIRS *=.*$'%SUBDIRS="$SUBDIRS"% ../Makefile.bak > ../Makefile
  fi
elif [ "$WHAT" = "lib" ]; then
  echo "New library"
  if [ ! -e $ABSBASEDIR/lib ]; then
    (cd $ABSBASEDIR; mkdir lib)
    echo "Created $ABSBASEDIR/lib"
  fi
  if [ ! -e ".$LIBNAME.version" ]; then
    echo "VERSION_MAJOR=0" > .$LIBNAME.version
    echo "VERSION_MINOR=1" >> .$LIBNAME.version
    echo "VERSION_PATCH=0" >> .$LIBNAME.version
  else
    echo "Warning: .$LIBNAME.version already exists!"
  fi
  if [ "$BASEDIR" = ".." ]; then
    echo "Adjusting SUBDIRS in ../Makefile"
    SUBDIRS=`grep '^SUBDIRS *=' ../Makefile | sed -e 's%^SUBDIRS *=%%' -e 's%^ *%%' -e 's% +% %g' -e 's% *$%%'`
    MAINDIR=`pwd | sed -e 's%^.*/%%g'`
    if [ -z "$SUBDIRS" ]; then
      SUBDIRS="$MAINDIR"
    else
      SUBDIRS="$MAINDIR $SUBDIRS"
    fi
    mv ../Makefile ../Makefile.bak
    sed -e s%'^SUBDIRS *=.*$'%SUBDIRS="$SUBDIRS"% ../Makefile.bak > ../Makefile
  fi
elif [ "$WHAT" = "sub" ]; then
  if [ "$BASEDIR" = ".." ]; then
    echo "Please use \"makeproto [main|lib]\" first."
    exit 255
  fi
  echo "New subdirectory"
elif [ "$WHAT" = "test" ]; then
  echo "New test directory"
else
  echo "Usage:"
  echo "    makeproto [base|main|lib|sub|test]"
  echo
  echo " base : Create Makefile for new project"
  echo " main : Create an executable in this directory"
  echo " lib  : Create a shared library in $ABSBASEDIR/lib"
  echo " sub  : Subdirectory in a \"main\" or \"lib\" source tree"
  echo " test : Create test executables in this directory"
  exit 0
fi

echo "Creating Makefile..."

sed -e s%'^BASEDIR=.*$'%BASEDIR=$BASEDIR% \
    -e s%'^MAIN=.*'%MAIN=$NAME% \
    -e s%'^LIB=.*'%LIB=$LIBNAME% \
    $PROTODIR/$WHAT/Makefile > Makefile
