Use patched version of syncthing-inotify.
[src/xds/xds-server.git] / scripts / get-syncthing.sh
1 #!/bin/bash
2
3 # Configurable variables
4 [ -z "$SYNCTHING_VERSION" ] && SYNCTHING_VERSION=0.14.25
5
6 # FIXME: temporary HACK while waiting merge of #165
7 #[ -z "$SYNCTHING_INOTIFY_VERSION" ] && SYNCTHING_INOTIFY_VERSION=0.8.5
8 [ -z "$SYNCTHING_INOTIFY_VERSION" ] && SYNCTHING_INOTIFY_VERSION=master_and_patch165
9 [ -z "$DESTDIR" ] && DESTDIR=/usr/local/bin
10 [ -z "$TMPDIR" ] && TMPDIR=/tmp
11
12
13 TEMPDIR=$TMPDIR/.get-st.$$
14 mkdir -p ${TEMPDIR} && cd ${TEMPDIR} || exit 1
15 trap "cleanExit" 0 1 2 15
16 cleanExit ()
17 {
18    rm -rf ${TEMPDIR}
19 }
20
21 echo "Get Syncthing..."
22
23 ## Install Syncthing + Syncthing-inotify
24 ## gpg: key 00654A3E: public key "Syncthing Release Management <release@syncthing.net>" imported
25 gpg -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
26
27 tarball="syncthing-linux-amd64-v${SYNCTHING_VERSION}.tar.gz" \
28         && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
29     && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/sha1sum.txt.asc" -O \
30         && gpg -q --verify sha1sum.txt.asc \
31         && grep -E " ${tarball}\$" sha1sum.txt.asc | sha1sum -c - \
32         && rm sha1sum.txt.asc \
33         && tar -xvf "$tarball" --strip-components=1 "$(basename "$tarball" .tar.gz)"/syncthing \
34         && mv syncthing ${DESTDIR}/syncthing
35
36
37 echo "Get Syncthing-inotify..."
38 if [ "$SYNCTHING_INOTIFY_VERSION" = "master_and_patch165" ]; then
39     mkdir -p ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing || exit 1
40     cd ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing
41     git clone https://github.com/syncthing/syncthing || exit 1
42     git clone https://github.com/syncthing/syncthing-inotify || exit 1
43     cd syncthing-inotify
44     cat <<EOF > 165.patch
45     diff --git a/syncwatcher.go b/syncwatcher.go
46 index c36b034..5175c12 100644
47 --- a/syncwatcher.go
48 +++ b/syncwatcher.go
49 @@ -677,7 +677,10 @@ func accumulateChanges(debounceTimeout time.Duration,
50                 if flushTimerNeedsReset {
51                         flushTimerNeedsReset = false
52                         if !flushTimer.Stop() {
53 -                               <-flushTimer.C
54 +                               select {
55 +                               case <-flushTimer.C:
56 +                               default:
57 +                               }
58                         }
59                         flushTimer.Reset(currInterval)
60                 }
61 EOF
62     git apply 165.patch || exit 1
63     export GOPATH=$(realpath `pwd`/../../../..)
64     version=$(git describe --tags --always | sed 's/^v//')__patch_165
65     go build -v -i -ldflags "-w -X main.Version=$version" -o ${DESTDIR}/syncthing-inotify || exit 1
66 else
67
68 tarball="syncthing-inotify-linux-amd64-v${SYNCTHING_INOTIFY_VERSION}.tar.gz" \
69     && curl -sfSL "https://github.com/syncthing/syncthing-inotify/releases/download/v${SYNCTHING_INOTIFY_VERSION}/${tarball}" -O \
70     && tar -xvf "${tarball}" syncthing-inotify \
71         && mv syncthing-inotify ${DESTDIR}/syncthing-inotify
72 fi
73
74 echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"