#! /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
    # latest wicked moved to /usr/libexec (bsc#1174957), support both locations
    w_dir=/usr/libexec/wicked
    [ -d $w_dir ] || w_dir=/usr/lib/wicked
    for i in $w_dir/bin/wickedd-* ; do $i $debug_opts ; done
    /usr/sbin/wickedd-nanny $debug_opts
  } 2>/var/log/wickedd.log
fi

# New wicked: gather list of firmware interfaces for linuxrc.
#
# Parse lines like: 'fw_type interface_1 interface_2 ...' and store a
# comma-separated list of all interfaces and types in:
#
# /etc/firmware_devices (interface list)
# /etc/firmware_types (type list)
#
fw_types=
fw_interfaces=
while read -a fw ; do
  fw_types="$fw_types,${fw[0]}"
  unset fw[0]
  for i in ${fw[@]} ; do
    fw_interfaces="$fw_interfaces,$i"
  done
done < <(wicked firmware interfaces 2>/dev/null)

fw_types=${fw_types#,}
fw_interfaces=${fw_interfaces#,}

if [ -n "$fw_interfaces" ] ; then
  echo "firmwaredevices: $fw_interfaces" >/etc/firmware_devices
fi

if [ -n "$fw_types" ] ; then
  echo "firmwaretypes: $fw_types" >/etc/firmware_types
fi

if [ ! -f /etc/firmware_devices ] ; then
  # Old wicked: 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
fi
