-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
new_country.sh
52 lines (43 loc) · 1.25 KB
/
new_country.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
#!/bin/bash
# 'return' when run as "source <script>" or ". <script>", 'exit' otherwise
[[ "$0" != "${BASH_SOURCE[0]}" ]] && safe_exit="return" || safe_exit="exit"
script_name=$(basename "$0")
current_directory=$(pwd)
ask(){
# ask <question> <default>
local ANSWER
read -r -p "$1: " ANSWER
echo "${ANSWER:-$2}"
}
confirm(){
# confirm <question> (default = N)
local ANSWER
read -r -p "$1 (y/N): " -n 1 ANSWER
echo " "
[[ "$ANSWER" =~ ^[Yy]$ ]]
}
country_code=$(ask "Country code?")
country_code=`echo "$country_code" | tr '[:upper:]' '[:lower:]'`
country_path="$current_directory/src/$country_code"
if [ -d "$current_directory/src/$country_code" ] ; then
echo "Country folder already exists!"
$safe_exit 1
fi
country_name=$(ask "Country name?")
echo "Country: $country_name"
cp -R "$current_directory/stubs/new_country" "$country_path"
echo
files=$(grep -E -r -l -i ":country_name" $country_path/*)
for file in $files ; do
echo "UPDATING: $file"
temp_file="$file.temp"
< "$file" \
sed "s/:country_code/$country_code/g" \
| sed "s/:country_name/$country_name/g" \
> "$temp_file"
rm -f "$file"
mv "$temp_file" "$file"
done
mv "$country_path/README.md.stub" "$country_path/README.md"
echo
echo 'DONE!'