Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / solenv / bin / add-modelines
blobfcb26bc3639dd23dc8f6ece03845b011a7f2242b
1 #!/bin/bash
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 # add-modelines, a simple script to add comments to
11 # the beginning and end of source files for LibreOffice devs
13 # Blame goes to Jesse Adelman (at least at first)
14 # someone AT boldandbusted dotty-dot com
15 # http://www.boldandbusted.com/
16 # (c) 2010 Bold and Busted LLC
18 # NOTE: At present, this script only works for files with C-like comments.
19 # NOTE: If you don't specify -p, the script will act on the current working directory.
20 # NOTE: If no arguments are specified, the defitions below are in effect.
22 # TO DO
23 # - Deuglifiy?
24 # - Make source file type agnostic modelines?
25 # - Too many/too few comments?
26 # - Handle top level source directories with whitespace names? (Do they exist?)
28 # Turn off globbing, helps with SourceFiles
29 set -f
31 # POSIX
32 set -o posix
34 # Change these to taste
35 FirstLine='/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */'
36 LastLine='/* vim:set shiftwidth=4 softtabstop=4 expandtab: */'
37 SourceFiles='*.cxx *.cpp *.hxx *.hpp *.c *.h *.m *.mm *.idl *.src *.hrc'
39 # Set defaults (don't change these)
40 ModelineReplace="false"
42 # Functions
44 function SetEnvironment()
46 if [ -n "$(which tail)" -a -n "$(which head)" ]; then
48 headCMD=`which head`
49 tailCMD=`which tail`
51 else
53 echo "Missing head or tail, exiting..."
54 exit 1
57 if [ -n "$(which find)" ]; then
58 findCMD=`which find`
59 else
61 echo "Missing find, exiting..."
62 exit 1
65 if [ -n "$(which awk)" ]; then
66 awkCMD=`which awk`
67 else
69 echo "Missing awk, exiting..."
70 exit 1
75 function EditFile()
77 local FileToEdit
78 local currentFirstLine
79 local currentLastLine
81 FileToEdit="$1"
83 currentFirstLine=`$headCMD -1 "$FileToEdit"`
84 currentLastLine=`$tailCMD -1 "$FileToEdit"`
86 case "$ModelineReplace" in
87 "true" )
88 if [ "${currentFirstLIne:0:6}" = "${FirstLine:0:6}" ]; then
90 echo "$FirstLine" > "$FileToEdit".new
91 $tailCMD -n +2 "$FileToEdit" >> "$FileToEdit".new
94 if [ -e "$FileToEdit.new" ]; then
96 echo "$LastLine" >> "$FileToEdit".new
99 if [ "${currentLastLine:0:6}" = "${LastLine:0:6}" ]; then
101 $headCMD -n -1 "$FileToEdit" > "$FileToEdit".new
102 echo "$LastLine" >> "$FileToEdit".new
105 mv "$FileToEdit".new "$FileToEdit"
106 echo "$FileToEdit updated" ;;
107 "false" )
108 if [ "${currentFirstLine:0:6}" != "${FirstLine:0:6}" ]; then
109 if [ "${currentLastLine:0:6}" != "${LastLine:0:6}" ]; then
111 echo "$FirstLine" > "$FileToEdit".new
112 cat "$FileToEdit" >> "$FileToEdit".new
113 if [ "x${currentLastLine}" != "x" ] ; then
114 echo "" >> "$FileToEdit".new
116 echo "$LastLine" >> "$FileToEdit".new
117 mv "$FileToEdit".new "$FileToEdit"
118 echo "$FileToEdit updated"
121 fi ;;
122 esac
125 function PrintUsage()
127 echo "Usage: $0 [-z] [-s \"<sourcefile glob>\"] [-p <path to source>]"
130 # Main
132 SetEnvironment
134 # Get command line options
136 while getopts "zs:p:" opt; do
137 case $opt in
138 z) ModelineReplace="true" ;;
139 s) SourceFiles="$OPTARG" ;;
140 p) findPath="$OPTARG" ;;
141 *) PrintUsage
142 exit 1 ;;
143 esac
144 done
146 if [ $OPTIND -gt 1 ]; then
147 shift $(($OPTIND - 1))
150 if [ $# -gt 1 ]; then
152 PrintUsage
153 echo "Remember to quote the source file globs after -s"
154 exit 1
158 # Create GNU find expressions that traverse the filesystem once and only once
159 if [ -z "$findPath" ]; then
160 findArgs='.'
161 else
162 findArgs="$findPath"
165 for FileType in ${SourceFiles}; do
166 findArgs="$findArgs"' ( -iname '"$FileType"' -print -o -true ) -a '
167 done
169 # This gets rid of the final " -a " in the find argument list
170 findArgs="${findArgs:0:(${#findArgs}-3)}"
172 for file in $($findCMD $findArgs); do
173 EditFile "$file"
174 echo "Completed: " "$file"
175 done
177 # vim:set shiftwidth=4 softtabstop=4 expandtab: