/brlcad/branches/bottie/sh/template.sh

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git · Shell · 179 lines · 93 code · 15 blank · 71 comment · 13 complexity · 20d8805ff86db78bf0f527e5500fb663 MD5 · raw file

  1. # T E M P L A T E . S H
  2. # BRL-CAD
  3. #
  4. # Copyright (c) 2007-2011 United States Government as represented by
  5. # the U.S. Army Research Laboratory.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. #
  11. # 1. Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. #
  14. # 2. Redistributions in binary form must reproduce the above
  15. # copyright notice, this list of conditions and the following
  16. # disclaimer in the documentation and/or other materials provided
  17. # with the distribution.
  18. #
  19. # 3. The name of the author may not be used to endorse or promote
  20. # products derived from this software without specific prior written
  21. # permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  24. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  27. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. #
  35. ###
  36. #
  37. # Generates an empty template file with a standard header and footer
  38. # already applied. Basically, this is a wrapper on a couple other
  39. # steps and scripts that are frequently used to stub out new files
  40. # being added.
  41. #
  42. # Examples:
  43. #
  44. # # create a new template C file with an LGPL header
  45. # sh/template.sh lgpl somefile.c
  46. #
  47. # # create a new shell script file under the BSD license
  48. # sh/template.sh bsd newscript.sh
  49. #
  50. ###
  51. LICE="$1"
  52. FILE="$2"
  53. PROJ="$3"
  54. COPY="$4"
  55. # force locale setting to C so things like date output as expected
  56. LC_ALL=C
  57. USAGE="Usage: $0 LICENSE FILE [ProjectName] [CopyrightHolder]"
  58. ##################
  59. # validate input #
  60. ##################
  61. if [ "x$LICE" = "x" ] ; then
  62. echo "ERROR: must give a license type (BSD, BDL, LGPL)"
  63. echo "$USAGE"
  64. exit 1
  65. fi
  66. case $LICE in
  67. bsd|BSD)
  68. LICE=BSD
  69. ;;
  70. bdl|BDL)
  71. LICE=BDL
  72. ;;
  73. lgpl|LGPL)
  74. LICE=LGPL
  75. ;;
  76. *)
  77. echo "ERROR: Unknown or unsupported license type: $LICE"
  78. echo "License should be one of BSD, BDL, LGPL"
  79. echo "$USAGE"
  80. exit 1
  81. ;;
  82. esac
  83. if [ "x$FILE" = "x" ] ; then
  84. echo "ERROR: must give the name/path of a file to check/update"
  85. echo "$USAGE"
  86. exit 1
  87. fi
  88. # create the file with sanity checks
  89. touch "$FILE"
  90. if [ ! -f "$FILE" ] ; then
  91. echo "ERROR: unable to find $FILE"
  92. exit 1
  93. elif [ ! -r "$FILE" ] ; then
  94. echo "ERROR: unable to read $FILE"
  95. exit 2
  96. elif [ ! -w "$FILE" ] ; then
  97. echo "ERROR: unable to write to $FILE"
  98. exit 2
  99. fi
  100. # set up permissions to something reasonable for revision control
  101. chmod ug+rw "$FILE"
  102. case "x$FILE" in
  103. x*.sh)
  104. # shell scripts should have execute bit set
  105. chmod ug+x "$FILE"
  106. ;;
  107. esac
  108. # find the header script
  109. for dir in `dirname $0` . .. sh ../sh ; do
  110. header_sh="$dir/header.sh"
  111. if [ -f "$header_sh" ] ; then
  112. break
  113. fi
  114. done
  115. if [ ! -f "$header_sh" ] ; then
  116. rm -f "$FILE"
  117. echo "ERROR: Unable to find the header.sh script"
  118. echo " Searched for header.sh in:"
  119. echo " `dirname $0`:.:..:sh:../sh"
  120. exit 1
  121. fi
  122. # apply the header
  123. output="`/bin/sh \"$header_sh\" \"$LICE\" \"$FILE\" \"$PROJ\" \"$COPY\" 2>&1`"
  124. if [ $? -ne 0 ] ; then
  125. rm -f "$FILE"
  126. echo "$output"
  127. echo "ERROR: the header failed to apply, aborting"
  128. exit 1
  129. fi
  130. # get rid of the backup
  131. rm -f "$FILE.backup"
  132. # find the footer script
  133. for dir in `dirname $0` . .. sh ../sh ; do
  134. footer_sh="$dir/footer.sh"
  135. if [ -f "$footer_sh" ] ; then
  136. break
  137. fi
  138. done
  139. if [ ! -f "$footer_sh" ] ; then
  140. rm -f "$FILE"
  141. echo "ERROR: Unable to find the footer.sh script"
  142. echo " Searched for footer.sh in:"
  143. echo " `dirname $0`:.:..:sh:../sh"
  144. exit 1
  145. fi
  146. # apply the footer
  147. output="`/bin/sh \"$footer_sh\" \"$FILE\" 2>&1`"
  148. if [ $? -ne 0 ] ; then
  149. rm -f "$FILE"
  150. echo "$output"
  151. echo "ERROR: the header failed to apply, aborting"
  152. exit 1
  153. fi
  154. # get rid of the backup
  155. rm -f "$FILE.backup"
  156. echo "$FILE successfully created with $LICE license"
  157. # Local Variables:
  158. # mode: sh
  159. # tab-width: 8
  160. # sh-indentation: 4
  161. # sh-basic-offset: 4
  162. # indent-tabs-mode: t
  163. # End:
  164. # ex: shiftwidth=4 tabstop=8