Fix syncthing-inotify build error
[src/xds/xds-agent.git] / scripts / get-syncthing.sh
1 #!/bin/bash
2
3 # Configurable variables
4 [ -z "$SYNCTHING_VERSION" ] && SYNCTHING_VERSION=0.14.28
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; SYNCTHING_INOTIFY_CMID=af6fbf9d63f95a0; }
9 [ -z "$DESTDIR" ] && DESTDIR=/usr/local/bin
10 [ -z "$TMPDIR" ] && TMPDIR=/tmp
11 [ -z "$GOOS" ] && GOOS=$(go env GOOS)
12 [ -z "$GOARCH" ] && GOARCH=$(go env GOARCH)
13 [ -z "$CLEANUP" ] && CLEANUP=false
14
15
16 TEMPDIR=$TMPDIR/.get-syncthing.tmp
17 mkdir -p ${TEMPDIR} && cd ${TEMPDIR} || exit 1
18 trap "cleanExit" 0 1 2 15
19 cleanExit ()
20 {
21     if [ "$CLEANUP" = "true" ]; then
22         rm -rf ${TEMPDIR}
23     fi
24 }
25
26 TB_EXT="tar.gz"
27 EXT=""
28 [[ "$GOOS" = "windows" ]] && { TB_EXT="zip"; EXT=".exe"; }
29
30 GOOS_ST=${GOOS}
31 GOOS_STI=${GOOS}
32 [[ "$GOOS" = "darwin" ]] && GOOS_ST="macosx"
33
34 echo "Get Syncthing..."
35
36
37
38 ## Install Syncthing + Syncthing-inotify
39 ## gpg: key 00654A3E: public key "Syncthing Release Management <release@syncthing.net>" imported
40 gpg -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
41
42 tarball="syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}.${TB_EXT}" \
43         && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
44     && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/sha1sum.txt.asc" -O \
45         && gpg -q --verify sha1sum.txt.asc \
46         && grep -E " ${tarball}\$" sha1sum.txt.asc | sha1sum -c - \
47         && rm -rf sha1sum.txt.asc syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}
48         if [ "${TB_EXT}" = "tar.gz" ]; then
49         tar -xvf "$tarball" --strip-components=1 "$(basename "$tarball" .tar.gz)"/syncthing \
50         && mv syncthing ${DESTDIR}/syncthing || exit 1
51     else
52         unzip "$tarball" && mv syncthing-windows-*/syncthing.exe ${DESTDIR}/syncthing.exe || exit 1
53     fi
54
55 echo "Get Syncthing-inotify..."
56 if [ "$SYNCTHING_INOTIFY_VERSION" = "master" ]; then
57     mkdir -p ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing || exit 1
58     cd ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing
59     [[ ! -d ./syncthing ]] && (git clone https://github.com/syncthing/syncthing || exit 1; )
60     [[ ! -d ./syncthing-inotify ]] && (git clone https://github.com/syncthing/syncthing-inotify || exit 1; )
61     cd syncthing-inotify
62     if [ "$SYNCTHING_INOTIFY_CMID" != "" ]; then
63         git checkout -q $SYNCTHING_INOTIFY_CMID || exit 1
64     fi
65     git status
66     export GOPATH=$(cd ../../../.. && pwd)
67     version=$(git describe --tags --always | sed 's/^v//')__patch_165
68
69     # Workaround about "cannot find package "golang.org/x/sys/unix"
70     go get -u golang.org/x/sys/unix
71
72     # Workaround about "undefined: stream" error when cross-building MacOS
73     # https://github.com/rjeczalik/notify/issues/108
74     OPTS=""
75     [[ "$GOOS_STI" = "darwin" ]] && OPTS="-tags kqueue"
76
77     go build ${OPTS} -v -i -ldflags "-w -X main.Version=$version" -o ${DESTDIR}/syncthing-inotify${EXT} || exit 1
78 else
79
80     tarball="syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}.${TB_EXT}"
81     curl -sfSL "https://github.com/syncthing/syncthing-inotify/releases/download/v${SYNCTHING_INOTIFY_VERSION}/${tarball}" -O || exit 1
82         rm -rf syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}
83     if [ "${TB_EXT}" = "tar.gz" ]; then
84         tar -xvf "${tarball}" syncthing-inotify && mv syncthing-inotify ${DESTDIR}/syncthing-inotify || exit 1
85     else
86         unzip "$tarball" && mv syncthing-inotify.exe ${DESTDIR}/syncthing-inotify.exe || exit 1
87     fi
88 fi
89
90 echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"