1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#!/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
|