4b329505f517a69372fdc576231c4a4d307a91f7
[src/xds/xds-server.git] / scripts / xds-utils / get-syncthing.sh
1 #!/bin/bash
2  ###########################################################################
3 # Copyright 2017 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/server
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 gpg -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
64
65 tarball="syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}.${TB_EXT}" \
66         && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
67     && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/sha1sum.txt.asc" -O \
68         && gpg -q --verify sha1sum.txt.asc \
69         && grep -E " ${tarball}\$" sha1sum.txt.asc | sha1sum -c - \
70         && rm -rf sha1sum.txt.asc syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}
71         if [ "${TB_EXT}" = "tar.gz" ]; then
72         tar -xvf "$tarball" --strip-components=1 "$(basename "$tarball" .tar.gz)"/syncthing \
73         && mv syncthing ${DESTDIR}/syncthing || exit 1
74     else
75         unzip "$tarball" && mv syncthing-windows-*/syncthing.exe ${DESTDIR}/syncthing.exe || exit 1
76     fi
77
78 echo "Get Syncthing-inotify..."
79 if [ "$SYNCTHING_INOTIFY_VERSION" = "master" ]; then
80     mkdir -p ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing || exit 1
81     cd ${TEMPDIR}/syncthing-inotify-build/src/github.com/syncthing
82     [[ ! -d ./syncthing ]] && (git clone https://github.com/syncthing/syncthing || exit 1; )
83     [[ ! -d ./syncthing-inotify ]] && (git clone https://github.com/syncthing/syncthing-inotify || exit 1; )
84     cd syncthing-inotify
85     git status
86     version=$(git describe --tags --always | sed 's/^v//')__patch_165
87     if [ "$SYNCTHING_INOTIFY_CMID" != "" ]; then
88         git checkout -q $SYNCTHING_INOTIFY_CMID || exit 1
89         version=${version}__patch_165
90     fi
91
92     # Workaround about "cannot find package "golang.org/x/sys/unix"
93     go get -u golang.org/x/sys/unix
94
95     # Workaround about "undefined: stream" error when cross-building MacOS
96     # https://github.com/rjeczalik/notify/issues/108
97     OPTS=""
98     [[ "$GOOS_STI" = "darwin" ]] && OPTS="-tags kqueue"
99
100     export GOPATH=$(cd ../../../.. && pwd)
101     go build -v -i -ldflags "-w -X main.Version=$version" -o ${DESTDIR}/syncthing-inotify${EXT} || exit 1
102 else
103
104     tarball="syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}.${TB_EXT}"
105     curl -sfSL "https://github.com/syncthing/syncthing-inotify/releases/download/v${SYNCTHING_INOTIFY_VERSION}/${tarball}" -O || exit 1
106     rm -rf syncthing-inotify-${GOOS_STI}-${GOARCH}-v${SYNCTHING_INOTIFY_VERSION}
107     if [ "${TB_EXT}" = "tar.gz" ]; then
108         tar -xvf "${tarball}" syncthing-inotify && mv syncthing-inotify ${DESTDIR}/syncthing-inotify || exit 1
109     else
110         unzip "$tarball" && mv syncthing-inotify.exe ${DESTDIR}/syncthing-inotify.exe || exit 1
111     fi
112 fi
113
114 echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"