#!/bin/sh -e
#
# 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist 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 3 of the License, or
# (at your option) any later version.
#
# cdist 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 cdist. If not, see <http://www.gnu.org/licenses/>.
#

device="$(cat "$__object/parameter/device" 2>/dev/null || echo "/$__object_id")"
chroot="$(cat "$__object/parameter/chroot")"

target_os=$(cat "$__object/explorer/target_os")

mkdir "$__object/files"
install_script="$__object/files/install_script"
# Link file descriptor #6 with stdout
exec 6>&1
# Link stdout with $install_script
exec > "$install_script"

# Generate script to install bootloader on distro
printf '#!/bin/sh -l\n'

case "$target_os" in
   ubuntu|debian)
      if [ -s "$__global/explorer/efi" ]; then
         # FIXME: untested. maybe also just run update-grub for EFI system?
         printf 'grub-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
         printf 'mkdir -p /boot/efi/EFI/BOOT\n'
         printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
      else
         printf 'grub-install "%s"\n' "$device"
         printf 'update-grub\n'
      fi
   ;;
   archlinux)
      if [ -s "$__global/explorer/efi" ]; then
         echo "EFI boot loader installation is on your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
         echo "Please contribute an implementation for it if you can." >&2
         exit 1
      else
         printf 'grub-install "%s"\n' "$device"
         # bugfix/workarround: rebuild initramfs
         # FIXME: doesn't belong here
         printf 'mkinitcpio -p linux\n'
         printf 'grub-mkconfig -o /boot/grub/grub.cfg\n'
      fi
   ;;
   centos)
      if [ -s "$__global/explorer/efi" ]; then
         printf 'grub2-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
         printf 'mkdir -p /boot/efi/EFI/BOOT\n'
         printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
      else
         printf 'grub2-install "%s"\n' "$device"
         printf 'grub2-mkconfig --output=/boot/grub2/grub.cfg\n'
      fi
   ;;
   *)
      echo "Your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
      echo "If you can, please contribute an implementation for it." >&2
      exit 1
   ;;
esac
# Restore stdout and close file descriptor #6.
exec 1>&6 6>&-


cat << DONE
# Ensure /tmp exists
[ -d "${chroot}/tmp" ] || mkdir -m 1777 "${chroot}/tmp"
# Generate script to run in chroot
script=\$(mktemp "${chroot}/tmp/${__type##*/}.XXXXXXXXXX")
cat > \$script << script_DONE
$(cat "$install_script")
script_DONE

# Make script executable
chmod +x "\$script"

# Run script in chroot
relative_script="\${script#$chroot}"
chroot "$chroot" "\$relative_script"
rm -rf \$script
DONE
