| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# This file is part of the LibreOffice project. |
| 4 |
# |
| 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/. |
| 8 |
# |
| 9 |
|
| 10 |
# add-modelines, a simple script to add comments to |
| 11 |
# the beginning and end of source files for LibreOffice devs |
| 12 |
|
| 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 |
| 17 |
|
| 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 definitions below are in effect. |
| 21 |
|
| 22 |
# TO DO |
| 23 |
# - Deuglify? |
| 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?) |
| 27 |
|
| 28 |
# Turn off globbing, helps with SourceFiles |
| 29 |
set -f |
| 30 |
|
| 31 |
# POSIX |
| 32 |
set -o posix |
| 33 |
|
| 34 |
# Change these to taste |
| 35 |
FirstLine='/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */' |
| 36 |
LastLine='/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */' |
| 37 |
SourceFiles='*.cxx *.cpp *.hxx *.hpp *.c *.h *.m *.mm *.idl *.src *.hrc' |
| 38 |
|
| 39 |
# Set defaults (don't change these) |
| 40 |
ModelineReplace="false" |
| 41 |
|
| 42 |
# Functions |
| 43 |
|
| 44 |
function SetEnvironment() |
| 45 |
{ |
| 46 |
if ! command -v tail || ! command -v head; then |
| 47 |
echo "Missing head or tail, exiting..." |
| 48 |
exit 1 |
| 49 |
fi |
| 50 |
if ! command -v find; then |
| 51 |
echo "Missing find, exiting..." |
| 52 |
exit 1 |
| 53 |
fi |
| 54 |
} |
| 55 |
|
| 56 |
function EditFile() |
| 57 |
{ |
| 58 |
local FileToEdit |
| 59 |
local currentFirstLine |
| 60 |
local currentLastLine |
| 61 |
|
| 62 |
FileToEdit="$1" |
| 63 |
|
| 64 |
currentFirstLine=$(head -1 "$FileToEdit") |
| 65 |
currentLastLine=$(tail -1 "$FileToEdit") |
| 66 |
|
| 67 |
case "$ModelineReplace" in |
| 68 |
"true" ) |
| 69 |
if [ "${currentFirstLine:0:6}" = "${FirstLine:0:6}" ]; then |
| 70 |
{ |
| 71 |
echo "$FirstLine" > "$FileToEdit".new |
| 72 |
tail -n +2 "$FileToEdit" >> "$FileToEdit".new |
| 73 |
} |
| 74 |
fi |
| 75 |
if [ -e "$FileToEdit.new" ]; then |
| 76 |
{ |
| 77 |
echo "$LastLine" >> "$FileToEdit".new |
| 78 |
} |
| 79 |
fi |
| 80 |
if [ "${currentLastLine:0:6}" = "${LastLine:0:6}" ]; then |
| 81 |
{ |
| 82 |
head -n -1 "$FileToEdit" > "$FileToEdit".new |
| 83 |
echo "$LastLine" >> "$FileToEdit".new |
| 84 |
} |
| 85 |
fi |
| 86 |
mv "$FileToEdit".new "$FileToEdit" |
| 87 |
echo "$FileToEdit updated" ;; |
| 88 |
"false" ) |
| 89 |
if [ "${currentFirstLine:0:6}" != "${FirstLine:0:6}" ]; then |
| 90 |
if [ "${currentLastLine:0:6}" != "${LastLine:0:6}" ]; then |
| 91 |
{ |
| 92 |
echo "$FirstLine" > "$FileToEdit".new |
| 93 |
cat "$FileToEdit" >> "$FileToEdit".new |
| 94 |
if [ "x${currentLastLine}" != "x" ] ; then |
| 95 |
echo "" >> "$FileToEdit".new |
| 96 |
fi |
| 97 |
echo "$LastLine" >> "$FileToEdit".new |
| 98 |
mv "$FileToEdit".new "$FileToEdit" |
| 99 |
echo "$FileToEdit updated" |
| 100 |
} |
| 101 |
fi |
| 102 |
fi ;; |
| 103 |
esac |
| 104 |
} |
| 105 |
|
| 106 |
function PrintUsage() |
| 107 |
{ |
| 108 |
echo "Usage: $0 [-z] [-s \"<sourcefile glob>\"] [-p <path to source>]" |
| 109 |
} |
| 110 |
|
| 111 |
# Main |
| 112 |
|
| 113 |
SetEnvironment |
| 114 |
|
| 115 |
# Get command line options |
| 116 |
|
| 117 |
while getopts "zs:p:" opt; do |
| 118 |
case $opt in |
| 119 |
z) ModelineReplace="true" ;; |
| 120 |
s) SourceFiles="$OPTARG" ;; |
| 121 |
p) findPath="$OPTARG" ;; |
| 122 |
*) PrintUsage |
| 123 |
exit 1 ;; |
| 124 |
esac |
| 125 |
done |
| 126 |
|
| 127 |
if [ $OPTIND -gt 1 ]; then |
| 128 |
shift $((OPTIND - 1)) |
| 129 |
fi |
| 130 |
|
| 131 |
if [ $# -gt 1 ]; then |
| 132 |
{ |
| 133 |
PrintUsage |
| 134 |
echo "Remember to quote the source file globs after -s" |
| 135 |
exit 1 |
| 136 |
} |
| 137 |
fi |
| 138 |
|
| 139 |
# Create GNU find expressions that traverse the filesystem once and only once |
| 140 |
if [ -z "$findPath" ]; then |
| 141 |
findArgs='.' |
| 142 |
else |
| 143 |
findArgs="$findPath" |
| 144 |
fi |
| 145 |
|
| 146 |
for FileType in ${SourceFiles}; do |
| 147 |
findArgs="$findArgs"' ( -iname '"$FileType"' -print -o -true ) -a ' |
| 148 |
done |
| 149 |
|
| 150 |
# This gets rid of the final " -a " in the find argument list |
| 151 |
findArgs=(${findArgs:0:(${#findArgs}-3)}) |
| 152 |
|
| 153 |
for file in $(find "${findArgs[@]}"); do |
| 154 |
EditFile "$file" |
| 155 |
echo "Completed: " "$file" |
| 156 |
done |
| 157 |
|
| 158 |
# vim:set shiftwidth=4 softtabstop=4 expandtab: |