#!/bin/sh
#
# PATH and IFS are here for paranoia's sake
# The path may be modified as necessary.

# 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_pvm (standard simple pvm project prototype)/              $PROJECT/" > ../$PROJECT/COPYING

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

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

# parsecl.c (master and slave)
cat parsecl_master.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/parsecl_master.c
cat parsecl_slave.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/parsecl_slave.c

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

# project.c (main master/slave source)
cat project_master.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_master.c
cat project_slave.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_slave.c

# project.h (main master/slave include)
cat project_master.h | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_master.h
cat project_slave.h | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_slave.h

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

# project_work.c (master and slave primary work routine)
cat project_master_work.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_master_work.c
cat project_slave_work.c | \
  sed -e "s/project/$PROJECT/" > ../$PROJECT/"$PROJECT"_slave_work.c

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

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

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

# 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

