-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvinbt
More file actions
executable file
·132 lines (112 loc) · 2.86 KB
/
vinbt
File metadata and controls
executable file
·132 lines (112 loc) · 2.86 KB
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
#!/bin/bash
#
# vinbt - interactive command line NBT editor
#
# Copyright (C) 2012-2013 DMBuce <dmbuce@gmail.com>
#
# 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 2 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; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
PROG="$(basename $0)"
USAGE="Usage: $PROG <file>
Edit an NBT file using a text editor.
"
# print message to stdout
msg() {
local message="$1"
shift
printf "$message\n" "$@"
}
# print warning to stderr
warn() {
local message="$1"
shift
printf "$PROG: $message\n" "$@" >&2
}
# print error to stderr and quit
die() {
warn "$@"
exit 1
}
# append dir to PATH
pathmunge () {
local dir="$1"
if [[ -d "$dir" ]] && ! echo "$PATH" | egrep -q "(^|:)$dir($|:)"; then
if [[ "$2" == "before" ]]; then
PATH="$dir:$PATH"
else
PATH="$PATH:$dir"
fi
fi
}
# find norbert
which_norbert() {
local norbert dir PATHORIG
PATHORIG="$PATH"
if [[ "$NORBERT" != "" ]]; then
echo "$NORBERT"
return 0
fi
for dir in "$HOME/bin" "$PWD"; do
pathmunge "$dir"
done
for norbert in norbert norbert.py; do
if which "$norbert" 2>/dev/null; then
PATH="$PATHORIG"
return 0
fi
done
die "no norbert in (%s)" "$PATH"
}
# find a text editor
which_editor() {
local editor
if [[ "$DISPLAY" != "" ]]; then
editor="${VISUAL}"
fi
editor="${editor:-$EDITOR}"
editor="${editor:-vi}"
echo "$editor"
}
# print usage and quit if --help given
case "$1" in "-h"|"--help"|"")
msg "%s" "$USAGE"
exit
esac
if [[ "$#" != 1 ]]; then
die "Too many arguments\n%s" "$USAGE"
fi
editor="$(which_editor)"
norbert="$(which_norbert)" || exit $?
file="$1"
tmpfile="$(mktemp --tmpdir "vinbt-$(basename "$file").norbert.XXXXXXXXXX")" \
|| exit $?
trap "rm -f \"$tmpfile\"" EXIT
$norbert -d 0 -p norbert -f "$file" >"$tmpfile" || exit $?
tmpfileorig="$(<"$tmpfile")"
$editor "$tmpfile"
if diff -q "$tmpfile" <(echo "$tmpfileorig") >/dev/null; then
warn "no changes made"
warn "%s unchanged" "$file"
else
warn "changes made"
warn "writing changes to %s" "$file"
if ! $norbert -d 0 -i norbert -f "$tmpfile" -o "$file" >/dev/null; then
warn "error writing changes"
savefile="$(mktemp "vinbt-$(basename "$file").norbert.XXXXXXXXXX")" \
|| exit $?
warn "saving as %s" "$savefile"
cp "$tmpfile" "$savefile"
fi
fi