#!/bin/bash

# Quick hack for creating project source files

error_if_exists ()
{
    test -n "$1" -a -e "$1" && {
        echo \"$1\" already exists
        exit 1
    }
}


error_if_not_exists ()
{
    test -n "$1" -a ! -e "$1" && {
        echo \"$1\" must exist
        exit 1
    }
}


license ()
{
cat << EOF_
/* Copyright (c) 2003-2005, Sven Suursoho
 * See LICENSE for details */

EOF_
}


create_module ()
{
    # Check that we won't overwrite anything
    error_if_exists "cool/${MODULE}/"
    file="cool/${MODULE}.hpp"
    error_if_exists "$file"

    # create new module directory
    mkdir "cool/${MODULE}/"

    license > ${file}
cat >> ${file} << EOF_
#ifndef cool_${MODULE}_hpp_included
#define cool_${MODULE}_hpp_included

/**
 * \\defgroup cool_${MODULE} ${MODULE}
 * \\ingroup cool
 * \\{
 * Short description
 *
 * Header(s):
 *  - \\ref ${CLASS}.hpp
 * \\}
 */

#include <cool/${MODULE}/${CLASS}.hpp>


#endif  /* cool_${MODULE}_hpp_included */
EOF_
}


create_exception ()
{
    error_if_not_exists "cool/${MODULE}/"
    file="cool/${MODULE}/exception.hpp"
    error_if_exists "${file}"

    # Check that we won't overwrite anything
    license > ${file}
cat >> ${file} << EOF_
#ifndef cool_${MODULE}_exception_hpp_included
#define cool_${MODULE}_exception_hpp_included

/**
 * \\file exception.hpp
 * \\author Sven Suursoho
 *
 * \\brief Class(es): cool::${CLASS}_error
 *
 * In module \\ref cool_${MODULE}.
 */

#include <cool/cool.hpp>
#include <cool/exception/exception.hpp>


namespace cool {
/** \\addtogroup cool_${MODULE} */
/** \\{ */


/**
 * \\class ${CLASS}_error
 * \\brief
 */
EXCEPTION_CLASS(${CLASS}_error, exception);


/** \\} */
}   // namespace cool


#endif  /* cool_${MODULE}_exception_hpp_included */
EOF_
}


create_header ()
{
    error_if_not_exists "cool/${MODULE}/"
    file="cool/${MODULE}/${CLASS}.hpp"
    error_if_exists "${file}"

    license > ${file}
cat >> ${file} << EOF_
#ifndef cool_${MODULE}_${CLASS}_hpp_included
#define cool_${MODULE}_${CLASS}_hpp_included

/**
 * \\file ${CLASS}.hpp
 * \\author Sven Suursoho
 *
 * \\brief Class(es): cool::${CLASS}
 *
 * In module \\ref cool_${MODULE}.
 */

#include <cool/cool.hpp>
#include "bits/${CLASS}.hpp"


namespace cool {
/** \\addtogroup cool_${MODULE} */
/** \\{ */


/**
 * \\brief
 */
class COOL_API ${CLASS}
{
  public:

      ${CLASS} ();
      ~${CLASS} ();


  private:
};


/** \\} */
}   // namespace cool


#endif  /* cool_${MODULE}_${CLASS}_hpp_included */
EOF_
}


create_bits ()
{
    error_if_not_exists "cool/${MODULE}/"
    test ! -e "cool/${MODULE}/bits/" && mkdir "cool/${MODULE}/bits/"
    file="cool/${MODULE}/bits/${CLASS}.hpp"
    error_if_exists "${file}"

    license > ${file}
cat >> ${file} << EOF_
#ifndef cool_${MODULE}_bits_${CLASS}_hpp_included
#define cool_${MODULE}_bits_${CLASS}_hpp_included


namespace cool {
namespace bits {
namespace ${MODULE} {


// ${CLASS}


}   // namespace ${MODULE}
}   // namespace bits
}   // namespace cool


#endif  /* cool_${MODULE}_bits_${CLASS}_hpp_included */
EOF_
}


create_source ()
{
    test ! -e "src/${MODULE}/" && mkdir "src/${MODULE}/"
    file="src/${MODULE}/${CLASS}.cpp"
    error_if_exists "${file}"

    license > ${file}
cat >> ${file} << EOF_
#include <cool/${MODULE}/${CLASS}.hpp>


namespace cool {


${CLASS}::${CLASS} ()
{
}


${CLASS}::~${CLASS} ()
{
}


}   // namespace cool
EOF_
}


create_test ()
{
    file="tests/${CLASS}.cpp"
    error_if_exists "${file}"

    license > ${file}
cat >> ${file} << EOF_
#undef NDEBUG
#include <assert.h>
#if defined(INCLUDE_MODULE)
#  include <cool/${MODULE}.hpp>
#else
#  include <cool/${MODULE}/${CLASS}.hpp>
#endif
#include iostream_header


int
main (int argc, char *argv[])
{
  try {
      cool::${CLASS} ${CLASS};
  }
  catch (STD_ exception &e) {
      STD_ cout << e.what() << STD_ endl;
      return 1;
  }

  return 0;
}
EOF_
}


if test $# != 3 -o "`type -t create_${1}`" != "function"; then
cat << USAGE_ >&2
Usage:
  `basename $0` source module class

where:
  source ::= { module | exception | header | bits | source | test }
  module ::= name of module where class belongs to
  class ::= name of new class
USAGE_
    exit 1
fi


MODULE=${2}
CLASS=${3}
create_${1}
