#!/bin/bash

# Check latest version
URL="https://update.zentyal.org/last-8.0.txt"
TIMEOUT="300"
DEST="/var/lib/zentyal/latestversion"
LK_REGEX="^[A-Z0-9]{5}-[A-Z0-9]{5}.[A-Z0-9]{5}-[A-Z0-9]{5}$"
LK_FILE='/var/lib/zentyal/.license'

if ! wget --timeout="$TIMEOUT" -q "$URL" -O "$DEST"  >/dev/null 2>&1; then
  exit 1
fi

# Stop services if commercial trial expired
if [[ "$(/usr/share/zentyal/shell '$global->edition()')" == "trial-expired" ]]; then
  zs restart >/dev/null 2>&1
fi

if [[ -f "$LK_FILE" ]] && grep -qE "$LK_REGEX" "$LK_FILE"; then
  /usr/share/zentyal/check_license >/dev/null 2>&1
fi

if [[ $? -eq 1 ]]; then
  zs webadmin stop
fi

exit 0
