#! /bin/sh

# ensure some trees are completely writable
for i in /etc /media /root /run /var ; do
  cp -aL $i ${i}_tmp
  rm -rf $i
  mv ${i}_tmp $i
  ln -snf /proc/self/mounts /etc/mtab
done

if [ -f /mounts/initrd/etc/suse-blinux.conf ] ; then
  grep -q '^brlname=none' /mounts/initrd/etc/suse-blinux.conf || {
    cp /mounts/initrd/etc/suse-blinux.conf /etc
    insserv brld
    insserv sbl
  }
fi

if [ -e /usr/lib/systemd/system/sshd.service -a "$SSH" = 1 ] ; then
  cp -f /usr/lib/systemd/system/sshd.service /etc/systemd/system
  ln -s ../sshd.service /etc/systemd/system/multi-user.target.wants
fi

if [ -n "$SSHPASSWORDENC" ] ; then
  echo "root:$SSHPASSWORDENC" | chpasswd -e
elif [ -n "$SSHPASSWORD" ] ; then
  echo "root:$SSHPASSWORD" | chpasswd
fi

# keep some initrd config files for rescue system
cd /mounts/initrd
for i in etc/modprobe.d/blacklist etc/modprobe.d/noload etc/resolv.conf etc/sysconfig/network/* root/.ssh ; do
  [ -f "$i" ] && cp -f "$i" "/$i"
  [ -d "$i" ] && cp -fr --parents "$i" /
done
cd /

# create machine id
a=`dd if=/dev/urandom bs=1 count=10 2>/dev/null | md5sum` ; echo ${a%% *} >/etc/machine-id

# preserve hostname
hostname >/etc/hostname

# bash >/dev/console 2>&1

rm -f /mounts/initrd/{*,.*}
rmdir /mounts/initrd/* 2>/dev/null
rm -rf /mounts/initrd/{bin,download,etc,lbin,lib,modules,oldroot,root,sbin,scripts,tmp,usr,var}

