#! /bin/bash

exec >&2

chmod 700 /root/.gnupg

if [ -x /usr/sbin/haveged ] ; then
  /usr/sbin/haveged -w 1024 -v 0

  # Adjust the OOM killer score so kernel prefers killing the haveged
  # daemon instead of the YaST installer (or it's child).
  # The entropy will be collected by kernel anyway, just slower without haveged.
  if [ -f /var/run/haveged.pid ] ; then
    haveged_pid=`cat /var/run/haveged.pid`
    echo 1000 > /proc/$haveged_pid/oom_score_adj
  fi
fi

if [ -d /usr/lib/rpm/gnupg/keys ] ; then
  touch /installkey.gpg
  gpg --batch --homedir /root/.gnupg --no-default-keyring --ignore-time-conflict --ignore-valid-from --keyring /installkey.gpg --import /usr/lib/rpm/gnupg/keys/*
  mkdir -p /pubkeys
  for i in /usr/lib/rpm/gnupg/keys/*.asc ; do
    [ -f "$i" ] && ln -s "$i" "/pubkeys/${i##*/}.key"
  done
elif [ -f /usr/lib/rpm/gnupg/suse-build-key.gpg ] ; then
  cp /usr/lib/rpm/gnupg/suse-build-key.gpg /installkey.gpg
fi

if [ ! -s /installkey.gpg ] ; then
  echo "warning: no build keys!"
fi

if [ -d /sys/firmware/efi ]; then
  mount -t efivarfs none /sys/firmware/efi/efivars
fi

# needed for nfs
echo "Mounting rpc_pipefs on /var/lib/nfs/rpc_pipefs"
mount -t rpc_pipefs rpc_pipefs /var/lib/nfs/rpc_pipefs

if [ -x /usr/sbin/rpc.idmapd ] ; then
  echo "Starting rpc.idmapd"
  /usr/sbin/rpc.idmapd < /dev/null
fi

if [ -x /bin/dbus-daemon ] ; then
  echo "Starting dbus-daemon"
  /bin/dbus-uuidgen --ensure
  /bin/dbus-daemon --system
fi

if [ -x /usr/sbin/wpa_supplicant ] ; then
  /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -u -B -f /var/log/wpa_supplicant.log
fi

if [ -x usr/sbin/wickedd ] ; then
  debug_opts="--debug mini --log-target stderr:time,pid,ident"
  if [ -n "$linuxrc_debug" ] ; then
    if [ "$linuxrc_debug" = 1 ] ; then
      debug_opts="--debug most --log-target stderr:time,pid,ident"
    else
      debug_opts="--debug all --log-target stderr:time,pid,ident"
    fi
  fi
  echo "Starting wicked"
  {
    /usr/sbin/wickedd $debug_opts
    for i in /usr/lib/wicked/bin/wickedd-* ; do $i $debug_opts ; done
    /usr/sbin/wickedd-nanny $debug_opts
  } 2>/var/log/wickedd.log
fi

# gather list of ibft interfaces for linuxrc
ibft=
while read ifname xxx ; do
  [ -z "$ifname" ] && continue
  [ -n "$ibft" ] && ibft="$ibft",
  ibft="$ibft$ifname"
done < <(/etc/wicked/extensions/ibft -l)
echo "ibftdevices: $ibft" >/etc/ibft_devices

# log also to console 4 if it exists
if [ -c /dev/tty4 ] ; then
  echo "*.*	/dev/tty4" >>/etc/syslog.conf
fi

