Automated update from: http://smariot.no-ip.org/translate
[QuestHelper.git] / Development / update_lang.sh
blob600ec519cbae8138217d6c99b1c7dcd40ee8b9e8
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@luna"
16 WEB_REMOTE="http://smariot.no-ip.org/translate/"
17 SERVER_PATH="/home/lighttpd/default/cgi/lang"
19 function filter_names {
20 # Keep only the '____.lua' portion of a name.
21 sed -n "s/.*\\(....\\.lua\\)$/\\1/p"
24 function remote_list {
25 # Get list of files on the server.
26 ${GIT_SSH} ${SSH_REMOTE} "ls -1 ${SERVER_PATH}/lang_????.lua" | filter_names
29 function local_list {
30 # Get list of local files.
31 ls -1 lang/????.lua | filter_names
34 function changed {
35 # Check if the file on the server matches the local file.
36 if [ "`${GIT_SSH} ${SSH_REMOTE} \"cat ${SERVER_PATH}/lang_${1} | sha1sum -\"`" == "`cat lang/${1} | sha1sum -`" ] ; then
37 echo "same"
38 else
39 echo "changed"
43 function uploadifchanged {
44 # Uploads a local file to the server, if it has been changed.
45 if [ `changed ${1}` == "changed" ] ; then
46 echo "Uploading ${1}..."
47 ${GIT_SSH} ${SSH_REMOTE} "cat > ${SERVER_PATH}/lang_${1}" < lang/${1} || die "Error uploading '${1}'."
51 echo "Updating repository..."
53 git checkout master &> /dev/null || die "Error switching to 'master' branch."
54 git pull smariot master --tags &> /dev/null || die "Error pulling changes from smariot's repository."
55 git pull zorba master --tags &> /dev/null || die "Error pulling changes from zorba's repository."
57 # Step zero, replace the enus.lua file on the server, all the other files will be formatted using it as a template
58 # when we download them.
59 if [ -e lang/enus.lua ] ; then
60 echo "Making sure server has latest version of enus.lua..."
61 uploadifchanged "enus.lua"
62 else
63 die "'lang/enus.lua' has, like, been abducted by aliens!"
66 # Step one, switch to our translations branch, it should match the state the files were in
67 # the last time this script was run. (We're not merging yet, we'll do that after we commit,
68 # so that git can fix any conflicts for us.
69 echo "Switching to translations branch..."
70 git checkout translations &> /dev/null || die "Error switching to translations branch."
72 # Step two, download all the translations from the server, and add them to the repository.
73 for FILE in `remote_list` ; do
74 # enus shouldn't be translated; skip it.
75 if [ ${FILE} != "enus.lua" ] ; then
76 echo "Downloading ${FILE}..."
77 wget -q ${WEB_REMOTE}${FILE} -O lang/${FILE} || die "Error updating 'lang/${FILE}'"
78 git add lang/${FILE} || die "Error adding 'lang/${FILE}' to repository."
80 done
82 # Step three, update language files in QuestHelper.toc
83 echo "Updating translations files in QuestHelper.toc"
85 FIRST="true"
86 while read -r LINE ; do
87 case ${LINE} in
88 # Does the line look like a translation?
89 lang\\????.lua )
90 # Is this the first translation we've seen?
91 if [ ${FIRST} == "true" ] ; then
92 FIRST="false"
93 # Dump all the translations; replace slashes with backslashes.
94 ls -1 lang/????.lua | sed "s/\\//\\\\/g"
97 # Otherwise, write the original line back out.
99 echo "${LINE}"
101 esac
102 done
103 # Did we actually find any translations?
104 if [ ${FIRST} == "true" ] ; then
105 die "Didn't find any translations in QuestHelper.toc! What manner of sorcery is this?"
107 ) < QuestHelper.toc > _QuestHelper.toc
109 mv -f _QuestHelper.toc QuestHelper.toc || die "Error copying over updated QuestHelper.toc"
110 git add QuestHelper.toc || die "Error adding 'QuestHelper.toc' to repository."
112 # Step four, create the commit.
113 echo "Creating a new commit."
114 git commit -m "Automated update from: http://smariot.no-ip.org/translate" &> /dev/null || die "Error creating commit. (Possibly because there was nothing to commit.)"
116 # Step five, rebase with master (prettier history than merging).
117 git rebase master &> /dev/null || die "Error rebasing with 'master'."
118 #git pull . master &> /dev/null || die "Error merging with 'master' into translations."
120 # Step six, delete any removed translations.
121 for FILE in `remote_list` ; do
122 if [ ${FILE} != "enus.lua" ] ; then
123 if [ ! -e "lang/${FILE}" ] ; then
124 echo "Deleting ${FILE} from the server."
125 ${GIT_SSH} ${SSH_REMOTE} "rm ${SERVER_PATH}/lang_${FILE}" || die "Error deleting '${FILE}' from server."
128 done
130 # Step seven, upload the translations to the server, in case they were changed by the merge.
131 echo "Uploading updated translations back to server..."
132 for FILE in `local_list` ; do
133 uploadifchanged ${FILE}
134 done
136 # Step eight, switch back to master branch, merge, and upload.
137 echo "Merging translations back into master branch."
138 git checkout master &> /dev/null || die "Error switching to 'master' branch."
139 git pull . translations &> /dev/null || die "Error pulling changes from 'translations' branch."
141 # Step nine, push the changes back to the server.
142 echo "Pushing changes..."
143 echo "(Not really)"
144 #git push qhbot master --tags &> /dev/null || die "Error pushing changes."
146 echo "All done!"