PageRenderTime 168ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 1ms

/brlcad/tags/rel-7-0-1/sh/footer.sh

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
Shell | 294 lines | 171 code | 28 blank | 95 comment | 21 complexity | 7df7ed54b7c7aba565264cf61fdef3d7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. #!/bin/bash
  2. # F O O T E R . S H
  3. # BRL-CAD
  4. #
  5. # Copyright (c) 2004 United States Government as represented by the
  6. # U.S. Army Research Laboratory.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. #
  15. # 2. Redistributions in binary form must reproduce the above
  16. # copyright notice, this list of conditions and the following
  17. # disclaimer in the documentation and/or other materials provided
  18. # with the distribution.
  19. #
  20. # 3. The name of the author may not be used to endorse or promote
  21. # products derived from this software without specific prior written
  22. # permission.
  23. #
  24. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  25. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  28. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. #
  36. ###
  37. #
  38. # This script ensures that a file has an emacs/vi variable footer with
  39. # the requested indentation settings.
  40. #
  41. # The script assumes one file as the argument, so example use might be:
  42. # find . -type f -and \( -name \*.sh -or -name \*.c -or -name \*.h -or -name \*.tcl -or -name \*.tk -or -name \*.itcl -or -name \*.itk -or -name \*.pl \) -not -regex '.*src/other.*' -exec sh/footer.sh {} \;
  43. #
  44. # bash arrays are actually used for convenience, hence why bash and
  45. # not sh.
  46. #
  47. # Author -
  48. # Christopher Sean Morrison
  49. #
  50. # Source -
  51. # The U.S. Army Research Laboratory
  52. # Aberdeen Proving Ground, Maryland 21005-5068 USA
  53. ###
  54. FILE="$1"
  55. # where are the tap stops?
  56. tab_width=8
  57. # what level indentation?
  58. indentation=4
  59. ##################
  60. # validate input #
  61. ##################
  62. if [ "x$FILE" = "x" ] ; then
  63. echo "ERROR: must give the name/path of a file to check/update"
  64. exit 1
  65. elif [ ! -f "$FILE" ] ; then
  66. echo "ERROR: unable to find $FILE"
  67. exit 1
  68. elif [ ! -r "$FILE" ] ; then
  69. echo "ERROR: unable to read $FILE"
  70. exit 2
  71. elif [ ! -w "$FILE" ] ; then
  72. echo "ERROR: unable to write to $FILE"
  73. exit 2
  74. fi
  75. if [ "x$2" != "x" ] ; then
  76. echo "ERROR: script only supports single file arguments"
  77. exit 3
  78. fi
  79. ########################
  80. # figure out file type #
  81. ########################
  82. # mode is the emacs major mode
  83. # mode_vars are the indentation variables that need to be set
  84. # wrap is whether or not in needs to be incased in /* */
  85. # commentchar is the comment character to prefex each line
  86. ###
  87. case $FILE in
  88. *.sh)
  89. echo "$FILE is a shell script"
  90. mode="sh"
  91. mode_vars="sh-indentation sh-basic-offset"
  92. wrap=0
  93. commentchar="#"
  94. ;;
  95. *.c)
  96. echo "$FILE is a C source file"
  97. mode="C"
  98. mode_vars="c-basic-offset"
  99. wrap=1
  100. commentchar="*"
  101. ;;
  102. *.h)
  103. echo "$FILE is a C header"
  104. mode="C"
  105. mode_vars="c-basic-offset"
  106. wrap=1
  107. commentchar="*"
  108. ;;
  109. *.tcl)
  110. echo "$FILE is a Tcl source file"
  111. mode="Tcl"
  112. mode_vars="c-basic-offset tcl-indent-level"
  113. wrap=0
  114. commentchar="#"
  115. ;;
  116. *.tk)
  117. echo "$FILE is a Tk source file"
  118. mode="Tcl"
  119. mode_vars="c-basic-offset tcl-indent-level"
  120. wrap=0
  121. commentchar="#"
  122. ;;
  123. *.itcl)
  124. echo "$FILE is a IncrTcl source file"
  125. mode="Tcl"
  126. mode_vars="c-basic-offset tcl-indent-level"
  127. wrap=0
  128. commentchar="#"
  129. ;;
  130. *.itk)
  131. echo "$FILE is a IncrTk source file"
  132. mode="Tcl"
  133. mode_vars="c-basic-offset tcl-indent-level"
  134. wrap=0
  135. commentchar="#"
  136. ;;
  137. *.pl)
  138. echo "$FILE is a Perl source file"
  139. mode="Perl"
  140. mode_vars="c-basic-offset perl-indent-level"
  141. wrap=0
  142. commentchar="#"
  143. ;;
  144. *)
  145. echo "WARNING: $FILE has an unknown filetype"
  146. ;;
  147. esac
  148. #################################
  149. # prepare emacs variable arrays #
  150. #################################
  151. variables=( ${variables[@]} mode )
  152. values=( ${values[@]} $mode )
  153. variables=( ${variables[@]} tab-width )
  154. values=( ${values[@]} $tab_width )
  155. for mv in $mode_vars ; do
  156. variables=( ${variables[@]} $mv )
  157. values=( ${values[@]} $indentation )
  158. done
  159. variables=( ${variables[@]} indent-tabs-mode )
  160. values=( ${values[@]} t )
  161. ##############################
  162. # generate the comment block #
  163. ##############################
  164. comment_block="
  165. "
  166. if [ "x$wrap" = "x1" ] ; then
  167. comment_block="${comment_block}/`echo "${commentchar}"`
  168. "
  169. fi
  170. if [ "x$wrap" = "x1" ] ; then
  171. # pretty-indent the comment block
  172. prefixspace=" "
  173. fi
  174. # use temp vars so that emacs doesn't think this line is a local var block too
  175. do_not="Local"
  176. parse="Variables"
  177. comment_block="${comment_block}`echo "${prefixspace}${commentchar} ${do_not} ${parse}:"`
  178. "
  179. index=0
  180. for var in ${variables[@]} ; do
  181. comment_block="${comment_block}`echo "${prefixspace}${commentchar} ${var}: ${values[$index]}"`
  182. "
  183. index="`expr $index \+ 1`"
  184. done
  185. comment_block="${comment_block}`echo "${prefixspace}${commentchar} End:"`
  186. "
  187. comment_block="${comment_block}`echo "${prefixspace}${commentchar} ex: shiftwidth=$indentation tabstop=$tab_width"`"
  188. if [ "x$wrap" = "x1" ] ; then
  189. comment_block="${comment_block}
  190. "
  191. comment_block="${comment_block}`echo " ${commentchar}/"`"
  192. fi
  193. #########################
  194. # check the emacs block #
  195. #########################
  196. matching_found=0
  197. index=0
  198. for var in ${variables[@]} ; do
  199. existing_var=`cat $FILE | grep -i "${prefixspace}[${commentchar}] ${var}:" | sed 's/:/ /g' | awk '{print $3}'`
  200. existing_suffix=`cat $FILE | grep -i "${prefixspace}[${commentchar}] ${var}:" | sed 's/:/ /g' | awk '{print $4}'`
  201. if [ "x$existing_var" = "x" ] ; then
  202. echo "$var not found"
  203. else
  204. matching_found="`expr $matching_found \+ 1`"
  205. # echo "found $var=$existing_var"
  206. if [ "x$existing_var" != "x${values[$index]}" ] ; then
  207. echo "$var does not match ... fixing"
  208. perl -pi -e "s/(${prefixspace}[${commentchar}] ${var}:.*)/${prefixspace}${commentchar} ${var}: ${values[$index]}/i" $FILE
  209. elif [ "x$existing_suffix" != "x" ] ; then
  210. echo "$var has trailing goo ... fixing"
  211. perl -pi -e "s/(${prefixspace}[${commentchar}] ${var}:.*)/${prefixspace}${commentchar} ${var}: ${values[$index]}/i" $FILE
  212. fi
  213. fi
  214. index="`expr $index \+ 1`"
  215. done
  216. ######################
  217. # check the vi block #
  218. ######################
  219. existing_vi="`cat $FILE | grep -i "${prefixspace}${commentchar} ex:"`"
  220. if [ "x$existing_vi" = "x" ] ; then
  221. echo "No vi line found..."
  222. elif [ "x$existing_vi" != "x${prefixspace}${commentchar} ex: shiftwidth=$indentation tabstop=$tab_width" ] ; then
  223. echo "vi line is wrong ... fixing"
  224. perl -pi -e "s/(${prefixspace}[${commentchar}] ex:.*)/${prefixspace}${commentchar} ex: shiftwidth=${indentation} tabstop=${tab_width}/i" $FILE
  225. fi
  226. ###################################
  227. # final sanitization of the block #
  228. ###################################
  229. if [ $matching_found -eq 0 ] ; then
  230. # make sure there are no local vars
  231. do_not="Local"
  232. match="Variables"
  233. local=`cat "$FILE" | grep -i "${do_not} ${match}:" | awk '{print $1}'`
  234. # w00t, no local vars so just dump a shiney new block at the end of the file
  235. if [ "x$local" = "x" ] ; then
  236. cat >> $FILE <<EOF
  237. $comment_block
  238. EOF
  239. else
  240. echo "Darn.. existing local var block somewhere with no matches (${FILE})"
  241. fi
  242. elif [ $matching_found -ne $index ] ; then
  243. echo "Darn.. partial match on needs to be resolved manually (${FILE})"
  244. else
  245. # make sure open and closing do not have trailing comment chars too
  246. do_not="Local"
  247. match="Variables"
  248. existing_suffix=`cat $FILE | grep -i "${prefixspace}[${commentchar}] ${do_not} ${match}:" | sed 's/:/ /g' | awk '{print $4}'`
  249. if [ "x$existing_suffix" != "x" ] ; then
  250. echo "loc. var has trailing goo ... fixing"
  251. perl -pi -e "s/(${prefixspace}[${commentchar}] ${do_not} ${match}:.*)/${prefixspace}${commentchar} ${do_not} ${match}:/i" $FILE
  252. fi
  253. existing_suffix=`cat $FILE | grep -i "${prefixspace}[${commentchar}] End:" | sed 's/:/ /g' | awk '{print $3}'`
  254. if [ "x$existing_suffix" != "x" ] ; then
  255. echo "end has trailing goo ... fixing"
  256. perl -pi -e "s/(${prefixspace}[${commentchar}] End:.*)/${prefixspace}${commentchar} End:/i" $FILE
  257. fi
  258. fi
  259. # Local Variables:
  260. # mode: sh
  261. # tab-width: 8
  262. # sh-indentation: 4
  263. # sh-basic-offset: 4
  264. # indent-tabs-mode: t
  265. # End:
  266. # ex: shiftwidth=4 tabstop=8