da06d155d87974c3de6f6bb7868e236081bd63c3
[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
14
15 TEMPDIR=$TMPDIR/.get-st.$$
16 mkdir -p ${TEMPDIR} && cd ${TEMPDIR} || exit 1
17 trap "cleanExit" 0 1 2 15
18 cleanExit ()
19 {
20    rm -rf ${TEMPDIR}
21 }
22
23 TB_EXT="tar.gz"
24 EXT=""
25 [[ "$GOOS" = "windows" ]] && { TB_EXT="zip"; EXT=".exe"; }
26
27 echo "Get Syncthing..."
28
29 ## Install Syncthing + Syncthing-inotify
30 ## gpg: key 00654A3E: public key "Syncthing Release Management <release@syncthing.net>" imported
31 gpg -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
32
33 tarball="syncthing-${GOOS}-${GOARCH}-v${SYNCTHING_VERSION}.${TB_EXT}" \
34         && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
35     && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/sha1sum.txt.asc" -O \
36         && gpg -q --verify sha1sum.txt.asc \
37         && grep -E " ${tarball}\$" sha1sum.txt.asc | sha1sum -c - \
38         && rm sha1sum.txt.asc
39         if [ "${TB_EXT}" = "tar.gz" ]; then
40         tar -xvf "$tarball" --strip-components=1 "$(basename "$tarball" .tar.gz)"/syncthing \
41         && mv syncthing ${DESTDIR}/syncthing || exit 1
42     else
43         unzip "$tarball" && mv syncthing-windows-*/syncthing.exe ${DESTDIR}/syncthing.exe || exit 1
44     fi
45
46 echo "Get Syncthing-inotify..."
47 if [ "$SYNCTHING_INOTIFY_VERSION" = "master" ]; then
48     mkdir -p ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing || exit 1
49     cd ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing
50     git clone https://github.com/syncthing/syncthing || exit 1
51     git clone https://github.com/syncthing/syncthing-inotify || exit 1
52     cd syncthing-inotify
53     if [ "$SYNCTHING_INOTIFY_CMID" != "" ]; then
54         git checkout -q $SYNCTHING_INOTIFY_CMID || exit 1
55     fi
56     git status
57     export GOPATH=$(realpath `pwd`/../../../..)
58     version=$(git describe --tags --always | sed 's/^v//')__patch_165
59     go build -v -i -ldflags "-w -X main.Version=$version" -o ${DESTDIR}/syncthing-inotify${EXT} || exit 1
60 else
61
62     tarball="syncthing-inotify-${GOOS}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}.${TB_EXT}"
63     curl -sfSL "https://github.com/syncthing/syncthing-inotify/releases/download/v${SYNCTHING_INOTIFY_VERSION}/${tarball}" -O || exit 1
64     if [ "${TB_EXT}" = "tar.gz" ]; then
65         tar -xvf "${tarball}" syncthing-inotify && mv syncthing-inotify ${DESTDIR}/syncthing-inotify || exit 1
66     else
67         unzip "$tarball" && mv syncthing-inotify.exe ${DESTDIR}/syncthing-inotify.exe || exit 1
68     fi
69 fi
70
71 echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"