#!/bin/sh
#
# newfile : create a new project file based on templates
#
# This file Version	$Revision$
#
# Creation date:		Wed Jun  3 19:46:03 CEST 1998
# Last modification: 	$Date$
# By:					$Author$
# Current State:		$State$
#
# Author:				root
#
# Copyright (C) 1994-1998 by Ripley Software Development 
# All Rights Reserved
#
# This file is part of the Newt Development Environment
#
##############################################################################
#
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this script; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
##############################################################################
#
# $Source$
#
##############################################################################
#
# $Log$
#
##############################################################################

# Declaration of variables

# Default directories
skel_dir=${NEWT_SKELETON_DIR:-/usr/local/skeleton}
prj_file=${NEWT_PROJECT_FILE:-/usr/local/skeleton/projects}

# Default values for variable substitution in the .skel files
project=NONE
prj_desc="This file is part of no particular project."
lictype=""
apptype="program"

# Get the real program name
progname=`basename $0`

function usage
{
	echo "Usage: "
	echo
	echo "    newfile name [project]"
	echo
	echo "Where:"
	echo "    name   : name of file to be created. If it already exists you "
	echo "             will be asked if you want to overwrite it."
	echo "             An extension is not required, it will be added automatically."
	echo "    project: name of the project of which this new file will be"
	echo "             part."
	echo
	echo "The current skeleton directory is: $skel_dir"
	echo "The current projects file is     : $prj_file"
	exit 1
}

# Check arguments
case $# in
	2)
		filename="$1"
		project=$2
		;;
	1)
		filename="$1"
		;;
	*)
		usage
		;;
esac

# Check if we have a skeleton file for the name under which we have
# been called.

# Pick out the file type, it is found by stripping the first 3 characters
# of the name by which we have been called.
#
# Examples:
#
# newc  -> create a new .c file
# newh  -> create a new .h file
# newsh -> create a new .sh file
#
skel_type=`echo $progname | cut -b 4-`

# Skeleton file to use
skel_file=$skel_dir/$skel_type.skel

# Check if a skeleton file for this file type exists
if [ ! -f "$skel_file" ] ; then
	echo ".$skel_type: unsupported file type."
	echo "No appropriate skeleton found in $skel_dir".
	exit 1
fi

# If we have been given a project, check if it exists. If so, extract
# the different fields. If not, ask if the user wishes to continue.
if [ "$project" != "NONE" ] ; then
	project_entry=`grep -i -e "^$project" $prj_file`

	if [ "x$project_entry" = "x" ] ; then
		echo "Warning: no project entry found for $project in"
		echo "$prj_file."
		echo "Do you wish to continue? (y/n)"
		read answer
		case $answer in
			y|Y)
				echo "Continuing with default values..."
				;;
			*)
				exit 1
				;;
		esac
	else
		# split project entry into separate fields
		prj_desc=`echo $project_entry | cut -d : -f 2`
		apptype=`echo $project_entry | cut -d : -f 3`
		lictype=`echo $project_entry | cut -d : -f 4`
	fi
fi

# Now check the file that is to be created.
# First check if an extension was given on the command line.

ext=`echo "$filename" | cut -d'.' -f 2`

# cut echo's back the original input if no substition could be made
if [ "$ext" = "$filename" ] ; then
	filename="$filename.$skel_type"
fi

# Check if the file already exists
if [ -f "$filename" ] ; then
	echo "$filename already exists."
	echo "Do you wish to overwrite it? (y/n)"
	read answer
	case $answer in
		y|Y)
			rm -f "$filename"
			# sanity check
			if [ -f "$filename" ] ; then
				echo "Error removing $filename. $filename not re-created."
				exit 1
			fi
			;;
		*)
			echo "$filename not overwritten."
			exit 1
			;;
	esac
fi

# Now get the author's name
user=`whoami`
auth1=`grep -e "^$user" /etc/passwd | cut -d : -f 5 | cut -d , -f 1`

# sanity check
if [ "x$auth1" = "x" ] ; then
	echo "Failed to retrieve user information. Are you using NIS?"
	echo "Using $user instead."
	auth1=$user
fi

# escape any quotes
author=`echo "$auth1" | sed -e "s/'/\\\\'/g"`

# If this is a header file, we should transform the filename to
# an appropriate DEFINE entry
# The characters # -. '`!#,:;+ are replaced by an underscore.
if [ "$skel_type" = "h" ] ; then
	define=_`basename "$filename" | sed -e "s/\\([-. '\\\`!#,:;+]\\)/_/g"`_
fi

# get basepart of the filename
base=`basename "$filename"`

# As last part, get system date
date=`date`

# Run sed over the given skeleton file, and voila, we've got the new file!
sed -e "s%@AUTHOR@%$author%g" \
	-e "s%@DATE@%$date%g" \
	-e "s%@PROJECT@%$prj_desc%g" \
	-e "s%@PROJECTNAME@%$project%g" \
	-e "s%@FILE@%$base%g" \
	-e "s%@APPTYPE@%$apptype%g" \
	-e "s%@LICTYPE@%$lictype%g" \
	-e "s%@DEFINE@%$define%g" \
	$skel_file > $filename

# all done
