-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathting
More file actions
executable file
·175 lines (157 loc) · 4.27 KB
/
ting
File metadata and controls
executable file
·175 lines (157 loc) · 4.27 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
# ting - manage your TING pen.
# Copyright 2024 Maciej Żok
# SPDX-License-Identifier: MIT
# <https://github.com/macie/ting-cli>
set -eu
# following line is used by `make release`
TING_VERSION='24.03-prerelease'
#
# FUNCTIONS
#
# ting_is_connected checks if the TING pen is connected to the computer. If
# pen is not connected, it prints error message to stderr and returns EX_NOINPUT.
#
# Usage: ting_is_connected <path/to/ting>
ting_is_connected() {
if [ ! -d "${1:-}" ]; then
echo "ERROR: Device '$1' not found. Is TING pen connected?" >&2
return 66 # EX_NOINPUT
fi
}
# ting_normalized_settings prints to stdout the content of settings.ini without
# any problematic non-ASCII characters.
#
# Usage: ting_normalized_settings <path/to/ting>
ting_normalized_settings() {
TING_SETTINGS_FILE="$1/\$ting/settings.ini" # UTF-16 (LE): FF FE
# removing BOM characters (FF FE), null character and carriage return
sed "1s/^$(printf '\377\376')//" "${TING_SETTINGS_FILE}" | tr -d '\000\r'
}
# ting_info prints to stdout information about the TING pen.
#
# Usage: ting_info <path/to/ting>
ting_info() {
ting_normalized_settings "$1" | awk -F '=' '
/^server/{ server = $2 }
/^area/{ area = $2 }
/^sw/{ sw = $2 }
/^language/{ lang = $2 }
/^serial/{ sn = $2 }
/^cdfs/{ sw_ver = $2 }
/^fw/{ fw_ver = $2 }
END {
country[33] = "fr"
country[34] = "es"
country[39] = "it"
country[44] = "uk"
country[48] = "pl"
country[49] = "de"
country[55] = "br"
country[86] = "cn"
country[90] = "tr"
# the other codes (20, 35, 99) are unknown to me
if (lang in country) { lang = country[lang] }
printf "Firmware version: %s\n", fw_ver
printf "Software version: %s (%s)\n", sw, sw_ver
printf "Serial number: %s\n", sn
printf "Locale: %s (%s)\n", area, lang
printf "Update server: %s\n", server
}'
}
# ting_list prints to stdout sorted list of books stored on the TING pen as
# tab-separated columns: "<title>" <authors> <publisher>
#
# Usage: ting_list <path/to/ting>
ting_list() {
for TING_BOOK_METADATA in $1/\$ting/?????_??.txt; do
awk -F ':' '
/^Name/{ sub(/^[ \t\r\n]+/, "", $2); title = $2 }
/^Author/{ sub(/^[ \t\r\n]+/, "", $2); authors = $2 }
/^Publisher/{ sub(/^[ \t\r\n]+/, "", $2); publisher = $2 }
END {
# titles are sometimes UPPERCASE
initial = substr(title, 1, 1)
rest = substr(title, 2)
gsub(/Ą/, "ą", rest)
gsub(/Ć/, "ć", rest)
gsub(/Ę/, "ę", rest)
gsub(/Ł/, "ł", rest)
gsub(/Ń/, "ń", rest)
gsub(/Ó/, "ó", rest)
gsub(/Ś/, "ś", rest)
gsub(/Ź/, "ź", rest)
gsub(/Ż/, "ż", rest)
printf "\"%s%s\"\t", toupper(initial), tolower(rest)
# do not print author if it is the same as publisher
if (authors != publisher) {
split(authors, a, ", ")
}
# print author(s) as: Surname, Name;
for (i = 1; i <= length(a); i++) {
split(a[i], words, " ")
surname = words[length(words)]
names = ""
for (j = 1; j < length(words); j++) {
names = names words[j]
}
printf "%s, %s%s", surname, names, (i < length(a)) ? "; " : ""
}
printf "\t"
printf "%s\n", publisher
}' "${TING_BOOK_METADATA}"
done | sort
}
#
# MAIN ROUTINE
#
{
if [ $# -ge 2 ] && [ $1 = '-d' ]; then
TING_DEV="$2"
shift 2
fi
case $# in
0)
echo "ERROR: Invalid usage. See: '$0 help'" >&2
exit 64 # EX_USAGE
;;
1)
case $1 in
help|-h)
cat >&2 <<-EOF
ting - manage your TING pen.
Usage:
ting [-d DIR] <COMMAND>
Commands:
help Show this help and exit.
info Show information about the TING pen.
list List books downloaded on the TING pen.
version Show version and exit.
Options:
-d DIR Mounting point of the TING pen.
EOF
;;
info)
ting_is_connected "${TING_DEV:-}" || exit $?
ting_info "${TING_DEV:-}"
;;
list)
ting_is_connected "${TING_DEV:-}" || exit $?
ting_list "${TING_DEV:-}"
;;
version)
echo "ting $TING_VERSION"
;;
*)
echo "ERROR: Unknown command '$1'. See valid commands with '$0 help'." >&2
exit 64 # EX_USAGE
;;
esac
;;
*)
echo "ERROR: Too many arguments. See usage: '$0 help'" >&2
exit 64 # EX_USAGE
;;
esac
exit 0 # EX_OK
}