#!/bin/sh

for directory in /boot /boot/efi /config; do

  # check for enabling ssh daemon
  if test -e $directory/sshd; then
    systemctl enable sshd && \
    systemctl start sshd && \
    mv $directory/sshd $directory/sshd.done
  fi
  
  # setup network (for wicked)
  for i in $directory/ifcfg-*; do
    [ -e "$i" ] || continue
    install -m 0644 "$i" /etc/sysconfig/network/
    RESTART_NETWORK=true
  done
  
  # setup wireless if wpa_supplicant.conf got prepared
  if test -e $directory/wpa_supplicant.conf; then
    mkdir -p /etc/wpa_supplicant/ && \
    cp $directory/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf && \
    mv $directory/wpa_supplicant.conf $directory/wpa_supplicant.conf.done
    cat > /etc/sysconfig/network/ifcfg-wlan0 <<EOF
BOOTPROTO='dhcp'
MTU=''
REMOTE_IPADDR=''
STARTMODE='onboot'
EOF
    RESTART_NETWORK=true
  fi
  
  # extract random files if the user insisted
  if test -e $directory/root.tar.gz; then
    tar xf -C / $directory/root.tar.gz && \
    rm -f $directory/root.tar.gz
  fi
done

# in case we got executed after network got setup already
if [ -n "$RESTART_NETWORK" ]; then
  systemctl try-restart network
fi
  
