#!/bin/sh
getnetworkinfo()
{
    CMDLINE=`cat /proc/cmdline`
    for i in $CMDLINE; do
        case $i in
            ip=*) IPINFO=${i#*=};;
        esac
    done
    ipaddr=$(echo $IPINFO | awk -F":" '{print $1}')
    gw=$(echo $IPINFO | awk -F":" '{print $3}')
    nm=$(echo $IPINFO | awk -F":" '{print $4}')
    ifname=$(echo $IPINFO | awk -F":" '{print $6}')
    cat -<< EOF > /hmc/tmp/mfginstallnetw.properties
NETWINTF=$ifname
NETWIPADDR=$ipaddr
NETWNETMASK=$nm
NETWGATEWAY=$gw
EOF

}
arch=$(uname -p)
if [ "$arch" = "x86_64" ]; then
# copy files over from nfs mounted /sysroot
    cp /sysroot/MFG/linuxmig /hmc/root
    cp /sysroot/MFG/mk288.cmd /hmc/root
    cp /sysroot/MFG/mk2882.cmd /hmc/root
    cp /sysroot/MFG/newpkg.tgz /hmc/root
    cp /sysroot/MFG/HmcShip /hmc/root
    cp /sysroot/MFG/enableRemoteAccess /hmc/root
    if [ -f /sysroot/authorized_keys ]; then
        cp /sysroot/authorized_keys /hmc/home/hscroot/.ssh/authorized_keys2
    fi
fi
touch /hmc/opt/hsc/data/.license_accepted
touch /hmc/opt/hsc/data/.machine_license_accepted
touch /hmc/opt/hsc/data/.ssh_enabled
touch /hmc/tmp/.vhmc_license_accepted
touch /hmc/opt/hsc/data/keycfgcheck
touch /hmc/opt/hsc/data/dispcfgcheck
touch /hmc/opt/hsc/data/disable_inband_svcevnt
touch /hmc/opt/hsc/data/nolocalecheck
chroot /hmc chown ccfw:ccfw /tmp/.vhmc_license_accepted
chroot /hmc usermod -s /bin/bash hscroot
chroot /hmc ln -s /usr/bin/sudo /usr/hmcrbin
grep -q "^hscroot" /hmc/etc/sudoers
if [ $? -ne 0 ]; then
    echo "hscroot ALL=(ALL) NOPASSWD: ALL" >> /hmc/etc/sudoers
fi
# CR1 will not use this process as SMC
# requires different flow. Starting with
# CR2 line, IBM MFG use remote access to
# setup post installation
mt=$(/opt/hmc/bin/GetVPD -m)
if [ "$mt" != "7063CR1" ]; then
    # Need to setup network such that it will use same network
    # currently used here
    getnetworkinfo
    # Always use the version obtained from install server
    if [ -f /hmc/dump/enableRemoteAccess ]; then
        cp /hmc/dump/enableRemoteAccess /hmc/root/
    else
        cp /opt/hmc/bin/enableRemoteAccess /hmc/root/
    fi
    chroot /hmc chmod +x /root/enableRemoteAccess
    # Create a systemd service that will disable firewall on port 22
    cat -<< EOF > /hmc/etc/systemd/system/mfgaccess.service
[Unit]
Description=HMC Manufacturing install process
After=ccfw.service

[Service]
Type=simple
ExecStart=/root/enableRemoteAccess
StandardOutput=null

[Install]
WantedBy=multi-user.target
EOF
    chroot /hmc /bin/systemctl enable mfgaccess.service
fi
exit 0
