#! /bin/sh
set -e

# Copyright (C) 2016-2020 The Debian Live team
# Copyright (C) 2010, 2011 Canonical Ltd.
# Author: Colin Watson <cjwatson@ubuntu.com>
#
# 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, 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, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

# Copy GRUB modules.

# TODO: take the modules list as parameter, and only include the needed modules, using moddeps.lst to get dependencies

if [ -z "$1" ] || [ -z "$2" ]; then
	echo "usage: $0 OUTPUT-DIRECTORY GRUB-PLATFORM"
	exit 1
fi

outdir="$1"
platform="$2"

# Copy over GRUB modules, except for those already built in.
cp -a "/usr/lib/grub/$platform"/*.lst "$outdir/boot/grub/$platform/"
for x in "/usr/lib/grub/$platform"/*.mod; do
	# TODO: Some of these exclusions are based on knowledge of module
	# dependencies.  It would be nice to have a way to read the module
	# list directly out of the image.
	case $(basename "$x" .mod) in
		configfile|fshelp|iso9660|memdisk|search|search_fs_file|search_fs_uuid|search_label|tar)
			# included in boot image
			;;
		affs|afs|afs_be|befs|befs_be|minix|nilfs2|sfs|zfs|zfsinfo)
			# unnecessary filesystem modules
			;;
		example_functional_test|functional_test|hello)
			# other cruft
			;;
		*)
			cp -a "$x" "$outdir/boot/grub/$platform/"
			;;
	esac
done

exit 0
