#!/bin/sh
cd /opt

# include develop config
. ./installer.config

# include functions.sh
. ./functions.sh

# wait for Network
# wait max 100 seconds until something is in /etc/resolv.conf
while ! test -s /etc/resolv.conf
do
  [ "$counter" -ge 100 ] && exit 1

  let "counter+=1"
  echo "waiting " $counter "seconds for network :-)"

  sleep 1
done


# chmod / or tc user can not login, so no tce-load would be possible
chmod 755 /

# install packages required by diskpreperation.sh
installTcPackages bash parted bc ntfs-3g dosfstools

# workaround for limited availability of btrfs-progs and reiserfsprogs
architecture=$(uname -m)
if [ "${architecture}" == "x86_64" ]
  then installTcPackages btrfs-progs
  else installTcPackages reiserfsprogs
fi

# install and set keyboard layout for debugging
installTcPackages kmaps util-linux
loadkmap </usr/share/kmap/qwertz/de-latin1-nodeadkeys.kmap

echo "create target directory"
mkdir /target
chmod 0755 /target

# run install.sh
echo "switch to bash and run installer"
bash ./installer.sh | tee >> /tmp/installer.logfile

mkdir -p /target/etc/ssh/
chmod 0755 /target/etc/ssh/
chown 0:0 /target/etc/ssh/

cp /opt/ansible.pub /target/etc/ssh/authorized_keys
chmod 0644 /target/etc/ssh/authorized_keys
chown 0:0 /target/etc/ssh/authorized_keys

cp /tmp/fstab.target /target/etc/fstab
chmod 0644 /target/etc/fstab
chown 0:0 /target/etc/fstab

sync

echo "installer finished, logfile: /tmp/installer.logfile"
