#! /bin/bash

#
# Script generating Makefiles in the project directories.
#
# Copyright (C) 1999
#
# 1024/624ACAD5 1997/01/26 Carlo Wood, Run on IRC <carlo@runaway.xs4all.nl>
# Key fingerprint = 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.
#

function find_basedir()
{
  TMP=`grep '^BASEDIR *=' $1/Makefile | sed -e 's/BASEDIR *=//' -e 's/[ 	]//g'`
  if [ -n "$TMP" ]; then
    BASEDIR=$1/$TMP
    ABSBASEDIR=`(cd $1/$TMP; pwd)`
    return
  fi
  if [ -n "`grep 'base/PTMakefile' $1/Makefile`" ]; then
    BASEDIR=$1
    ABSBASEDIR=`(cd $1; pwd)`
  fi
}

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

if [ ! -e ../Makefile -a -n "$1" -a "$1" != "base" ]; then
  echo "Please use \"makeproto base\" first."
  exit -1
fi

# Search for a base Makefile
BASEDIR=""
ABSBASEDIR=""
for i in .. ../.. ../../.. ../../../.. ../../../../..; do
  if [ -z "$ABSBASEDIR" -a -e $i/Makefile ]; then
    find_basedir $i;
  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

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

if [ "$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 -1
  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
