#!/bin/bash
. /etc/kona/soundsystem.inc

if [ ! -z $(pgrep -f unattended-upgrade) ]; then
  killall unattended-upgrade
fi

if [ ! -z "$(grep x86_64 /etc/kona/os-release)" ]; then
  arch="x86_64"
else
  arch="i386"
fi

apt-get -y --force-yes -f install

trace "update-desktop-bg start"
#apt-get update

if [ ! -d /etc/kona/DirettaHostSDK_148 ] || [ ! -z "$(diff /etc/kona/DirettaHostSDK_148/sdk_version /etc/kona/diretta/sdk_version)" ]; then
  trace "DirettaHostSDK_148 download start"
  wget -q https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/DirettaHostSDK_148.tgz
  if [ -f DirettaHostSDK_148.tgz ]; then
    trace "DirettaHostSDK download success"
    rm -fr /etc/kona/DirettaHostSDK_148
    tar xfz DirettaHostSDK_148.tgz -C/etc/kona
    cp /etc/kona/DirettaHostSDK_148/sdk_version /etc/kona/diretta
    trace "sdk_version=$(cat /etc/kona/diretta/sdk_version)"
    rm -f DirettaHostSDK_148.tgz
#    rm -f /etc/kona/diretta/*/alsa_bridge.ko
  else
    trace "DorettaHostSDK download failed"
  fi
fi
if [ -z "$(lscpu|grep avx)" ]; then
  if [ ! -f /etc/kona/diretta/syncAlsa_gcc15_x64_v2 ] || [ -f /etc/kona/alsahost-update ]; then
    wget -q https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/diretta_noavx.tgz
    if [ ! -z "$(pgrep -f syncAlsa)" ]; then
      systemctl stop diretta_sync_host
      systemctl stop diretta_sync_host2
    fi
    tar xfz diretta_noavx.tgz -C /etc/kona/diretta
    rm /etc/kona/diretta/syncAlsa
    rm /etc/kona/diretta/syncAlsa_nolog
    ln -s /etc/kona/diretta/syncAlsa_gcc15_x64_v2 /etc/kona/diretta/syncAlsa
    ln -s /etc/kona/diretta/syncAlsa_gcc15_x64_v2_nolog /etc/kona/diretta/syncAlsa_nolog
    rm diretta_noavx.tgz
    rm -f squeeze2upnp.tgz
  fi
else
  if [ ! -f /etc/kona/diretta/syncAlsa_gcc15_x64_v3 ] || [ -f /etc/kona/alsahost-update ]; then
    wget -q https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/diretta.tgz
    if [ ! -z "$(pgrep -f syncAlsa)" ]; then
      systemctl stop diretta_sync_host
      systemctl stop diretta_sync_host2
    fi
    tar xfz diretta.tgz -C/etc/kona/diretta
    if [ ! -f /etc/kona/diretta/syncAlsa ]; then
      ln -s /etc/kona/diretta/syncAlsa_gcc15_x64_v3 /etc/kona/diretta/syncAlsa
      ln -s /etc/kona/diretta/syncAlsa_gcc15_x64_v3_nolog /etc/kona/diretta/syncAlsa_nolog
    fi
    rm -f diretta.tgz /etc/kona/alsahost-update
    rm -f squeeze2upnp.tgz
  fi
fi

if [ ! -d /etc/kona/diretta-renderer-2/ffmpeg-headers/libavcodec ]; then
  wget -q https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/ffmpeg-headers.tgz
  rm -fr /etc/kona/diretta-renderer-2/ffmpeg-headers
  tar xfz ffmpeg-headers.tgz -C/etc/kona/diretta-renderer-2
  rm ffmpeg-headers.tgz
  trace "ffmpeg-headers is downloaded and extracted to diretta-renderer-2"
  if [ ! -z "$(diff /etc/kona/diretta-renderer-2/renderer_version diretta-renderer-2/renderer_version)" ]; then
    cp /etc/kona/update/update.tgz /etc/kona/update/update-$(cat /etc/kona/version).tgz
    trace "restart update diretta-renderer-2"
  fi
fi

if [ -z "$(dpkg -l|grep cpufrequtils)" ]; then
  apt -y install cpufrequtils
fi

apt-get clean
