#!/bin/sh
#
# PATH and IFS are here for paranoia's sake
# The path may be modified as necessary.
IFS=''
PATH=/etc:/bin:/usr/bin:/usr/ucb:/usr/etc:/usr/local/bin:$USER/scripts
export PATH IFS

# Correct command-line invocation usage:
Usage="Usage: `basename $0` [-h] new_project_name"

# Generic Tmpfile
Tmpfile=$0.$$

# Clean up temporary file on normal exit or interrupt:
trap 'rm $Tmpfile > /dev/null 2>&1; exit 0' 0 1 2 3 15

# Usage fragment
if [ $# -ne 1 ]
then
  echo $Usage >&2
  exit 1
else
  PROJECT=$1
fi

case $PROJECT in
  -*) echo $Usage >&2 ; exit 1 ;;	# Help, presumably....
esac

# Here we begin cloning this project template...
# We will assume that this is being run IN the project directory
# and will clone it one notch up (presumably in e.g. $HOME/Src if
# one usually keeps the project template in $HOME/Src/project).
mkdir -p ../$PROJECT
if [ ! -d ../$PROJECT ]
then
  echo "Oops, unable to create project directory ../$PROJECT"
  exit 1
fi

# The project directory now exists.  Let's fill it, converting
# filenames and internal variables as needed, one at a time.
# This is as simple as anything, really.

# COPYING
cat COPYING |  \
  sed -e "s/project (standard simple project prototype)/              $PROJECT/" > ../$PROJECT/COPYING

# Makefile
cat Makefile | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/Makefile

# README, copyright.h (straight copies)
cp README ../$PROJECT/README
cp copyright.h ../$PROJECT/copyright.h

# parsecl.c
cat parsecl.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/parsecl.c

# project.1 (man page)
cat project.1 | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/$PROJECT.1

# project.c (main source)
cat project.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/$PROJECT.c

# project.h (main include)
cat project.h | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/$PROJECT.h

# project.spec
cat project.spec | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/$PROJECT.spec

# project_work.c
cat project_work.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_work.c

# project.cvs.time
cat project.cvs.time | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/$PROJECT.cvs.time

# Done, I think, for the moment.
echo "Project directory ../$PROJECT is created and loaded."
echo "It is STRONGLY SUGGESTED that your next step be to put"
echo "$PROJECT under CVS control..."

exit 0

