| 1 | #!/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 | echo "$(basename "$0") [--dir <dir>] [--dry-run] [<lang> ...]" |
| 43 | exit 0 |
| 44 | ;; |
| 45 | *) |
| 46 | LANGS+=("$1") |
| 47 | shift; |
| 48 | ;; |
| 49 | esac |
| 50 | done |
| 51 | |
| 52 | git rev-parse --is-inside-work-tree >/dev/null 2>&1 || { |
| 53 | echo "Error: Not inside a Git repository." >&2 |
| 54 | exit 1 |
| 55 | } |
| 56 | |
| 57 | # default to all dirs defined by TP_PROJECTS |
| 58 | if [ ${#DIRS[@]} -eq 0 ]; then |
| 59 | DIRS=( ${!TP_PROJECTS[@]} ) |
| 60 | fi |
| 61 | |
| 62 | function get_author { |
| 63 | echo $(gawk 'BEGIN { FS=": " } /Last-Translator/ { sub("\\\\n\"", ""); print $2 }' "$1") |
| 64 | } |
| 65 | |
| 66 | function get_revision { |
| 67 | echo $(gawk 'BEGIN { FS=": " } /PO-Revision-Date/ { sub("\\\\n\"", ""); print $2 }' "$1" \ |
| 68 | | date '+%s' -f -) |
| 69 | } |
| 70 | |
| 71 | function get_revision_orig { |
| 72 | echo $(git show HEAD:"$1" \ |
| 73 | | gawk 'BEGIN { FS=": " } /PO-Revision-Date/ { sub("\\\\n\"", ""); print $2 }' \ |
| 74 | | date '+%s' -f -) |
| 75 | } |
| 76 | |
| 77 | function git_commit { |
| 78 | local POFILE="$1" |
| 79 | local MSG="$2" |
| 80 | local AUTHOR=$(get_author "$POFILE") |
| 81 | |
| 82 | git commit --author "$AUTHOR" -m "$MSG" "$POFILE" |
| 83 | } |
| 84 | |
| 85 | function save_modified_files { |
| 86 | local POFILENAME="$1" |
| 87 | local PODIR="$2" |
| 88 | local POFILE="${PODIR}/${POFILENAME}" |
| 89 | |
| 90 | printf " %s : %10s : " $PODIR $POFILENAME |
| 91 | |
| 92 | new_rev=$(get_revision "$POFILE") |
| 93 | old_rev=$(get_revision_orig "$POFILE") |
| 94 | |
| 95 | if [ $new_rev -gt $old_rev ]; then |
| 96 | if [ "$DRYRUN" = "true" ]; then |
| 97 | echo " updated (ignore, dry-run)" |
| 98 | else |
| 99 | echo " updated" |
| 100 | git_commit $POFILE "$PODIR: update $POFILENAME (from translationproject.org)" |
| 101 | fi |
| 102 | else |
| 103 | echo " ok" |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | function git_files { |
| 108 | local MODE="$1" |
| 109 | local PODIR="$2" |
| 110 | |
| 111 | echo $(git ls-files $MODE "${PODIR}/*.po" \ |
| 112 | | gawk '/[[:alpha:]_\-]*\.po/ { sub(".*/", ""); print $0; }' \ |
| 113 | | sort) |
| 114 | } |
| 115 | |
| 116 | function download_files { |
| 117 | local PROJECT="$1" |
| 118 | local PODIR="$2" |
| 119 | |
| 120 | echo -n " Dowloding from ${URL}/${PROJECT} ..." |
| 121 | |
| 122 | if [ ${#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 | echo " done" |
| 130 | } |
| 131 | |
| 132 | function update_project { |
| 133 | local PROJECT="$1" |
| 134 | local PODIR="$2" |
| 135 | |
| 136 | echo |
| 137 | echo "== Syncing $PROJECT to $PODIR/ ===" |
| 138 | |
| 139 | download_files $PROJECT $PODIR |
| 140 | |
| 141 | PO_NEW=$(git_files -o $PODIR) |
| 142 | PO_MOD=$(git_files -m $PODIR) |
| 143 | |
| 144 | for f in $PO_MOD; do |
| 145 | save_modified_files $f $PODIR |
| 146 | done |
| 147 | |
| 148 | for f in $PO_NEW; do |
| 149 | git add "$PODIR/$f" |
| 150 | git_commit "$PODIR/$f" "$PODIR: add $f (from translationproject.org)" |
| 151 | done |
| 152 | |
| 153 | if [ -f "${PODIR}/LINGUAS" ]; then |
| 154 | LINGUAS=$(find $PODIR"/" -name '*.po' -type f -printf '%P\n' | sed 's/\.po//g' | sort) |
| 155 | echo "$LINGUAS" > "${PODIR}/LINGUAS" |
| 156 | |
| 157 | if [ "$(git ls-files -m "${PODIR}/LINGUAS")" = "${PODIR}/LINGUAS" ]; then |
| 158 | if [ "$DRYRUN" = "true" ]; then |
| 159 | echo ""${PODIR}/LINGUAS" needs update" |
| 160 | else |
| 161 | git commit -m "$PODIR: update LINGUAS list" "${PODIR}/LINGUAS" |
| 162 | fi |
| 163 | fi |
| 164 | fi |
| 165 | |
| 166 | if [ -f "${PODIR}/po4a.cfg" ]; then |
| 167 | LANGS_LINE=$(find $PODIR"/" -name '*.po' -type f -printf '%P\n' | sed 's/\.po//g' | sort | tr '\n' ' ' | sed 's/ $//') |
| 168 | sed -i "s/^\[po4a_langs\].*$/[po4a_langs] ${LANGS_LINE}/" "${PODIR}/po4a.cfg" |
| 169 | |
| 170 | if [ "$(git ls-files -m "${PODIR}/po4a.cfg")" = "${PODIR}/po4a.cfg" ]; then |
| 171 | if [ "$DRYRUN" = "true" ]; then |
| 172 | echo "${PODIR}/po4a.cfg needs update" |
| 173 | else |
| 174 | git commit -m "$PODIR: update po4a.cfg language list" "${PODIR}/po4a.cfg" |
| 175 | fi |
| 176 | fi |
| 177 | fi |
| 178 | |
| 179 | # cleanup |
| 180 | git checkout -f "$PODIR"/ &> /dev/null |
| 181 | } |
| 182 | |
| 183 | function regenerate_po_files { |
| 184 | local PODIR="$1" |
| 185 | |
| 186 | if [ "$PODIR" = "po" ]; then |
| 187 | if [ ! -f "${PODIR}/Makefile" ]; then |
| 188 | echo "missing ${PODIR}/Makefile (ingore)" |
| 189 | return; |
| 190 | else |
| 191 | echo "== Updating $PODIR (be patient) ==" |
| 192 | make -C $PODIR update-po &> /dev/null |
| 193 | fi |
| 194 | elif [ "$PODIR" = "po-man" ]; then |
| 195 | echo "== Updating $PODIR (be patient) ==" |
| 196 | make gen-poman-dist |
| 197 | else |
| 198 | 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=$(git diff --numstat $PODIR/*.po | gawk -v ORS=" " '/1[[:blank:]]1[[:blank:]]/ { print $3 }') |
| 204 | if [ -n "$PO_IGNORE" ]; then |
| 205 | git checkout -f $PO_IGNORE &> /dev/null |
| 206 | fi |
| 207 | |
| 208 | if [ $(git ls-files -m "$PODIR" | wc -l) -gt 0 ]; then |
| 209 | if [ "$DRYRUN" = "true" ]; then |
| 210 | echo "${PODIR}: needs merge changes" |
| 211 | else |
| 212 | git commit -m "$PODIR: merge changes" $PODIR |
| 213 | fi |
| 214 | fi |
| 215 | |
| 216 | git checkout -f "$PODIR" &> /dev/null |
| 217 | } |
| 218 | |
| 219 | for d in "${DIRS[@]}"; do |
| 220 | update_project "${TP_PROJECTS[$d]}" "$d" |
| 221 | regenerate_po_files "$d" |
| 222 | done |