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

if [ -f initsystem.sh ] && [ ! -z "$(diff initsystem.sh /etc/kona/initsystem.sh)" ]; then
  cp initsystem.sh /etc/kona/initsystem-new.sh
fi

if [ ! -z "$(diff initdesktop.sh /etc/kona/initdesktop.sh)" ]; then
  if [ ! -f /etc/kona/initdesktop ]; then
    cp initdesktop.sh /etc/kona
    rm -f /etc/kona/initdesktop-new.sh
  else
    cp initdesktop.sh /etc/kona/initdesktop-new.sh
  fi
  chmod 777 /etc/kona/initdesktop.sh
fi

if [ ! -z "$(diff initdesktop2.sh /etc/kona/initdesktop2.sh)" ]; then
  if [ ! -f /etc/kona/initdesktop2 ]; then
    cp initdesktop2.sh /etc/kona
    rm -f /etc/kona/initdesktop2-new.sh
  else
    cp initdesktop2.sh /etc/kona/initdesktop2-new.sh
  fi
  chmod 777 /etc/kona/initdesktop2.sh
fi

if [ -f /etc/kona/diretta/alsahost_version ]; then
  if  [ ! -z "$(diff diretta/alsahost_version /etc/kona/diretta/alsahost_version)" ]; then
    cp diretta/alsahost_version /etc/kona/diretta
    touch /etc/kona/alsahost-update
    cp diretta/alsa_bridge.c diretta/alsa_bridge.h diretta/Makefile /etc/kona/diretta
    cp update-desktop-bg.sh /etc/kona/update
    rm /etc/kona/diretta/*/alsa_bridge.ko
    touch /etc/kona/diretta_updated
    trace "update-bg - make alsa bridge driver"
  fi
fi

if [ -f /etc/kona/pkgadd.sh ]; then
if [ -z "$(dpkg -l|grep "ii  lutris")" ]; then
  cp update-desktop.sh /etc/kona/update
  trace "update - install lutris"
fi
if [ -z "$(dpkg -l|grep steam-launcher)" ]; then
  cp steam-stable.list /etc/apt/sources.list.d
  cp steam.gpg /usr/share/keyrings
  cp update-desktop.sh /etc/kona/update
  dpkg --add-architecture i386
  trace "update - install steam"
fi
fi

if [ ! -z "$(diff diretta-renderer/renderer_version /etc/kona/diretta-renderer/renderer_version)" ]; then
  rm -fr /etc/kona/diretta-renderer
  cp diretta-renderer /etc/kona -R
  pushd . > /dev/null
  cd /etc/kona/diretta-renderer
  if [ -z "$(lscpu|grep avx)" ]; then
    sed -i -e "s/fastmemcpy-avx.o/fastmemcpy.o/" Makefile
  fi
  make 2> /dev/null > /dev/null
  if [ "$(cat /etc/kona/diretta_renderer_version)" = "1" ]; then
    if [ ! -z "$(pgrep -f DirettaRendererUPnP)" ]; then
      systemctl stop diretta-renderer
    fi
    cp bin/DirettaRendererUPnP /etc/kona/diretta
  fi
  popd > /dev/null
  echo 0 > /etc/kona/renderer_updated
  chmod 777 /etc/kona/renderer_updated
  trace "update - make diretta renderer 1.0"
fi

  if [ ! -z "$(lscpu|grep avx)" ]; then
    trace "update - renderer_version-x(new) = $(cat diretta-renderer-x/renderer_version)"
    trace "update - renderer_version-x(current)=$(cat /etc/kona/diretta-renderer-x/renderer_version)"
    if [ ! -z "$(diff diretta-renderer-x/renderer_version /etc/kona/diretta-renderer-x/renderer_version)" ]; then
      if [ -f /etc/kona/diretta-renderer-x/ffmpeg-headers/libavcodec ]; then
#        mv /etc/kona/diretta-renderer-x/ffmpeg-headers diretta-renderer-x
        rm -fr /etc/kona/diretta-renderer-x
        cp diretta-renderer-x /etc/kona -R
        pushd . > /dev/null
        cd /etc/kona/diretta-renderer-x
	ln -s ../diretta-renderer-2/ffmpeg-headers
#    make FFMPEG_IGNORE_MISMATCH=1 2> /dev/null > /dev/null
        make 2> /dev/null > /dev/null
        if [ ! -z "$(pgrep -f DirettaRendererUPnP-X)" ]; then
          systemctl stop diretta-renderer
        fi
        cp bin/DirettaRendererUPnP /etc/kona/diretta/DirettaRendererUPnP-X
        popd > /dev/null
        echo 1 > /etc/kona/renderer_updated
        chmod 777 /etc/kona/renderer_updated
	trace "update - make diretta renderer-x"
      #else
      #  cp update-desktop.sh /etc/kona/update
      #  trace "update - install ffmpeg-headers(x)"
      fi
    fi
  fi
  trace "update - renderer_version-2(new) = $(cat diretta-renderer-2/renderer_version)"
  trace "update - renderer_version-2(current)=$(cat /etc/kona/diretta-renderer-2/renderer_version)"
  if [ ! -z "$(diff diretta-renderer-2/renderer_version /etc/kona/diretta-renderer-2/renderer_version)" ]; then
    if [ -d /etc/kona/diretta-renderer-2/ffmpeg-headers/libavcodec ]; then
      mv /etc/kona/diretta-renderer-2/ffmpeg-headers diretta-renderer-2
      rm -fr /etc/kona/diretta-renderer-2
      cp diretta-renderer-2 /etc/kona -R
      pushd . > /dev/null
      cd /etc/kona/diretta-renderer-2
      make 2> /dev/null > /dev/null
      if [ "$(cat /etc/kona/diretta_renderer_version)" = "2" ]; then
        if [ ! -z "$(pgrep -f DirettaRendererUPnP)" ]; then
          systemctl stop diretta-renderer
        fi
        cp bin/DirettaRendererUPnP /etc/kona/diretta
      fi
      popd > /dev/null
      echo 0 > /etc/kona/renderer_updated
      chmod 777 /etc/kona/renderer_updated
#    else
#      cp update-desktop-bg.sh /etc/kona/update
#      trace "update - install ffmeg-headers"
    fi
  fi

if [ -d /etc/kona/DirettaHostSDK_148 ]; then
  if [ ! -z "$(diff sdk_version /etc/kona/DirettaHostSDK_148/sdk_version)" ]; then
    cp update-desktop-bg.sh /etc/kona/update
    trace "update-bg - update DirettaHostSDK_148"
  fi
fi
cp sdk_version /etc/kona/diretta

if [ -z "$(dpkg -l|grep libflac-dev)" ] || [ -z "$(dpkg -l|grep libmpg123-dev)" ] || [ -z "$(dpkg -l|grep libvorbis-dev)" ] || [ -z "$(dpkg -l|grep libfdk-aac-dev)" ]; then
  cp update-desktop.sh /etc/kona/update
  trace "update.sh - install libflac-dev"
else
  if [ -d /etc/kona/DirettaHostSDK_148 ]; then
  if [ ! -f /etc/kona/slim2diretta/build/slim2diretta ] || [ ! -z "$(diff slim2diretta/slim2diretta_version /etc/kona/slim2diretta/slim2diretta_version)" ]; then
    rm -fr /etc/kona/slim2diretta
    cp slim2diretta /etc/kona -R
    if [ ! -f /etc/systemd/system/slim2diretta.service ]; then
      cp /etc/kona/slim2diretta/slim2diretta.service /etc/systemd/system
      systemctl daemon-reload
    fi
    pushd . > /dev/null
    mkdir -p /etc/kona/slim2diretta/build
    cd /etc/kona/slim2diretta/build
    cmake .. 2> /dev/null > /dev/null
    make 2> /dev/null > /dev/null
    if [ ! -z "$(pgrep -f slim2diretta)" ]; then
      systemctl stop slim2diretta
    fi
    cp slim2diretta /etc/kona/diretta
    popd > /dev/null
    touch /etc/kona/change_slim2diretta
    chmod 777 /etc/kona/change_slim2diretta
  fi
  fi
fi

if [ ! -f /etc/sysctl.conf ]; then
  echo "DefaultTimeoutStopSec=10s" > /etc/sysctl.conf
fi

#cp autoupdatecheck.sh /etc/kona
cp healthcheck.sh /etc/kona

if [ ! -z "$(grep -i MATE /etc/kona/os-release)" ]; then
  cp mozo-made.desktop /etc/skel/.local/share/applications
  for i in $(ls /home); do
    if [ -f /home/$i/.local/share/applications/mozo-made.desktop ]; then
      cp mozo-made.desktop /home/$i/.local/share/applications
    fi
  done
fi
if [ ! -f /etc/skel/.xinputrc ]; then
  cp _xinputrc /etc/skel/.xinputrc
  for i in $(ls /home); do
    if [ ! -f /home/$i/.xinputrc ]; then
      cp _xinputrc /home/$i/.xinputrc
      chown $i:$i /home/$i/.xinputrc
    fi
  done
fi
if [ ! -f /usr/bin/wineserver ]; then
  ln -s /usr/lib/x86_64-linux-gnu/wine/wineserver /usr/bin/wineserver
fi

if [ ! -d /etc/kona/diretta-renderer-2/ffmpeg-headers/libavcodec ]; then
  cp update-desktop-bg.sh /etc/kona/update
  trace "update - get ffmpeg-headers"
fi

if [ ! -z "$(diff about-konalinux.desktop /etc/skel/Desktop/about-konalinux.desktop)" ]; then
  cp about-konalinux.desktop /etc/skel/Desktop
  for i in $(ls /home); do
    if [ -f /home/$i/Desktop/about-konalinux.desktop ]; then
      cp about-konalinux.desktop /home/$i/Desktop
      chown $i:$i /home/$i/Desktop/about-konalinux.desktop
    fi
  done
fi
if [ ! -z "$(diff トラブルシューティング.txt /etc/skel/Desktop/トラブルシューティング.txt)" ]; then
  cp トラブルシューティング.txt /etc/skel/Desktop
  for i in $(ls /home); do
    if [ -f /home/$i/Desktop/トラブルシューティング.txt ]; then
      cp トラブルシューティング.txt /home/$i/Desktop
      chown $i:$i /home/$i/Desktop/トラブルシューティング.txt
    fi
  done
fi
if [ ! -z "$(diff リリースノート.txt /etc/skel/Desktop/リリースノート.txt)" ]; then
  cp リリースノート.txt /etc/skel/Desktop
  for i in $(ls /home); do
    if [ -f /home/$i/Desktop/リリースノート.txt ]; then
      cp リリースノート.txt /home/$i/Desktop
      chown $i:$i /home/$i/Desktop/リリースノート.txt
    fi
  done
fi

if [ -z "$(dpkg -l|grep cpufrequtils)" ]; then
  cp update-desktop-bg.sh /etc/kona/update
fi

