Reset by default LD_LIBRARY_PATH in xds-project.conf
[src/xds/xds-agent.git] / scripts / get-syncthing.sh
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2017-2018 IoT.bzh
4 #
5 # author: Sebastien Douheret <sebastien@iot.bzh>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ###########################################################################
19
20 # Configurable variables
21 [ -z "$SYNCTHING_VERSION" ] && SYNCTHING_VERSION=0.14.28
22 [ -z "$SYNCTHING_INOTIFY_VERSION" ] && SYNCTHING_INOTIFY_VERSION=0.8.6
23 # XXX - may be cleanup
24 # Used as temporary HACK while waiting merge of #165
25 #[ -z "$SYNCTHING_INOTIFY_VERSION" ] && { SYNCTHING_INOTIFY_VERSION=master; SYNCTHING_INOTIFY_CMID=af6fbf9d63f95a0; }
26 [ -z "$DESTDIR" ] && DESTDIR=/opt/AGL/xds/agent
27 [ -z "$TMPDIR" ] && TMPDIR=/tmp
28 [ -z "$GOOS" ] && GOOS=$(go env GOOS)
29 [ -z "$GOARCH" ] && GOARCH=$(go env GOARCH)
30 [ -z "$CLEANUP" ] && CLEANUP=false
31
32
33 TEMPDIR=$TMPDIR/.get-syncthing.tmp
34 mkdir -p ${TEMPDIR} && cd ${TEMPDIR} || exit 1
35 trap "cleanExit" 0 1 2 15
36 cleanExit ()
37 {
38     if [ "$CLEANUP" = "true" ]; then
39         rm -rf ${TEMPDIR}
40     fi
41 }
42
43 TB_EXT="tar.gz"
44 EXT=""
45 [[ "$GOOS" = "windows" ]] && { TB_EXT="zip"; EXT=".exe"; }
46
47 GOOS_ST=${GOOS}
48 GOOS_STI=${GOOS}
49 [[ "$GOOS" = "darwin" ]] && GOOS_ST="macosx"
50
51 echo "Get Syncthing..."
52
53
54
55 ## Install Syncthing + Syncthing-inotify
56 ## gpg: key 00654A3E: public key "Syncthing Release Management <release@syncthing.net>" imported
57 GPG=$(which gpg)
58 if [ "$?" != 0 ]; then
59     echo "You must install first gpg ( eg.: sudo apt install gpg )"
60     exit 1
61 fi
62
63 # Workaround about issue with the particular GPG keyserver:
64 # trying several explicit keyservers until the command succeeds
65 # (see https://github.com/jacobalberty/unifi-docker/issues/64)
66 for server in $(shuf -e pool.sks-keyservers.net \
67                         ha.pool.sks-keyservers.net \
68                         hkp://p80.pool.sks-keyservers.net:80 \
69                         keyserver.ubuntu.com \
70                         hkp://keyserver.ubuntu.com:80 \
71                         pgp.mit.edu) ; do
72     ${GPG} -q --keyserver "$server" --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E && break || :
73 done
74
75 tarball="syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}.${TB_EXT}" \
76     && curl --connect-timeout 60 --retry 3 -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
77     && curl --connect-timeout 60 --retry 3 -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/sha1sum.txt.asc" -O \
78     && gpg -q --verify sha1sum.txt.asc \
79     && grep -E " ${tarball}\$" sha1sum.txt.asc | sha1sum -c - \
80     && rm -rf sha1sum.txt.asc syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}
81     if [ "${TB_EXT}" = "tar.gz" ]; then
82         tar -xvf "$tarball" --strip-components=1 "$(basename "$tarball" .tar.gz)"/syncthing \
83         && mv syncthing ${DESTDIR}/syncthing || exit 1
84     else
85         unzip "$tarball" && mv syncthing-windows-*/syncthing.exe ${DESTDIR}/syncthing.exe || exit 1
86     fi
87
88 echo "Get Syncthing-inotify..."
89 if [ "$SYNCTHING_INOTIFY_VERSION" = "master" ]; then
90     mkdir -p ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing || exit 1
91     cd ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing
92     [[ ! -d ./syncthing ]] && (git clone https://github.com/syncthing/syncthing || exit 1; )
93     [[ ! -d ./syncthing-inotify ]] && (git clone https://github.com/syncthing/syncthing-inotify || exit 1; )
94     cd syncthing-inotify
95     git status
96     version=$(git describe --tags --always | sed 's/^v//')__patch_165
97     if [ "$SYNCTHING_INOTIFY_CMID" != "" ]; then
98         git checkout -q $SYNCTHING_INOTIFY_CMID || exit 1
99         version=${version}__patch_165
100     fi
101
102     # Workaround about "cannot find package "golang.org/x/sys/unix"
103     go get -u golang.org/x/sys/unix
104
105     # Workaround about "undefined: stream" error when cross-building MacOS
106     # https://github.com/rjeczalik/notify/issues/108
107     OPTS=""
108     [[ "$GOOS_STI" = "darwin" ]] && OPTS="-tags kqueue"
109
110     export GOPATH=$(cd ../../../.. && pwd)
111     go build ${OPTS} -v -i -ldflags "-w -X main.Version=$version" -o ${DESTDIR}/syncthing-inotify${EXT} || exit 1
112 else
113
114     tarball="syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}.${TB_EXT}"
115     curl --connect-timeout 60 --retry 3 -sfSL "https://github.com/syncthing/syncthing-inotify/releases/download/v${SYNCTHING_INOTIFY_VERSION}/${tarball}" -O || exit 1
116     rm -rf syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}
117     if [ "${TB_EXT}" = "tar.gz" ]; then
118         tar -xvf "${tarball}" syncthing-inotify && mv syncthing-inotify ${DESTDIR}/syncthing-inotify || exit 1
119     else
120         unzip "$tarball" && mv syncthing-inotify.exe ${DESTDIR}/syncthing-inotify.exe || exit 1
121     fi
122 fi
123
124 echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"