-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmklex.sh
executable file
·139 lines (113 loc) · 3.2 KB
/
mklex.sh
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
#!/bin/bash
error () {
printf "\e[31m$1\e[0m\n" "${@:2}"
}
success () {
printf "\e[32m$1\e[0m\n" "${@:2}"
}
warning () {
printf "\e[33m$1\e[0m\n" "${@:2}"
}
println () {
printf "$1\n" "${@:2}"
}
# ~/wikitongues-config is where the necessary locations for this operation get stored.
# The file is created by the setup.sh script.
if [[ ! -f ~/wikitongues-config ]]; then
error "Settings not configured."
println "From within the Oral-History-Template-Instantiator directory, please run the setup script: "
println "> ./setup.sh"
exit 1
fi
source ~/wikitongues-config
# The metadata address is the absolute path to where the airtable API script live
metadataPath=$metadata
# The destination address is the absolute path to where you want the lexicon templates to be created.
destination=$lexiconDestination
if [[ -z $destination ]]; then
error "Settings not configured for mklex."
println "From within the Oral-History-Template-Instantiator directory, please run the setup script: "
println "> ./setup.sh"
exit 1
fi
# Reads flags
flagger () {
open=false
dev=false
makeMetadataFile=true
lexicons=()
identifiers=()
for arg in "$@"; do
if [[ $arg == '-d' || $arg == '--dev' ]]; then
dev=true
elif [[ $arg == '-o' || $arg == '--open' ]]; then
open=true
else
lexicons+=("$arg")
fi
done
if [[ ! $airtableConfig || $airtableConfig == false ]]; then
makeMetadataFile=false
warning "Airtable API not configured. No metadata file will be automatically produced."
fi
}
runner () {
destination="$1"
if [ -z "${lexicons[*]}" ]; then
warning "Please specify at least one lexicon ID"
exit 1
fi
for lexiconId in ${lexicons[*]}; do
directorator "$destination" "$lexiconId"
done
if [[ $open == true ]]; then
for id in ${identifiers[*]}; do
open "$destination"/"$id"
done
fi
}
directorator () {
destination="$1"
lexiconId="$2"
identifier=$(renamer "$destination" "$lexiconId")
identifiers+=("$identifier")
if [ "$identifier" != "$lexiconId" ]; then
warning "Directory for %s is named %s for archival compatibility." "$lexiconId" "$identifier"
fi
if [ -d "$destination"/"$identifier" ]; then
println "A directory named %s already exists in this location. Skipping." "$identifier"
return
fi
mkdir "$destination"/"$identifier"
if [[ $makeMetadataFile == true ]]; then
node "$metadataPath"/getLexiconMetadata.js "$lexiconId" "$metadata" "$destination" "$identifier"
if [ $? != 0 ]; then
rm -r "$destination"/"$identifier"
exit 1
fi
fi
if [ -d "$destination"/"$identifier" ]; then
success "Lexicon directory successfully created for %s." "$lexiconId"
else
error "Something went wrong."
exit 1
fi
}
# Rename directory to S3-compliant identifier for LOC archival
renamer () {
destination="$1"
lexiconId="$2"
# Convert to ascii characters
identifier=$(echo "$lexiconId" | iconv -f UTF-8 -t ascii//TRANSLIT//ignore)
# Remove characters left by Mac iconv implementation
identifier=${identifier//[\'\^\~\"\`]/''}
# Change + to -
identifier=${identifier//\+/'-'}
echo "$identifier"
}
flagger "$@"
if [[ $dev == true ]]; then
runner "."
else
runner "$destination"
fi