#!/bin/bash
. /etc/kona/soundsystem.inc
touch /etc/kona/sysiniting
trace initsystem-in
[ -f /etc/kona/log/trace.log ] && chmod 777 /etc/kona/log/trace.log
if [ -f /usr/bin/mpd ]; then
  mkdir -p /run/mpd
  chown mpd:audio /run/mpd
  chmod 775 /run/mpd
  mkdir -p /var/log/mpd
  chown mpd:audio /var/log/mpd
  chmod 775 /var/log/mpd
  chown mpd:audio /var/lib/mpd
  chmod 775 /var/lib/mpd
  [ -d /var/lib/mpd/playlists ] && chmod 775 /var/lib/mpd/playlists
fi
if [ ! -f /etc/kona/initsystem ]; then
#  if ! grep -q ^net.ipv6.conf.all.disable_ipv6 /etc/sysctl.conf ; then
#    echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
#    echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
#  fi
  if [ ! -z "$(grep DefaultTimeoutStopSec= /etc/sysctl.conf)" ]; then
    sed -i -e "s/DefaultTimeoutStopSec=90s/DefaultTimeoutStopSec=10s/"  /etc/systemd/system.conf
    sed -i -e "s/#DefaultTimeoutStopSec=10s/DefaultTimeoutStopSec=10s/"  /etc/systemd/system.conf
  else
    echo "DefaultTimeoutStopSec=10s" >> /etc/sysctl.conf
  fi
  mkdir -p $tmp
  chmod 777 $tmp
#  mkdir -p /var/log/mpd
#  chown mpd:audio /var/log/mpd
#  chmod 775 /var/log/mpd
#  chown mpd:audio /var/lib/mpd/music
#  chmod 775 /var/lib/mpd/music
  if ! grep -q kona /etc/fstab ; then
    echo "tmpfs $tmp tmpfs defaults 0 0" >> /etc/fstab
    mount -a
  fi  
  if ! grep -q net.ifnames /etc/default/grub ; then
     sed -i -e "s/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0\"/" /etc/default/grub
  fi
  cp /etc/default/grub /etc/kona
  systemctl disable upmpdcli.service 2> /dev/null
  systemctl stop upmpdcli.service 2> /dev/null
  systemctl disable xrdp xrdp-sesman 2> /dev/null
  systemctl stop xrdp xrdp-sesman 2> /dev/null

  if [ ! -d /cdrom/casper ]; then
    user="$(ls /home)"
    autouser="$(grep ^User= /etc/sddm.conf|cut -b6-)"
    if [ ! -z "$autouser" ] && [ "$autouser" != "$user" ]; then
      sed -i -e "s/User=$autouser/User=/" /etc/sddm.conf
    fi
    adduser $user audio 
    cp /etc/kona/sources.list.d/* /etc/apt/sources.list.d
    rm -f /etc/apt/sources.list
  else
    systemctl stop cron
  fi

  dpkg-reconfigure xrdp 2> /dev/null

  mkdir -p /home/bubbleupnp
  chown bubleupnp:bubbleupnp /home/bubbleupnp

  chage -E -1 sddm

  touch /etc/kona/initsystem
fi

trace initsystem-1
if [ -f /usr/local/etc/kona ]; then
  rm -f /usr/local/etc/kona
fi
mkdir -p /usr/local/etc/kona
if [ ! -f /etc/kona/version ]; then
  em 10
fi

# auto update is executed
/etc/kona/autoupdate.sh

if [ ! -f /usr/local/etc/kona/konalinux ]; then
  cp /etc/kona/os-release /usr/local/etc/kona/konalinux
fi
if [ ! -f /etc/kona/os-release ] || [ ! -z "$(diff /etc/kona/os-release /usr/local/etc/kona/konalinux)" ]; then
  em 11
fi

trace initsystem-2
/etc/kona/healthcheck.sh

trace initsystem-3
/etc/kona/soundsystem.sh

trace initsystem-4

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

/etc/kona/desktopcheck.sh

trace initsystem-out
rm -f /etc/kona/sysiniting

if [ ! -z "$(rfkill|grep wlan|grep " blocked")" ]; then
  rfkill unblock wlan
fi

touch /etc/kona/initready
