summaryrefslogtreecommitdiffstats
path: root/scripts/extract_translator.sh
blob: bc4027205d3396bbc803f45ac6bdd547a976b4e1 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash

#
# author {
#  name "kuesji koesnu"
#  email "kuesji@koesnu.com"
#  website "kuesji.koesnu.com"
# }
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#



SINCE=$(date +%s --date="-1 month")
TEXT='Translated using Weblate'
INPUT_FILE=""
CSV_FILE=""
OUTPUT_FILE=""

set -ef

print_help(){
    usage='
 options
    --full    generate translator list from beginning ( only last month generated without this )
    -i file    give input file for previous contributors
    -o file    give output file for latest contributors list including previous
    --csv file    give input from csv file
    '

    echo "$usage"
}

find_hashs(){
    # if since is greater than 0 take last month's authors else full
    if [ "$SINCE" -gt 0 ]
    then
        git log --since="$SINCE" --pretty=format:"%h %s" --grep="$TEXT" | cut -d ' ' -f1
    else
        git log --pretty=format:"%h %s" --grep="$TEXT" | cut -d ' ' -f1
    fi
}

collect_authors(){
    for hash in $(find_hashs)
    do
        info=$(git show "$hash" --pretty=format:'%an' | head -n 1)
        echo $info
    done
}


# show help if no argument passed
if [ $# -lt 1 ]
then
    print_help
    exit 1
fi


# parsing arguments
while [ $# -gt 1 ];
do
    case "$1" in
        '--full')
            shift; SINCE="0";
            ;;
        '-i')
            shift; INPUT_FILE="$1"; shift;
            ;;
        '-o')
            shift; OUTPUT_FILE="$1"; shift;
            ;;
        '--csv')
            shift; CSV_FILE="$1"; shift;
            ;;
        *)
            shift;
            ;;
    esac
done

# we need an output list
if [ -z "$OUTPUT_FILE" ]
then
    echo 'error: please give output file with "-o filename" format' >&2
    exit 1
fi

# read input list if given
if [ ! -z "$INPUT_FILE" ] && [ -f "$INPUT_FILE" ]
then
    cat "$INPUT_FILE" > 'tmp.txt'
fi

# load csv if csv file given
if [ ! -z "$CSV_FILE" ] && [ -f "$CSV_FILE" ]
then
    # backup and switch delimiter to comma
    OLDIFS="$IFS"
    IFS=','
    for author in $( cat "$CSV_FILE" )
    do
        # first remove leading spaces, second sed trim trailing space
        echo "$author" |  sed 's,^ *,,g' | sed 's, *$,,g' >> 'tmp.txt'
    done
    # restore old delimitier
    IFS="$OLDIFS"
fi



# generate latest list and combine
collect_authors >> 'tmp.txt'
# read all, sort all, make unique and write to output file
cat 'tmp.txt' | sort | uniq > "$OUTPUT_FILE"

if [ ! -z "$CSV_FILE" ]
then
    # put a comma on end of every line
    sed -e 's/$/, /g' "$OUTPUT_FILE" > 'tmp.txt'
    # delete newline characters
    cat tmp.txt | tr -d '\n' > "$OUTPUT_FILE"
fi

# thanks for service tmp.txt, but we dont need you anymore
rm 'tmp.txt'

# vim:set shiftwidth=4 softtabstop=4 expandtab: