| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | # |
| 5 | # git-tp-sync - downloads the latest PO files from translationproject.org |
| 6 | # and commits changes to your GIT repository. |
| 7 | # |
| 8 | # Copyright (C) 2007-2025 Karel Zak <[email protected]> |
| 9 | # |
| 10 | # This program is free software; you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation; either version 2 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | declare -A TP_PROJECTS |
| 16 | |
| 17 | # Define local directories and project names on TP.org |
| 18 | # |
| 19 | # TP_PROJECTS[<dirname>]=<project> |
| 20 | # |
| 21 | TP_PROJECTS["po"]="util-linux" |
| 22 | TP_PROJECTS["po-man"]="util-linux-man" |
| 23 | |
| 24 | URL="rsync://translationproject.org/tp/latest" |
| 25 | |
| 26 | DRYRUN="false" |
| 27 | LANGS=() |
| 28 | DIRS=() |
| 29 | |
| 30 | while [[ $# -gt 0 ]]; do |
| 31 | case $1 in |
| 32 | --dry-run) |
| 33 | DRYRUN="true" |
| 34 | shift |
| 35 | ;; |
| 36 | --dir) |
| 37 | DIRS+=("$2") |
| 38 | shift |
| 39 | shift |
| 40 | ;; |
| 41 | --usage | --help) |
| 42 | echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0") [--dir <dir>] [--dry-run] [<lang> ...]" |
| 43 | exit 0 |
| 44 | ;; |
| 45 | *) |
| 46 | LANGS+=("$1") |
| 47 | shift; |
| 48 | ;; |
| 49 | esac |
| 50 | done |
| 51 | |
| 52 | gitscip-shell system . . git. rev-parse --is-inside-work-tree >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || { |
| 53 | echoscip-shell system . . echo. "Error: Not inside a Git repository." >&2 |
| 54 | exit 1 |
| 55 | } |
| 56 | |
| 57 | # default to all dirs defined by TP_PROJECTS |
| 58 | if [scip-shell system . . `[`. ${#DIRS[@]} -eq 0 ]; then |
| 59 | DIRS=( ${!TP_PROJECTS[@]} ) |
| 60 | fi |
| 61 | |
| 62 | function get_authorscip-shell shell util-linux 2.42.2-1 get_author(). { |
| 63 | echoscip-shell system . . echo. $(gawkscip-shell system . . gawk. 'BEGIN { FS=": " } /Last-Translator/ { sub("\\\\n\"", ""); print $2 }' "$1") |
| 64 | } |
| 65 | |
| 66 | function get_revisionscip-shell shell util-linux 2.42.2-1 get_revision(). { |
| 67 | echoscip-shell system . . echo. $(gawkscip-shell system . . gawk. 'BEGIN { FS=": " } /PO-Revision-Date/ { sub("\\\\n\"", ""); print $2 }' "$1" \ |
| 68 | | date '+%s' -f -) |
| 69 | } |
| 70 | |
| 71 | function get_revision_origscip-shell shell util-linux 2.42.2-1 get_revision_orig(). { |
| 72 | echoscip-shell system . . echo. $(gitscip-shell system . . git. show HEAD:"$1" \ |
| 73 | | gawk 'BEGIN { FS=": " } /PO-Revision-Date/ { sub("\\\\n\"", ""); print $2 }' \ |
| 74 | | date '+%s' -f -) |
| 75 | } |
| 76 | |
| 77 | function git_commitscip-shell shell util-linux 2.42.2-1 git_commit(). { |
| 78 | local POFILE="$1" |
| 79 | local MSG="$2" |
| 80 | local AUTHOR=$(get_authorscip-shell shell util-linux 2.42.2-1 get_author(). "$POFILE") |
| 81 | |
| 82 | gitscip-shell system . . git. commit --author "$AUTHOR" -m "$MSG" "$POFILE" |
| 83 | } |
| 84 | |
| 85 | function save_modified_filesscip-shell shell util-linux 2.42.2-1 save_modified_files(). { |
| 86 | local POFILENAME="$1" |
| 87 | local PODIR="$2" |
| 88 | local POFILE="${PODIR}/${POFILENAME}" |
| 89 | |
| 90 | printfscip-shell system . . printf. " %s : %10s : " $PODIR $POFILENAME |
| 91 | |
| 92 | new_rev=$(get_revisionscip-shell shell util-linux 2.42.2-1 get_revision(). "$POFILE") |
| 93 | old_rev=$(get_revision_origscip-shell shell util-linux 2.42.2-1 get_revision_orig(). "$POFILE") |
| 94 | |
| 95 | if [scip-shell system . . `[`. $new_rev -gt $old_rev ]; then |
| 96 | if [scip-shell system . . `[`. "$DRYRUN" = "true" ]; then |
| 97 | echoscip-shell system . . echo. " updated (ignore, dry-run)" |
| 98 | else |
| 99 | echoscip-shell system . . echo. " updated" |
| 100 | git_commitscip-shell shell util-linux 2.42.2-1 git_commit(). $POFILE "$PODIR: update $POFILENAME (from translationproject.org)" |
| 101 | fi |
| 102 | else |
| 103 | echoscip-shell system . . echo. " ok" |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | function git_filesscip-shell shell util-linux 2.42.2-1 git_files(). { |
| 108 | local MODE="$1" |
| 109 | local PODIR="$2" |
| 110 | |
| 111 | echoscip-shell system . . echo. $(gitscip-shell system . . git. ls-files $MODE "${PODIR}/*.po" \ |
| 112 | | gawk '/[[:alpha:]_\-]*\.po/ { sub(".*/", ""); print $0; }' \ |
| 113 | | sort) |
| 114 | } |
| 115 | |
| 116 | function download_filesscip-shell shell util-linux 2.42.2-1 download_files(). { |
| 117 | local PROJECT="$1" |
| 118 | local PODIR="$2" |
| 119 | |
| 120 | echoscip-shell system . . echo. -n " Dowloding from ${URL}/${PROJECT} ..." |
| 121 | |
| 122 | if [scip-shell system . . `[`. ${#LANGS[@]} -eq 0 ]; then |
| 123 | rsync -Lrtz "$URL"/"$PROJECT"/ "$PODIR" |
| 124 | else |
| 125 | for l in "${LANGS[@]}"; do |
| 126 | rsync -Lrtz "$URL"/"$PROJECT"/"$l".po "$PODIR" |
| 127 | done |
| 128 | fi |
| 129 | echoscip-shell system . . echo. " done" |
| 130 | } |
| 131 | |
| 132 | function update_projectscip-shell shell util-linux 2.42.2-1 update_project(). { |
| 133 | local PROJECT="$1" |
| 134 | local PODIR="$2" |
| 135 | |
| 136 | echoscip-shell system . . echo. |
| 137 | echoscip-shell system . . echo. "== Syncing $PROJECT to $PODIR/ ===" |
| 138 | |
| 139 | download_filesscip-shell shell util-linux 2.42.2-1 download_files(). $PROJECT $PODIR |
| 140 | |
| 141 | PO_NEW=$(git_filesscip-shell shell util-linux 2.42.2-1 git_files(). -o $PODIR) |
| 142 | PO_MOD=$(git_filesscip-shell shell util-linux 2.42.2-1 git_files(). -m $PODIR) |
| 143 | |
| 144 | for f in $PO_MOD; do |
| 145 | save_modified_filesscip-shell shell util-linux 2.42.2-1 save_modified_files(). $f $PODIR |
| 146 | done |
| 147 | |
| 148 | for f in $PO_NEW; do |
| 149 | gitscip-shell system . . git. add "$PODIR/$f" |
| 150 | git_commitscip-shell shell util-linux 2.42.2-1 git_commit(). "$PODIR/$f" "$PODIR: add $f (from translationproject.org)" |
| 151 | done |
| 152 | |
| 153 | if [scip-shell system . . `[`. -f "${PODIR}/LINGUAS" ]; then |
| 154 | LINGUAS=$(findscip-shell system . . find. $PODIR"/" -name '*.po' -type f -printf '%P\n' | sedscip-shell system . . sed. 's/\.po//g' | sortscip-shell system . . sort.) |
| 155 | echoscip-shell system . . echo. "$LINGUAS" > "${PODIR}/LINGUAS" |
| 156 | |
| 157 | if [scip-shell system . . `[`. "$(gitscip-shell system . . git. ls-files -m "${PODIR}/LINGUAS")" = "${PODIR}/LINGUAS" ]; then |
| 158 | if [scip-shell system . . `[`. "$DRYRUN" = "true" ]; then |
| 159 | echoscip-shell system . . echo. ""${PODIR}/LINGUAS" needs update" |
| 160 | else |
| 161 | gitscip-shell system . . git. commit -m "$PODIR: update LINGUAS list" "${PODIR}/LINGUAS" |
| 162 | fi |
| 163 | fi |
| 164 | fi |
| 165 | |
| 166 | if [scip-shell system . . `[`. -f "${PODIR}/po4a.cfg" ]; then |
| 167 | LANGS_LINE=$(findscip-shell system . . find. $PODIR"/" -name '*.po' -type f -printf '%P\n' | sedscip-shell system . . sed. 's/\.po//g' | sortscip-shell system . . sort. | trscip-shell system . . tr. '\n' ' ' | sedscip-shell system . . sed. 's/ $//') |
| 168 | sedscip-shell system . . sed. -i "s/^\[po4a_langs\].*$/[po4a_langs] ${LANGS_LINE}/" "${PODIR}/po4a.cfg" |
| 169 | |
| 170 | if [scip-shell system . . `[`. "$(gitscip-shell system . . git. ls-files -m "${PODIR}/po4a.cfg")" = "${PODIR}/po4a.cfg" ]; then |
| 171 | if [scip-shell system . . `[`. "$DRYRUN" = "true" ]; then |
| 172 | echoscip-shell system . . echo. "${PODIR}/po4a.cfg needs update" |
| 173 | else |
| 174 | gitscip-shell system . . git. commit -m "$PODIR: update po4a.cfg language list" "${PODIR}/po4a.cfg" |
| 175 | fi |
| 176 | fi |
| 177 | fi |
| 178 | |
| 179 | # cleanup |
| 180 | gitscip-shell system . . git. checkout -f "$PODIR"/ &> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 181 | } |
| 182 | |
| 183 | function regenerate_po_filesscip-shell shell util-linux 2.42.2-1 regenerate_po_files(). { |
| 184 | local PODIR="$1" |
| 185 | |
| 186 | if [scip-shell system . . `[`. "$PODIR" = "po" ]; then |
| 187 | if [scip-shell system . . `[`. ! -f "${PODIR}/Makefile" ]; then |
| 188 | echoscip-shell system . . echo. "missing ${PODIR}/Makefile (ingore)" |
| 189 | return; |
| 190 | else |
| 191 | echoscip-shell system . . echo. "== Updating $PODIR (be patient) ==" |
| 192 | makescip-shell system . . make. -C $PODIR update-po &> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 193 | fi |
| 194 | elif [scip-shell system . . `[`. "$PODIR" = "po-man" ]; then |
| 195 | echoscip-shell system . . echo. "== Updating $PODIR (be patient) ==" |
| 196 | makescip-shell system . . make. gen-poman-dist |
| 197 | else |
| 198 | echoscip-shell system . . echo. "Unsupported $PODIR directory (skip update)" |
| 199 | return; |
| 200 | fi |
| 201 | |
| 202 | # ignore files where is only modified one line ("POT-Creation-Date") |
| 203 | PO_IGNORE=$(gitscip-shell system . . git. diff --numstat $PODIR/*.po | gawkscip-shell system . . gawk. -v ORS=" " '/1[[:blank:]]1[[:blank:]]/ { print $3 }scip-shell filesystem . . `/1[[:blank:]]1[[:blank:]]/ { print $3 }`.') |
| 204 | if [scip-shell system . . `[`. -n "$PO_IGNORE" ]; then |
| 205 | gitscip-shell system . . git. checkout -f $PO_IGNORE &> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 206 | fi |
| 207 | |
| 208 | if [scip-shell system . . `[`. $(gitscip-shell system . . git. ls-files -m "$PODIR" | wcscip-shell system . . wc. -l) -gt 0 ]; then |
| 209 | if [scip-shell system . . `[`. "$DRYRUN" = "true" ]; then |
| 210 | echoscip-shell system . . echo. "${PODIR}: needs merge changes" |
| 211 | else |
| 212 | gitscip-shell system . . git. commit -m "$PODIR: merge changes" $PODIR |
| 213 | fi |
| 214 | fi |
| 215 | |
| 216 | gitscip-shell system . . git. checkout -f "$PODIR" &> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 217 | } |
| 218 | |
| 219 | for d in "${DIRS[@]}"; do |
| 220 | update_projectscip-shell shell util-linux 2.42.2-1 update_project(). "${TP_PROJECTS[$d]}" "$d" |
| 221 | regenerate_po_filesscip-shell shell util-linux 2.42.2-1 regenerate_po_files(). "$d" |
| 222 | done |