#!/bin/sh
#
# Copyright (c) 2020 Mantas Kriaučiūnas <baltix@gmail.com>. All rights reserved.
# Use of this source code is governed by a GNU GPL v3 license

set -e

# System-wide package configuration.
DEFAULTS_FILE="/etc/default/baltix"
GRUB_DEFAULTS="/etc/default/grub"

APT_GET="`which apt-get 2> /dev/null`"
#APT_CONFIG="`which apt-config 2> /dev/null`"

## MAIN ##
if [ ! -e "$DEFAULTS_FILE" ]; then
  echo 'repo_add_once="true"' > "$DEFAULTS_FILE"
  echo 'repo_reenable_on_distupgrade="true"' >> "$DEFAULTS_FILE"
fi

#TODO: add disabled repo "deb https://download.onlyoffice.com/repo/debian squeeze main"
#Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package

#Work around for automatic mscorefonts installation for EduBaltix
echo msttcorefonts msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections

# Work-around an apt-get error when PPA decription is changed
"$APT_GET" -qq --allow-releaseinfo-change update || true

if [ -e "$GRUB_DEFAULTS" ]; then
  grep "GRUB_RECORDFAIL_TIMEOUT" "$GRUB_DEFAULTS" >/dev/null 2>&1 || /bin/echo 'GRUB_RECORDFAIL_TIMEOUT=5' >> "$GRUB_DEFAULTS"
fi

# Run the cron job immediately to perform repository configuration.
nohup sh /etc/cron.daily/add-fix-baltix-repo > /dev/null 2>&1 &
