File: standard-header.sh

package info (click to toggle)
goodvibes 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,784 kB
  • sloc: ansic: 16,310; sh: 978; python: 21; makefile: 19
file content (134 lines) | stat: -rwxr-xr-x 2,598 bytes parent folder | download | duplicates (4)
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
#!/bin/bash -e

SPDX_LICENSE_ID=GPL-3.0-only

print_usage()
{
    cat << EOF
Usage: $(basename $0) <command> [<file>...]

Tool to check/add standard headers in C files.

Commands:
  check        Check if source files have a standard header.
  add          If standard header is missing, add it.
EOF
}

git_is_tracked()
{
    local file="$1"

    # Fill list of tracked files only once
    if [ $GIT_TRACKED_FILLED -eq 0 ]; then
	GIT_TRACKED="$(git ls-files)"
	GIT_TRACKED_FILLED=1
	#echo "--------"
	#echo "$GIT_TRACKED"
	#echo "--------"
    fi

    # Remove leading './' from filename if any
    file="$(echo $file | sed 's:^\.\/::')"

    echo "$GIT_TRACKED" | grep -q "$file"
}

unit_name()
{
    echo "Goodvibes Radio Player"
}

do_check()
{
    local file="$1"
    local unit="$(unit_name "$file")"

    head -n 4 "$file" | tr -d '\n' | \
	grep -q -F "/* * $unit * * Copyright (C)"
}

do_add()
{
    local file="$1"
    local unit="$(unit_name "$file")"

    cat << EOF > "$file.tmp"
/*
 * $unit
 *
 * Copyright (C) $(date +%Y) $(git config user.name)
 *
 * SPDX-License-Identifier: ${SPDX_LICENSE_ID:?}
 *
 * This program 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, version 3.
 *
 * This program 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 program. If not, see <https://www.gnu.org/licenses/>.
 */

$(cat $file)
EOF

    mv "$file.tmp" "$file"
}

# Check for help arguments
[ "$1" == "-h" -o "$1" == "--help" ] && \
    { print_usage; exit 0; }

# Check for proper usage
[ $# -lt 1 ] && \
    { print_usage; exit 1; }

# Ensure we're in the right directory
[ -d "src" ] || \
    { echo >&2 "Please run from root directory"; exit 1; }

# File list
if [ $# -eq 1 ]; then
    filestmp=$(find src -name \*.[ch])
    files=""
    for file in $filestmp; do
        if ! git_is_tracked $file; then
            continue
	fi
	files+="$file\n"
    done
else
    files="${@:2}"
fi

# Do the job
case $1 in
    check)
	for f in $files; do
	    if ! do_check $f; then
		echo "'$f': no standard header found."
	    fi
	done
	;;

    add)
	for f in $files; do
	    if do_check $f; then
		continue;
	    fi

	    echo "'$f': adding standard header."
	    do_add $f
	done
	;;

    *)
	print_usage
	exit 1
	;;
esac