-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimport-sources
executable file
·138 lines (110 loc) · 4.05 KB
/
import-sources
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
135
136
137
138
#!/bin/bash -eu
#
# Import Rogue sources to src/ dir, preparing them for initial git commit
#
# Copyright (C) 2015 Rodrigo Silva (MestreLion) <[email protected]>
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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. See <http://www.gnu.org/licenses/gpl.html>
#------------------------------------------------------------------------------
myname=$(basename "$0")
mydir=$(dirname "$(readlink -f "$0")")
targetdir=$mydir/src
force=0
#------------------------------------------------------------------------------
usage(){
cat <<-USAGE
Usage: ${myname} [-h|--help] [-f|--force] [--] SOURCE_DIR [TARGET_DIR]
Copy original sources from SOURCE_DIR to TARGET_DIR and:
- Remove executable bit (if any)
- Lowercase all file names (MAKEFILE is renamed Makefile)
- Remove 0x1A (^Z) char (usually as last char in file)
- Change newline from Windows/DOS (CRLF) to Unix (LF)
- Remove blank lines from end of file
- Fix indentations using tabs
TARGET_DIR defaults to: ${targetdir}
Will refuse to copy to a non-empty directory unless '--force' is used.
USAGE
exit
}
fatal() { [[ "${1:-}" ]] && echo "$myname: error: $1" >&2 || :; exit ${2:-1} ; }
# emulate sed's --in-place for commands that lack it, such as unexpand
inplace() {
local cmd=()
local outfile
local tmpfile
while (( $# )); do
case "$1" in
--) shift; break;;
*) cmd+=( "$1" );;
esac
shift
done
if ! (( $# && ${#cmd[@]} )); then
fatal "usage: inplace <command> [cmdargs...] -- FILE(s)..."
fi
for outfile; do
tmpfile=$(mktemp) || fatal
trap 'rm -f -- "$tmpfile"' RETURN
"${cmd[@]}" -- "$outfile" > "$tmpfile" && {
chmod --reference "$outfile" "$tmpfile" # GNU-only
mv "$tmpfile" -- "$outfile"; }
done
}
#------------------------------------------------------------------------------
# Pre-parse for `help`
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
dirs=()
while (( $# )); do
case "$1" in
-f|--force) force=1;;
--) shift; dirs+=( "$@" ); break;;
-*) fatal "invalid option: $1";;
*) dirs+=( "$1" );;
esac
shift
done
if (( "${#dirs[@]}" < 1 || "${#dirs[@]}" > 2 )); then usage; fi
sourcedir=${dirs[0]}
targetdir=${dirs[1]:-$targetdir}
if [[ -z "$sourcedir" ]]; then usage; fi
if ! [[ -d "$sourcedir" ]]; then fatal "SOURCE_DIR is not a directory: $sourcedir"; fi
sources=()
while read -r src; do sources+=( "$src" )
done < <(find "$sourcedir" -maxdepth 1 -type f)
if ! (( "${#sources[@]}" )); then fatal "source directory is empty: $sourcedir"; fi
if ! ((force)) && [[ "$(command ls "$targetdir" 2>/dev/null)" ]]; then
fatal "target directory is not empty, and --force was not used: $targetdir"
fi
if ! type rename &>/dev/null; then
printf '%s\n' "Required command 'rename' not found." \
"On Debian/Ubuntu this can be installed with:" \
" sudo apt install rename" >&2
exit 1
fi
#------------------------------------------------------------------------------
mkdir --parents -- "$targetdir"
cp --no-preserve mode --target-directory "$targetdir" -- "${sources[@]}" ||
fatal "could not copy cource files"
cd "$targetdir"
chmod -x *
rename 'y/A-Z/a-z/' *
shopt -s nullglob
files=(*.{asm,bat,c,h,opt} makefile? objs? tags?)
if ! ((${#files[@]})); then exit; fi
sed -i 's/\x1A//' -- "${files[@]}" # ^Z
sed -i 's/\r//' -- "${files[@]}" # CRLF to LF
sed -i 's/[ \t]*$//' -- "${files[@]}" # trailing blanks
sed -i ':a;/^\n*$/{$d;N;ba;}' -- "${files[@]}" # EOF blank lines
inplace unexpand -t4 --first-only -- "${files[@]}" # indentations
mv {m,M}akefile