Merge branch 'master' into translations
[QuestHelper.git] / Development / update_lang.sh
blob5a96ea726e40e1fe0bb26386ae92851ea831a314
1 #!/bin/bash
3 function die {
4 echo $* 1>&2
5 exit 1
8 export BOTKEY="`pwd`/botkey"
9 export GIT_SSH="`pwd`/botssh.sh"
11 cd ..
13 git status &> /dev/null && die "Working directory not clean; script may have crashed last run."
15 SSH_REMOTE="qhbot@idiotbox-03"
16 WEB_REMOTE="http://smariot.hopto.org/translate/"
18 function filter_names {
19 # Keep only the '____.lua' portion of a name.
20 sed -n "s/.*\\(....\\.lua\\)$/\\1/p"
23 function remote_list {
24 # Get list of files on the server.
25 ${GIT_SSH} ${SSH_REMOTE} "ls -1 /server/http/cgi/lang/lang_????.lua" | filter_names
28 function local_list {
29 # Get list of local files.
30 ls -1 lang/????.lua | filter_names
33 function changed {
34 # Check if the file on the server matches the local file.
35 if [ "`${GIT_SSH} ${SSH_REMOTE} \"cat /server/http/cgi/lang/lang_${1} | sha1sum -\"`" == "`cat lang/${1} | sha1sum -`" ] ; then
36 echo "same"
37 else
38 echo "changed"
42 function uploadifchanged {
43 # Uploads a local file to the server, if it has been changed.
44 if [ `changed ${1}` == "changed" ] ; then
45 echo "Uploading ${1}..."
46 ${GIT_SSH} ${SSH_REMOTE} "cat > /server/http/cgi/lang/lang_${1}" < lang/${1} || die "Error uploading '${1}'."
48 # Make sure webserver is able to write to this file.
49 # Sorry whoever put all that work into editing csCZ; I'm sure seeing the 'unable to save' message was disheartening,
50 # but fear not, it was emailed to me, and I applied it for you. :)
51 ${GIT_SSH} ${SSH_REMOTE} "chmod g+w /server/http/cgi/lang/lang_${1}" || die "Error making file writable '${1}'."
55 echo "Updating repository..."
57 git checkout master &> /dev/null || die "Error switching to 'master' branch."
58 git pull smariot master --tags &> /dev/null || die "Error pulling changes from smariot's repository."
59 git pull zorba master --tags &> /dev/null || die "Error pulling changes from zorba's repository."
61 # Step zero, replace the enus.lua file on the server, all the other files will be formatted using it as a template
62 # when we download them.
63 if [ -e lang/enus.lua ] ; then
64 echo "Making sure server has latest version of enus.lua..."
65 uploadifchanged "enus.lua"
66 else
67 die "'lang/enus.lua' has, like, been abducted by aliens!"
70 # Step one, switch to our translations branch, it should match the state the files were in
71 # the last time this script was run. (We're not merging yet, we'll do that after we commit,
72 # so that git can fix any conflicts for us.
73 echo "Switching to translations branch..."
74 git checkout translations &> /dev/null || die "Error switching to translations branch."
76 # Step two, download all the translations from the server, and add them to the repository.
77 for FILE in `remote_list` ; do
78 # enus shouldn't be translated; skip it.
79 if [ ${FILE} != "enus.lua" ] ; then
80 echo "Downloading ${FILE}..."
81 wget -q ${WEB_REMOTE}${FILE} -O lang/${FILE} || die "Error updating 'lang/${FILE}'"
82 git add lang/${FILE} || die "Error adding 'lang/${FILE}' to repository."
84 done
86 # Step three, update language files in QuestHelper.toc
87 echo "Updating translations files in QuestHelper.toc"
89 FIRST="true"
90 while read -r LINE ; do
91 case ${LINE} in
92 # Does the line look like a translation?
93 lang\\????.lua )
94 # Is this the first translation we've seen?
95 if [ ${FIRST} == "true" ] ; then
96 FIRST="false"
97 # Dump all the translations; replace slashes with backslashes.
98 ls -1 lang/????.lua | sed "s/\\//\\\\/g"
101 # Otherwise, write the original line back out.
103 echo "${LINE}"
105 esac
106 done
107 # Did we actually find any translations?
108 if [ ${FIRST} == "true" ] ; then
109 die "Didn't find any translations in QuestHelper.toc! What manner of sorcery is this?"
111 ) < QuestHelper.toc > _QuestHelper.toc
113 mv -f _QuestHelper.toc QuestHelper.toc || die "Error copying over updated QuestHelper.toc"
114 git add QuestHelper.toc || die "Error adding 'QuestHelper.toc' to repository."
116 # Step four, create the commit.
117 echo "Creating a new commit."
118 git commit -m "Automated update from: http://smariot.hopto.org/translate" &> /dev/null || die "Error creating commit. (Possibly because there was nothing to commit.)"
120 # Step five, merge with master.
121 git pull . master &> /dev/null || die "Error merging with 'master' into translations."
123 # Step six, delete any removed translations.
124 for FILE in `remote_list` ; do
125 if [ ${FILE} != "enus.lua" ] ; then
126 if [ ! -e "lang/${FILE}" ] ; then
127 echo "Deleting ${FILE} from the server."
128 ${GIT_SSH} ${SSH_REMOTE} "rm /server/http/cgi/lang/lang_${FILE}" || die "Error deleting '${FILE}' from server."
131 done
133 # Step seven, upload the translations to the server, in case they were changed by the merge.
134 echo "Uploading updated translations back to server..."
135 for FILE in `local_list` ; do
136 uploadifchanged ${FILE}
137 done
139 # Step eight, switch back to master branch, merge, and upload.
140 echo "Merging translations back into master branch."
141 git checkout master &> /dev/null || die "Error switching to 'master' branch."
142 git pull . translations &> /dev/null || die "Error pulling changes from 'translations' branch."
144 # Step nine, push the changes back to the server.
145 echo "Pushing changes..."
146 git push qhbot master --tags &> /dev/null || die "Error pushing changes."
148 echo "All done!"