#!/bin/sh

#######################################################################
#
# argonaut-nfsroot-integration
#
# Copyright (c) 2008 by Cajus Pollmeier <pollmeier@gonicus.de>
# Copyright (C) 2011-2016 FusionDirectory project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#
#######################################################################

msg() {
  echo "nfsroot-hooks/argonaut-nfsroot-integration: $1"
}

msg "Integrating argonaut tools inside the FAI nfsroot"
mkdir -p "${NFSROOT}"/etc/argonaut

if [ -n "$FAI_ARGONAUT" ]; then
  msg "removing argonaut.conf installed by the package"
  rm -Rf "${NFSROOT}"/etc/argonaut/argonaut.conf

  msg "installing $FAI_ARGONAUT into ${NFSROOT}"
  cp "${FAI_ARGONAUT}" "${NFSROOT}${FAI_ARGONAUT}"

  if [ -f "$NFSROOT"/etc/argonaut/argonaut.conf ]; then
    msg "Using the classical argonaut.conf nothing left to do"
  else
    msg "Using a customized argonaut.conf, copying over the classical one"
    mv -v "${NFSROOT}${FAI_ARGONAUT}" "${NFSROOT}"/etc/argonaut/argonaut.conf
  fi
else
  msg "FAI_ARGONAUT not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not work please indicate the file to be copied into FAI_ARGONAUT"
fi

if [ -n "$FAI_CONF" ]; then
  cp "${FAI_CONF}" "${NFSROOT}${FAI_CONF}"
else
  msg "FAI_CONF not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not work please indicate the file to be copied into FAI_CONF"
fi

if [ -n "$FAI_LDAP" ]; then
  msg "installing $FAI_LDAP into ${NFSROOT}"
  cp "${FAI_LDAP}" "${NFSROOT}${FAI_LDAP}"
else
  msg "FAI_LDAP not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not work please indicate the file to be copied into FAI_LDAP"
fi

if [ -n "$FAI_SSL_CERT" ]; then
  msg "installing ssl certificate into nfs root"
  cp "${FAI_SSL_CERT}" "${NFSROOT}"/etc/ssl/certs
else
  msg "FAI_SSL_CERT not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not be able to use ssl certificate with ldap, please indicate the certs to be copied into FAI_SSL_CERT"
fi

if [ -n "$FAI_SSL_CERT_KEY" ]; then
  msg "installing ssl certificate private key into nfs root"
  cp "${FAI_SSL_CERT_KEY}" "${NFSROOT}"/etc/ssl/private
else
  msg "FAI_SSL_CERT_KEY not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not be able to use ssl certifcate with ldap, please indicate the certificate private key to be copied into FAI_SSL_CERT_KEY"
fi

if [ -n "$FAI_SSL_CERT_CA" ]; then
  msg "installing ssl ca certificate into nfs root"
  cp "${FAI_SSL_CERT_CA}" "${NFSROOT}"/etc/ssl/certs
else
  msg "FAI_SSL_CERT_CA not mentionned in /etc/fai/nfsroot.conf"
  msg "installation will not be able to verify ssl certificate with ldap, please indicate the ca certificate to be copied into FAI_SSL_CERT_CA"
fi

# Get newest kernel installed inside the nfsroot
version=$(echo "$NFSROOT"/boot/vmlinuz-* | sort -n | head -n1 | cut -d- -f2-)

# Copy as default kernel (TFTPROOT)
if [ -f "$TFTPROOT"/vmlinuz-"$version" ]; then
  msg "installing vmlinuz-install"

  # Change vmlinuz-install name if declared
  if [ -n "$DISTRELEASE" ]; then
    vmlinuxName=vmlinuz-"$DISTRELEASE"-install
  else
    vmlinuxName=vmlinuz-install
  fi

  cp -f "$TFTPROOT"/vmlinuz-"$version" "$TFTPROOT"/"$vmlinuxName"
else
  msg "ERROR: cannot find kernel to use as vmlinuz-install!!!"
fi

# Copy initrd if present (TFTPROOT)
if [ -f "$TFTPROOT"/initrd.img-"$version" ]; then
  msg "installing initrd.img-install"

  # Change initrd.img-install name if declared
  if [ -n "$DISTRELEASE" ]; then
    initrdName=initrd.img-"$DISTRELEASE"-install
  else
    initrdName=initrd.img-install
  fi

  cp -f "$TFTPROOT"/initrd.img-"$version" "$TFTPROOT"/"$initrdName"
else
  msg "ERROR: cannot find initrd image to use as initrd.img-xx !!!"
fi

# Actions for TFTPROOT_EFI32 if declared
if [ -n "$TFTPROOT_EFI32" ]; then
  # WARNING about linking pxelinux.cfg
  msg "WARNING: not forget to link pxelinux.cfg in $TFTPROOT_EFI32"
  msg "WARNING: for exemple"
  msg "WARNING: cd $TFTPROOT_EFI32 ; ln -s ../bios/pxelinux.cfg $TFTPROOT_EFI32/pxelinux.cfg"
  # Copy as default kernel (TFTPROOT_EFI32)
  if [ -f "$TFTPROOT"/vmlinuz-"$version" ]; then
    msg "installing vmlinuz-install in $TFTPROOT_EFI32"

    # Change vmlinuz-install name if declared
    if [ -n "$DISTRELEASE" ]; then
      vmlinuxName=vmlinuz-"$DISTRELEASE"-install
    else
      vmlinuxName=vmlinuz-install
    fi

    cp -f "$TFTPROOT"/vmlinuz-"$version" "$TFTPROOT_EFI32"/vmlinuz-"$version"
    cp -f "$TFTPROOT"/vmlinuz-"$version" "$TFTPROOT_EFI32"/"$vmlinuxName"
  else
    msg "ERROR: cannot find kernel to use as vmlinuz-install!!!"
  fi

  # Copy initrd if present (TFTPROOT_EFI32)
  if [ -f "$TFTPROOT"/initrd.img-"$version" ]; then
    msg "installing initrd.img-install in $TFTPROOT_EFI32"

    # Change initrd.img-install name if declared
    if [ -n "$DISTRELEASE" ]; then
      initrdName=initrd.img-"$DISTRELEASE"-install
    else
      initrdName=initrd.img-install
    fi

    cp -f "$TFTPROOT"/initrd.img-"$version" "$TFTPROOT_EFI32"/initrd.img-"$version"
    cp -f "$TFTPROOT"/initrd.img-"$version" "$TFTPROOT_EFI32"/"$initrdName"
  else
    msg "ERROR: cannot find initrd image to use as initrd.img-xx !!!"
  fi
fi

# Actions for TFTPROOT_EFI64 if declared
if [ -n "$TFTPROOT_EFI64" ]; then
  # WARNING about linking pxelinux.cfg
  msg "WARNING: not forget to link pxelinux.cfg in $TFTPROOT_EFI64"
  msg "WARNING: for exemple"
  msg "WARNING: cd $TFTPROOT_EFI64 ; ln -s ../bios/pxelinux.cfg $TFTPROOT_EFI64/pxelinux.cfg"
  # Copy as default kernel (TFTPROOT_EFI64)
  if [ -f "$TFTPROOT"/vmlinuz-"$version" ]; then
    msg "installing vmlinuz-install in $TFTPROOT_EFI64"

    # Change vmlinuz-install name if declared
    if [ -n "$DISTRELEASE" ]; then
      vmlinuxName=vmlinuz-"$DISTRELEASE"-install
    else
      vmlinuxName=vmlinuz-install
    fi

    cp -f "$TFTPROOT"/vmlinuz-"$version" "$TFTPROOT_EFI64"/vmlinuz-"$version"
    cp -f "$TFTPROOT"/vmlinuz-"$version" "$TFTPROOT_EFI64"/"$vmlinuxName"
  else
    msg "ERROR: cannot find kernel to use as vmlinuz-install!!!"
  fi

  # Copy initrd if present (TFTPROOT_EFI64)
  if [ -f "$TFTPROOT"/initrd.img-"$version" ]; then
    msg "installing initrd.img-install in $TFTPROOT_EFI64"

    # Change initrd.img-install name if declared
    if [ -n "$DISTRELEASE" ]; then
      initrdName=initrd.img-"$DISTRELEASE"-install
    else
      initrdName=initrd.img-install
    fi

    cp -f "$TFTPROOT"/initrd.img-"$version" "$TFTPROOT_EFI64"/initrd.img-"$version"
    cp -f "$TFTPROOT"/initrd.img-"$version" "$TFTPROOT_EFI64"/"$initrdName"
  else
    msg "ERROR: cannot find initrd image to use as initrd.img-xx !!!"
  fi
fi
