summarylogtreecommitdiffstats
path: root/sheepit-client-launcher.sh
blob: ce3e1948a999a4e02d82421b55c3ef6add438a98 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash

# Prohibit running as root
if [ "($id -u)" == "0" ]; then
  echo 'This launcher should not be run as root.';
  exit 1;
fi

# Set up data directory
_DATA="${XDG_DATA_HOME:-$HOME/.local/share}/sheepit-client-launcher";
if [[ ! -d "${_DATA}" ]]; then
  mkdir -pv "${_DATA}";
fi

check_update() {
  echo 'Checking for update..';

  { _CHECKED_AT=$(< "${_DATA}/update_checked_at"); } 2>/dev/null;
  _CHECKED_AT="${_CHECKED_AT:-0}";
  _NEXT_CHECK_AT=$(("${_CHECKED_AT}" + 60 * 10));

  if (( "${_NEXT_CHECK_AT}" > "$(date +%s)" )); then
    echo 'Checking for update.. SKIPPED (reason: recently done)';
    return 0;
  fi

  echo -n 'Fetching MD5 of latest release.. ';
  _MD5_LATEST=$(curl --fail --silent https://www.sheepit-renderfarm.com/media/applet/client-info.php?get=md5);
  if [[ -z "${_MD5_LATEST}" ]]; then
    echo 'FAILED (reason: request error)';
    return 0;
  fi
  echo "${_MD5_LATEST}";

  echo -n 'Fetching MD5 of the current client.. ';
  _MD5_CURRENT=$(md5sum ${_DATA}/sheepit-client.jar | cut -f1 -d' ');
  echo "${_MD5_CURRENT}";

  if [[ "${_MD5_LATEST}" != "${_MD5_CURRENT}" ]]; then
    update;
  else
    echo 'Checking for update.. DONE.';
  fi

  date +%s > "${_DATA}/update_checked_at";
}

update() {
  echo 'Updating client..';

  echo -n 'Downloading file.. ';
  _TARGET=$(curl -OJ -s -w '%{filename_effective}' --output-dir "${_DATA}" 'https://www.sheepit-renderfarm.com/media/applet/client-latest.php');
  echo "${_TARGET}";

  echo 'Linking file..';
  ln -sfv "${_TARGET}"  "${_DATA}/sheepit-client.jar";

  echo 'Updating client.. DONE.';
}

if [[ ! -e "${_DATA}/sheepit-client.jar" ]]; then
  update;
else
  check_update;
fi

_CLIENT=$(readlink "${_DATA}/sheepit-client.jar");
echo "Launching ${_CLIENT%.jar}."
exec java -jar "${_DATA}/sheepit-client.jar" "$@";