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

trace "autoupdatecheck in"

if [ -f /etc/kona/ec ]; then
  ec=$(cat /etc/kona/ec)
  errormsg $ec
  systemctl reboot
fi

/etc/kona/startserver.sh

if [ ! -f /etc/kona/mpdstart ]; then
  if [ -f /var/run/mpd/pid ]; then
    kill $(cat /var/run/mpd/pid)
  fi
else
  ss="$(cat /etc/kona/soundsystem)"
  if [ "$ss" = "pipewire" ]; then
    ss="pulse"
  fi
  if [ ! -f /etc/kona/mpdalsa ] && [ -z "$(grep -v '#' ~/.config/mpd/mpd.conf | grep type | grep $ss)" ]; then
    trace "mpd config is incorrect - $ss"
    ls -la ~/.config/mpd >> /etc/kona/log/trace.log
    cp ~/.config/mpd/mpd.conf  /etc/kona/log
    rm ~/.config/mpd/mpd.conf
    cp /etc/skel/.config/mpd/mpd_$ss.conf ~/.config/mpd
    ln -s ~/.config/mpd/mpd_$ss.conf ~/.config/mpd/mpd.conf
    trace mpd_$ss.conf
    sync
#    em 300
#    systemctl reboot
    systemctl --user stop mpd mpd.socket
  fi 
  if [ -z "$(pgrep mpd)" ]; then
    systemctl --user start mpd
    sleep 3
    if [ -z "$(pgrep mpd)" ]; then
      systemctl --user status mpd > /etc/kona/log/mpd.log
      zenity --warning --title="MPD起動エラー" --text="MPDを起動できません\n起動するたび起きるときは\n/etc/kona/log/mpd.logファイルを添えて作者にメールするか\n会議室で報告してください" --no-wrap
    else
      trace "mpd is started"
    fi
  fi
#  systemctl --user restart mpd
#  trace "Restart MPD"
fi

if [ -f /etc/kona/Squeeze2UPnP ] && [ -z "$(pgrep -f squeeze2upnp-linux-x86_64-static)" ]; then
  pushd . > /dev/null
  cd /etc/kona/squeeze2upnp
  ./squeeze2upnp-linux-x86_64-static -z -c config.xml
  popd
fi

if [ -f /etc/kona/spotify ] && [ -z "$(pgrep librespot)" ]; then
  if [ ! -f /etc/kona/hqspotify ]; then
    librespot -n "klue7-spotify($HOSTNAME)" &
  else
    cp /usr/bin/librespot $tmp
    $tmp/librespot -n "klue7-spotify($HOSTNAME)" &
  fi
  trace "$tmp spotify-start"
fi

if [ "$(cat /etc/kona/soundsystem)" = "jack" ]; then
  while [ -z "$(pgrep jackd)" ]; do
    sleep 2
  done
fi
touch /etc/kona/audioready

crontab -l > /etc/kona/updatecheck2.conf
if [ -z "$(grep updatecheck.sh /etc/kona/updatecheck2.conf)" ]; then
  cp /etc/kona/updatecheck.conf /etc/kona/updatecheck2.conf
fi
crontab /etc/kona/updatecheck2.conf

/etc/kona/desktopcheck.sh

cd ~
rm -f connecting
if [ ! -z "$(ip route)" ]; then
  wget -q -t 2 --timeout=30 "https://simosnet.com/debian/packages/connecting"
fi
if [ -f connecting ]; then

  if [ ! -f /etc/kona/soundrivers/$(uname -r)/snd-usb-audio.ko-avx.tgz ]; then
    rm -f snd-usb-audio.ko_$(uname -r).tgz
    wget -q --timeout=30 https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/soundrivers/snd-usb-audio.ko_$(uname -r).tgz
    if [ -f snd-usb-audio.ko_$(uname -r).tgz ]; then
      mkdir -p /etc/kona/soundrivers/$(uname -r)
      tar xfz snd-usb-audio.ko_$(uname -r).tgz -C /etc/kona/soundrivers/$(uname -r)
    fi
    rm -f snd-usb-audio.ko_$(uname -r).tgz*
  fi

  if [ ! -f /etc/kona/soundrivers/$(uname -r)/xhci-hcd.ko-avx.tgz ]; then
    rm -f xhci-hcd.ko_$(uname -r).tgz
    wget -q --timeout=30 https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/soundrivers/xhci-hcd.ko_$(uname -r).tgz
    if [ -f xhci-hcd.ko_$(uname -r).tgz ]; then
      mkdir -p /etc/kona/soundrivers/$(uname -r)
      tar xfz xhci-hcd.ko_$(uname -r).tgz -C /etc/kona/soundrivers/$(uname -r)
    fi
    rm -f xhci-hcd.ko_$(uname -r).tgz*
  fi

  if [ ! -f /etc/kona/soundrivers/$(uname -r)/snd-pcm.ko-avx.tgz ]; then
    rm -f snd-pcm.ko_$(uname -r).tgz
    wget -q --timeout=30 https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/soundrivers/snd-pcm.ko_$(uname -r).tgz
    if [ -f snd-pcm.ko_$(uname -r).tgz ]; then
      mkdir -p /etc/kona/soundrivers/$(uname -r)
      tar xfz snd-pcm.ko_$(uname -r).tgz -C /etc/kona/soundrivers/$(uname -r)
      if [ ! -f /etc/kona/headless ]; then
        if [ -f /etc/kona/fastmemcpy ] || [ -f /etc/kona/fastmemcpy2 ]; then
          notify-send "高音質サウンドドライバーの準備ができました" "再起動するとシステムに組み込みます" -i "audio-speakers"
        fi
      fi
    fi
    rm -f snd-pcm.ko_$(uname -r).tgz*
  fi
  if [ ! -f /etc/kona/soundrivers/$(uname -r)/snd.ko-avx.tgz ]; then
    rm -f snd.ko_$(uname -r).tgz
    wget -q --timeout=30 https://simosnet.com/debian/packages/Konalinux/$kd/update/$kv/soundrivers/snd.ko_$(uname -r).tgz
    if [ -f snd.ko_$(uname -r).tgz ]; then
      mkdir -p /etc/kona/soundrivers/$(uname -r)
      tar xfz snd.ko_$(uname -r).tgz -C /etc/kona/soundrivers/$(uname -r)
    fi
    rm -f snd.ko_$(uname -r).tgz*
  fi

#  rm connecting
fi

if [ -f /etc/kona/headless ]; then
  touch /etc/kona/updatecheck
  exit
fi

soundsystem="$(cat /etc/kona/soundsystem)"
accel="$(cat /etc/kona/accelator)"
if [ "$(cat /etc/kona/janru)" != "none" ]; then
  janru=$(cat /etc/kona/janru)
fi
reso="$(cat /etc/kona/timeslice)"
slice=$((100 / $reso))
if [ -f /etc/kona/mpdstart ]; then
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    mpd="MPD : 起動中\n　"
  else
    mpd="MPD : running\n　"
  fi
else
  mpd=""
fi
if [ -f /etc/kona/lms ]; then
  if [ -f /etc/kona/squeezelite ]; then
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      lms="LMS : サーバー + レンダラー起動中\n　"
    else
      lms="LMS : server + renderer is running\n　"
    fi
  else
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      lms="LMS : サーバー起動中\n　"
    else
      lms="LMS : server is running\n　"
    fi
  fi
else
  if [ -f /etc/kona/squeezelite ]; then
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      lms="LMS : レンダラー起動中\n　"
    else
      lms="LMS : renderer is running\n　"
    fi
  else
    lms=""
  fi
fi
mode=$(cat /etc/kona/soundmode)
if [ -f /etc/kona/fastmemcpy ] || [ -f /etc/kona/fastmemcpy2 ] || [ -f /etc/kona/autohqsd ]; then
  if [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd-pcm.ko.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd-pcm.ko.zst.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd-pcm.ko.xz.org ]; then
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      hqsoundriver="高音質サウンドドライバー：有効"
    else
      hqsoundriver="High-quality Sound driver：active"
    fi
  fi
  if [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd.ko.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd.ko.zst.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd.ko.xz.org ]; then
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      hqsoundriver="高音質サウンドドライバー(レベル-2）：有効"
    else
      hqsoundriver="High-quality Sound driver(level-2)：active"
    fi
  fi
fi
if [ -f /etc/kona/hqusbaudio ]; then
  if [ -f /lib/modules/$(uname -r)/kernel/sound/usb/snd-usb-audio.ko.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/usb/snd-usb-audio.ko.zst.org ] || [ -f /lib/modules/$(uname -r)/kernel/sound/core/snd-usb-audio.ko.xz.org ]; then
    if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
      if [ ! -z "$hqsoundriver" ]; then
        hqusbdriver="\n　USB Audio高音質ドライバー：有効"
      else
        hqusbdriver="USB Audio高音質ドライバー：有効"
      fi
    else
      if [ ! -z "$hqsoundriver" ]; then
        hqusbdriver="\n　USB Audio high-quality sound driver：active"
      else
        hqusbdriver="USB Audio high-quality sound driver：active"
      fi
    fi
  fi
fi
if [ -f /etc/kona/hsusb ]; then
  if [ -f /lib/modules/$(uname -r)/kernel/drivers/usb/host/xhci-hcd.ko.org ] || [ -f /lib/modules/$(uname -r)/kernel/drivers/usb/host/xhci-hcd.ko.zst.org ]; then
    if [ ! -z "$hqusbdriver" ] || [ ! -z "$hqsoundriver" ]; then
      hsusbdriver="\n　USB 高速ドライバー：有効"
    else
      hsusbdriver="USB 高速ドライバー：有効"
    fi
  fi
fi
if [  -f /etc/kona/preset ]; then
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    preset="プリセット：$(cat /etc/kona/preset)"
  else
    if [ "$(cat /etc/kona/preset)" = "デフォルト" ]; then
      preset="Preset：default"
    else
      preset="Preset：$(cat /etc/kona/preset)"
    fi
  fi
fi
#sleep 3
audioicon="audio-speakers"
if [ ! -z "$(grep -i gnome /etc/kona/os-release)" ]; then
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    notify-send "オーディオシステムの準備ができました" "このメッセージが出てからオーディオを使い始めてください\n\n　サウンド・システム：$soundsystem\n　ジャンル：$janru\n　モード : $mode\n　$hqsoundriver\n" -i "$audioicon"
  else
    notify-send "Audio System is ready" "Start using the audio after this message appears.\n\n　Sound System：$soundsystem\n　Janru：$janru\n　Mode : $mode\n　$hqsoundriver\n" -i "$audioicon"
  fi
else
  kernel="$(uname -r)"
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    notify-send "オーディオシステムの準備ができました" "このメッセージが出てからオーディオを使い始めてください\n\n　カーネル：$kernel\n　サウンド・システム：$soundsystem\n　ジャンル：$janru\n　モード : $mode\n　$mpd$lms$hqsoundriver $hqusbdriver $hsusbdriver\n　$preset" -i "$audioicon"
  else
    notify-send "Audio System is ready" "Please start using the audio after this message appears.\n\n　Kernel：$kernel\n　Sound System：$soundsystem\n　Janru：$janru\n　Mode : $mode\n　$mpd$lms$hqsoundriver $hqusbdriver $hsusbdriver\n　$preset" -i "$audioicon"
  fi
fi
if [ -f /etc/kona/fastsoundriver ]; then
  rm -f /etc/kona/fastsoundriver
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    zenity --warning --title="高音質サウンドドライバー" --text="高音質サウンドドライバーを組み込みました\n再起動すると有効になります\n\n高音質サウンドドライバーを使うには\nセキュアブートをオフにしてください" --no-wrap
  else
    zenity --warning --title="High-quality Sound driver" --text="High-quality sound driver is installed.\nIt will be active after a restart.\n\nTo use the high-quality sound driver disable Secure Boot." --no-wrap
  fi
fi
if [ -f /etc/kona/hqusbaudiodriver ]; then
  rm -f /etc/kona/hqusbaudiodriver
  if [ "$LANG" = "ja_JP.UTF-8" ] || [ "$LANG" = "ja_JP.utf8" ]; then
    zenity --warning --title="USB Audio高音質ドライバー" --text="USB Audio高音質ドライバーを組み込みました\n再起動すると有効になります\n\n高音質サウンドドライバーを使うには\nセキュアブートをオフにしてください" --no-wrap
  else
    zenity --warning --title="USB Audio high-quality sound driver" --text="USB Audio high-quality sound driver is installed\nIt will be active after a restart\n\nTo using USB Audio high-quality sound driver disable Secure boot" --no-wrap
  fi
fi
if [ -f /etc/kona/hsusbdriver ]; then
  rm -f /etc/kona/hsusbdriver
  zenity --warning --title="USB高速ドライバー" --text="USB高速ドライバーを組み込みました\n再起動すると有効になります\n\n高速サウンドドライバーを使うには\nセキュアブートをオフにしてください" --no-wrap
fi

if [ ! -f /etc/kona/grubadderror ] && [ ! -d /cdrom/casper ]; then
  if [ -d /sys/firmware/efi ] && [ ! -d /boot/efi/EFI/Debian ]; then
    zenity --question --title="GRUB登録失敗" --text="DebianのEFIレコードがありません\nインストール時にGRUB関連のエラーが出ませんでしたか？\nGRUBの登録をスキップしたのでないならインストールしなおしてください\nEFIパーティションのマウントポイントは/boot/efiです\nなおDebian以外のブートレコードを使っているときは問題ありません\n\n今後このメッセージは表示しない" --no-wrap 
    if [ $? = "0" ]; then
      touch /etc/kona/grubadderror
    fi
  fi
fi

if [ -f /etc/kona/change_alsabridge ]; then
  trace "wait for change_alsabridge end"
  while [ -f /etc/kona/change_alsabridge ]; do
    sleep 2
  done
fi
if [ -z "$(systemctl list-unit-files|grep diretta_bridge_driver|grep disable)" ]; then
  if [ -f /etc/kona/diretta_updated ] || [ ! -z "$(systemctl status diretta_sync_host | grep "active (running)")" ] || [ ! -z "$(systemctl status diretta_sync_host2 | grep "active (running)")" ]; then
    if [ ! -f /etc/kona/headless ]; then
      if [ -f /etc/kona/diretta_updated ]; then
        notify-send "diretta host is updated → $(cat /etc/kona/diretta/alsahost_version)" -i $audioicon
	touch /etc/kona/change_alsabridge
        trace "autoupdatecheck1 - set change_alsabridge"
	rm /etc/kona/diretta_updated
      else
        notify-send "diretta host($(cat /etc/kona/diretta/alsahost_version)) is ready !" -i $audioicon
      fi
    else
      if [ -f /etc/kona/diretta_updated ]; then
	wall "diretta host is updated → $(cat /etc/kona/diretta/alsahst_version)"
	touch /etc/kona/change_alsabridge
	rm /etc/kona/diretta_updated
        trace "autoupdatecheck2 - set change_alsabridge"
      else
        wall "diretta host($(cat /etc/kona/diretta/alsahost_version)) is ready !"
      fi
    fi
  fi
fi
if [ -z "$(systemctl list-unit-files|grep diretta-renderer.service|grep disable)" ]; then
  if [ ! -z "$(grep ^HQ=\"1\" /etc/kona/diretta/diretta-renderer.conf)" ]; then
    if [ ! -z "$(grep ^FORK=\"1\" /etc/kona/diretta/diretta-renderer.conf)" ]; then
      diretta_renderer="DirettaRendererUPnP-X"
      diretta_renderer_name="diretta renderer fork-hq"
      renderer_version="$(cat /etc/kona/diretta-renderer-x/renderer_version)"
    else
      diretta_renderer="DirettaRendererUPnP"
      diretta_renderer_name="diretta renderer hq"
      if [ -f /etc/kona/diretta-renderer-2/renderer_version ]; then
        renderer_version="$(cat /etc/kona/diretta-renderer-2/renderer_version)"
      else
        renderer_version="$(cat /etc/kona/diretta-renderer/renderer_version)"
      fi
    fi
  elif [ ! -z "$(grep ^FORK=\"1\" /etc/kona/diretta/diretta-renderer.conf)" ]; then
    diretta_renderer="DirettaRendererUPnP-X"
    diretta_renderer_name="diretta renderer fork"
    renderer_version="$(cat /etc/kona/diretta-renderer-x/renderer_version)"
  else
    diretta_renderer="DirettaRendererUPnP"
    diretta_renderer_name="diretta renderer"
    if [ "$(cat /etc/kona/diretta_renderer_version)" = "2" ]; then
      renderer_version="$(cat /etc/kona/diretta-renderer-2/renderer_version)"
    else
      renderer_version="$(cat /etc/kona/diretta-renderer/renderer_version)"
    fi
  fi
  if [ -z "$(pgrep -f $diretta_renderer)" ]; then
    sleep 4
  fi
  if [ -z "$(pgrep -f $diretta_renderer)" ]; then
    sleep 4
  fi
  if [ ! -z "$(pgrep -f $diretta_renderer)" ] || [ -f /etc/kona/renderer_updated ]; then
    if [ ! -f /etc/kona/headless ]; then
      if [ -f /etc/kona/renderer_updated ]; then
	if [ "$(cat /etc/kona/renderer_updated)" = "1" ]; then
	  notify-send "diretta renderer(fork) is updated →  $(cat /etc/kona/diretta-renderer-x/renderer_version)" -i $audioicon
	else
	  if [ "$(cat /etc/kona/diretta_renderer_version)" = "2" ]; then
	    notify-send "diretta renderer is updated →  $(cat /etc/kona/diretta-renderer-2/renderer_version)" -i $audioicon
	  else
	    notify-send "diretta renderer is updated →  $(cat /etc/kona/diretta-renderer/renderer_version)" -i $audioicon
	  fi
	fi
	if [ -z "$(systemctl list-unit-files|grep diretta-renderer.service|grep disable)" ] && [ -z "$(pgrep -f DirettaRendererUPnP)" ]; then
	  touch /etc/kona/change_direttarenderer
	fi
	rm /etc/kona/renderer_updated
      else
	if [ ! -z "$renderer_version" ]; then
          notify-send "$diretta_renderer_name($renderer_version) is ready !" -i $audioicon
	else
          notify-send "$diretta_renderer_name is ready !" -i $audioicon
	fi
      fi
    else
      if [ -f /etc/kona/renderer_updated ]; then
	if [ "$(cat /etc/kona/renderer_updated)" = "1" ]; then
	  wall "diretta renderer(fork) is updated → $(cat /etc/kona/diretta-renderer-x/renderer_version)"
        else
	  if [ "$(cat /etc/kona/diretta_renderer_version)" = "2" ]; then
	    wall "diretta renderer is updated → $(cat /etc/kona/diretta-renderer-2/renderer_version)"
	  else
	    wall "diretta renderer is updated → $(cat /etc/kona/diretta-renderer/renderer_version)"
	  fi
	fi
	if [ -z "$(systemctl list-unit-files|grep diretta-renderer.service|grep disable)" ] && [ -z "$(pgrep -f DirettaRendererUPnP)" ]; then
	  touch /etc/kona/change_direttarenderer
	fi
	rm /etc/kona/renderer_updated
      else
	if [ ! -z "$renderer_version" ]; then
          wall "$diretta_renderer_name($renderer_version) is ready !"
	else
          wall "$diretta_renderer_name is ready !"
	fi
      fi
    fi
  else
    if [ ! -z "$(/etc/kona/diretta/DirettaRendererUPnP --list-targets|grep "No Diretta targets found")" ]; then
      if [ ! -f /etc/kona/headless ]; then
        notify-send "No Diretta targets found !" -i $audioicon
      else
        wall "No Diretta targets found !"
      fi
      touch /etc/kona/change_direttarenderer
    fi
  fi
fi
#if [ -z "$(systemctl list-unit-files|grep squeeze2diretta|grep disable)" ]; then
#  if [ ! -z "$(pgrep squeeze2diretta)" ] || [ -f /etc/kona/change_squeeze2diretta ]; then
#    if [ ! -f /etc/kona/headless ]; then
#      if [ -f /etc/kona/change_squeeze2diretta ]; then
#        notify-send "Squeeze2Diretta is updated → $(cat /etc/kona/squeeze2diretta/squeeze2diretta_version)" -i $audioicon
#      else
#        notify-send "Squeeze2Diretta($(cat /etc/kona/squeeze2diretta/squeeze2diretta_version)) is ready !" -i $audioicon
#      fi
#    else
#      if [ -f /etc/kona/change_squeeze2diretta ]; then
#        wall "Squeeze2Diretta is updated → $(/etc/kona/squeeze2diretta/squeeze2diretta_version)"
#      else
#        wall "Squeeze2Diretta($(/etc/kona/squeeze2diretta/squeeze2diretta_version)) is ready !"
#      fi
#    fi
#  fi
#fi  
if [ -z "$(systemctl list-unit-files|grep slim2diretta|grep disable)" ]; then
  if [ ! -z "$(pgrep slim2diretta)" ] || [ -f /etc/kona/change_slim2diretta ]; then
    if [ ! -f /etc/kona/headless ]; then
      if [ -f /etc/kona/change_slim2diretta ]; then
        notify-send "Slim2Diretta is updated → $(cat /etc/kona/slim2diretta/slim2diretta_version)" -i $audioicon
      else
        notify-send "Slim2Diretta($(cat /etc/kona/slim2diretta/slim2diretta_version)) is ready !" -i $audioicon
      fi
    else
      if [ -f /etc/kona/change_slim2diretta ]; then
        wall "Slim2Diretta is updated → $(/etc/kona/slim2diretta/slim2diretta_version)"
      else
        wall "Slim2Diretta($(/etc/kona/slim2diretta/slim2diretta_version)) is ready !"
      fi
    fi
  else
    if [ ! -z "$(/etc/kona/diretta/slim2diretta --list-targets|grep "No Diretta targets found")" ]; then
      if [ ! -f /etc/kona/headless ]; then
	 notify-send "No Diretta targets found !" -i $audioicon
      else
        wall "No Diretta targets found !"
      fi
      touch /etc/kona/change_slim2diretta
    fi
  fi
fi  

/etc/kona/checkhqskernel.sh

touch /etc/kona/updatecheck
trace "autoupdatecheck out"
