#!/bin/sh
#
# Script to add a new function to an existing class
#
# Copyright 1999-2000 Trolltech AS.  All rights reserved.
#


# load the helper functions
. q_functions


if [ $# -lt 4 ]
then
echo "Usage: q_add_function AccessModifier RetType ExistingClassName NewFunctionName ArgList"
print_example_usage
exit
fi


ACCESS_MODIFIER=$1
RET_TYPE=$2
EXISTING_CLASS_NAME=`echo $3 | cut -d ":" -f 1`
NEW_FUNCTION_NAME=`echo $3 | cut -d ":" -f 3`
ARG_LIST=$4


EXISTING_CLASS_NAME_LOWER=`echo $EXISTING_CLASS_NAME | tr "[A-Z]" "[a-z]"`
EXISTING_CLASS_HEADER_FILE="$EXISTING_CLASS_NAME_LOWER".h
EXISTING_CLASS_SOURCE_FILE="$EXISTING_CLASS_NAME_LOWER".cpp
ORIGINAL_CLASS_HEADER_FILE="$EXISTING_CLASS_NAME_LOWER".h.orig


function print_source_file
{
cat << END


$RET_TYPE $EXISTING_CLASS_NAME::$NEW_FUNCTION_NAME$ARG_LIST
{
}
END
}


function print_function_definition
{
cat << END
$ACCESS_MODIFIER
    $RET_TYPE $NEW_FUNCTION_NAME$ARG_LIST;
END
}


function print_new_header_file
{
get_number_of_lines
get_first_line_of_class_definition
if [ -z "$LINE" ]
then
cat << END
/*
    No good, can't find $EXISTING_CLASS_NAME class definition anywhere.
    You'll have to manually edit the header file to add the
    following function definition to the $EXISTING_CLASS_NAME class:

END
print_function_definition
echo -e "\n*/"
cat $ORIGINAL_CLASS_HEADER_FILE
else
head -n $LINE $ORIGINAL_CLASS_HEADER_FILE
print_function_definition
tail -n `expr $LINES - $LINE` $ORIGINAL_CLASS_HEADER_FILE
fi
}


[ -f $EXISTING_CLASS_HEADER_FILE ] || { echo "file $EXISTING_CLASS_HEADER_FILE not found" ; exit ; }

# Backup file
mv $EXISTING_CLASS_HEADER_FILE $ORIGINAL_CLASS_HEADER_FILE


print_source_file     >> $EXISTING_CLASS_SOURCE_FILE
print_new_header_file >> $EXISTING_CLASS_HEADER_FILE