summarylogtreecommitdiffstats
path: root/tws_check_update
blob: 70471603aaad1928e118afedad8ea79d2a19d978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash

# Exit status codes:
#   0 =  Existing version is latest
#   1 =  New version of TWS available
#   2 =  Error

DOWNLOAD_DIR="$HOME/.tws_scripts"
FILE_MIRROR="tws-latest-standalone-linux-x64.sh"
URL_MIRROR="https://download2.interactivebrokers.com/installers/tws/latest-standalone"
FILE_CUR="${DOWNLOAD_DIR}/${FILE_MIRROR}"

get_perm_file_name()
{
	local ver;
	local dst;
	ver="$(tws_get_version "$1" || echo unknown)"
	dst="$(echo "$1" | sed -e "s/\(.*\)\.\(.*\)/\1-${ver}.\2/g" -e 's/_latest//g')"
	echo "${dst}"
}

mkdir -p $DOWNLOAD_DIR

TDIR="$(mktemp -d -t twsdl.XXXX)" || exit 2
trap "rm -rf ${TDIR}" exit
cd "${TDIR}" || exit 2

# create fake sparse file for wget because wget -K is broken and -O file
# conflicts with -N
if test -f "${FILE_CUR}" ; then
	truncate -r "${FILE_CUR}" "${FILE_MIRROR}"
	touch -r "${FILE_CUR}" "${FILE_MIRROR}"
fi

if wget --no-verbose -U x -N -P "${TDIR}" "${URL_MIRROR}/${FILE_MIRROR}" ;then
    if test "${FILE_MIRROR}" -nt "${FILE_CUR}" ; then
	DST="$(get_perm_file_name "${FILE_MIRROR}")"
        mv "${FILE_MIRROR}" "${DOWNLOAD_DIR}/${DST}"
	ln -sf "${DST}" "${FILE_CUR}"
	echo "TWS $(echo "${DST}" |sed 's/.*-\(.*\)\..*/\1/g') fetched" 1>&2
	exit 1
    else
        echo "TWS $(readlink "${DOWNLOAD_DIR}/${FILE_MIRROR}" |sed 's/.*-\(.*\)\..*/\1/g') is latest" 1>&2
	exit 0
    fi
else
    echo "TWS update failed" 1>&2
    exit 2
fi