summaryrefslogtreecommitdiff
path: root/herculeswrapper/include.sh
blob: f48e76589820f9b7d069934bfb846c65430c4a88 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env bash

function create_pipe {
    trap "rm -f $PIPE" EXIT
    if [[ ! -p $PIPE ]]; then
        echo "Making pipe $PIPE"
        rm -f $PIPE
        mkfifo $PIPE
    fi
}

# $1 - text
function send_all_pipes {
    echo $1 >$LOGIN_PIPE
    echo $1 >$CHAR_PIPE
}

# $1 - text
function send_char_pipe {
    echo $1 >$CHAR_PIPE
}

# $1 - server binary name
# $2 - sleep time
function server_logic {
    create_pipe
    $1

    while true
    do
        if read line <$PIPE; then
            echo pipe: $line
            if [[ "$line" == 'exit' ]]; then
                exit
            fi
            if [[ "$line" == 'restart' ]]; then
                sleep $2
                $1
            fi
        fi
    done
}

function hard_reset {
    echo ${1}: hard reset
    git fetch upstream
    git reset --hard upstream/master
    git clean -f
    # the following should be unnecessary, but just in case:
    git checkout --detach
    git branch -D master
    git checkout upstream/master -b master
}

function pull_all {
    hard_reset "data"
    cd ../client-data
    hard_reset "client"
    cd ../music
    hard_reset "music"
    cd ../server-code
    hard_reset "code"
    cd src/evol
    hard_reset "plugin"
    cd ../../../tools
    hard_reset "tools"
    cd ..
    ./pull.sh
    cd server-data
    git_merge
    cd ../
    ./status.sh
    cd server-data
    make maps
}

function build_all {
    cd ../server-code
    ./build.sh 2>err.txt
    cat err.txt
    cd ../server-data
    make updatedb
}

function build_clean {
    cd ../tools/localserver
    ./clean.sh
    cd ../../server-data
}

function build_plugin {
    cd ../server-plugin
    ./build.sh 2>err.txt
    cat err.txt
    cd ../server-data
}

function git_merge {
    if [ -f ../tools/herculeswrapper/herc-map-wrapper-config ]
    then
        # prevent interaction
        export GIT_EDITOR=:
        # force commits to have predictable hashes
        export GIT_AUTHOR_DATE='1970-01-01 00:00 +0000'
        export GIT_COMMITTER_DATE='1970-01-01 00:00 +0000'

        reset=$'\e[m'
        black_fg=$'\e[30m'
        red_fg=$'\e[31m'
        green_fg=$'\e[32m'
        yellow_fg=$'\e[33m'
        blue_fg=$'\e[34m'
        magenta_fg=$'\e[35m'
        cyan_fg=$'\e[36m'
        gray_fg=$'\e[37m'

        reset2='##0'
        black_fg2='##0'
        red_fg2='##1'
        green_fg2='##2'
        blue_fg2='##3'
        orange_fg2='##4'
        yellow_fg2='##5'
        pink_fg2='##6'
        purple_fg2='##7'
        gray_fg2='##8'
        brown_fg2='##9'


        now()
        {
            date -u +'%F %T'
        }

        good()
        {
            echo "$(now)$green_fg" "$@" "$reset" >&2
        }

        info()
        {
            echo "$(now)$cyan_fg" "$@" "$reset" >&2
        }

        warning()
        {
            echo "$(now)$yellow_fg" "$@" "$reset" >&2
        }

        error()
        {
            echo "$(now)$red_fg" "$@" "$reset" >&2
        }

        good2()
        {
            echo \""$green_fg2" "$@" "$reset2"\",
        }

        info2()
        {
            echo \""$brown_fg2" "$@" "$reset2"\",
        }

        warning2()
        {
            echo \""$yellow_fg2" "$@" "$reset2"\",
        }

        error2()
        {
            echo \""$red_fg2" "$@" "$reset2"\",
        }

        run()
        {
            info '$' "$@"
            "$@"
        }

        wipe_logs()
        {
            echo -e "$(now)\n===\n" > $merge_log
            chmod +r $merge_log
        }

        do_git()
        {
            info '$' "git $@"
            echo -e "${PWD##*/} $ git $@" >> $merge_log
            git "$@" 2> >(tee -a $merge_log >&2)
            ec=$?

            # print a newline to logs on error
            if [[ $ec != 0 ]]; then echo "" >> $merge_log; fi
            return $ec
        }

        try_merge()
        {
            test -n "$1"
            local branch=$1 commit
            info 'commit=$(' git rev-parse --verify -q $branch ')'
            if ! commit=$(git rev-parse --verify -q $branch)
            then
                error2 bogus $branch >> $motd
                error bogus $branch
                return
            fi
            info commit=$commit
            if run bash -c 'set -o pipefail; git branch --contains '$commit' | grep -qw master'
            then
                warning2 already $branch $commit >> $motd
                warning already $branch
                return
            fi
            if git merge-base --is-ancestor $branch $commit && do_git merge --ff-only $branch $commit
            then
                good2 fast $branch $commit >> $motd
                good fast $branch $commit
                return
            fi
            if do_git merge $branch $commit
            then
                good2 merge $branch $commit >> $motd
                good merge $branch $commit
                return
            fi
            git merge --abort
            error2 abort $branch $commit >> $motd
            error abort $branch $commit
            return
        }

        start_time=$(date +%s)
        source ../tools/herculeswrapper/herc-map-wrapper-config

        run test -f $motd
        echo -e 'function\tscript\tMOTD_debug_text\t{\n    setarray $@Debug_Messages$[0],' > $motd
        merge_log=${tmw_tools}/herculeswrapper/stderr.log
        wipe_logs
        do_git fetch --all
        do_git reset --hard $server_main_branch
        info 'commit=$(' git rev-parse --verify -q $server_main_branch ')'
        commit=$(git rev-parse --verify -q $server_main_branch)
        info2 server base $server_main_branch $commit >> $motd
        for branch in ${server_extra_branches[@]}
        do
            try_merge $branch
        done

        run cd $client_data

        # force new master and back up for future restoration
        do_git fetch --all
        do_git reset --hard $client_main_branch

        info 'commit=$(' git rev-parse --verify -q $client_main_branch ')'
        commit=$(git rev-parse --verify -q $client_main_branch)
        info2 client base $client_main_branch $commit >> $motd
        for branch in ${client_extra_branches[@]}
        do
            try_merge $branch
        done

        run cd $evol_music

        do_git fetch --all
        do_git reset --hard $music_main_branch
        info 'commit=$(' git rev-parse --verify -q $music_main_branch ')'
        commit=$(git rev-parse --verify -q $music_main_branch)
        info2 music base $music_main_branch $commit >> $motd
        for branch in ${music_extra_branches[@]}
        do
            try_merge $branch
        done

        echo '"End of Commits";' >> $motd
        echo -e '    return;\n}' >> $motd

        # generate the transient updates
        run cd $tmw_tools/update/
        run ./createnew.sh
        run ./create_music.sh
        run cd ../../server-data/
    fi
}