#!/usr/bin/sh -x

# Create Copr infrastructure image from the default Fedora Cloud image.
# Copyright (C) 2020 Red Hat, Inc.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

. "/usr/share/eimg"/lib

tmpdir=/tmp
orig_image=$1
eimg_download_image "$orig_image"
image_wip=$(mktemp "$tmpdir/wip-image-XXXXX.qcow2")
cp "$eimg_download_image_result" "$image_wip"

export LIBGUESTFS_PATH=/tmp/libguestfs-fedora-appliance
/usr/bin/eimg-appliance-in-container "$LIBGUESTFS_PATH" || :

config_network()
{
    # eimg_remote_run 'echo net.ipv6.conf.all.disable_ipv6=1 > /etc/sysctl.d/no-ipv6.conf'
    :
}

/usr/bin/eimg-prep "$image_wip" \
    && /usr/bin/eimg-wait "$image_wip" \
    && EIMG_WAITED=: /usr/bin/eimg-update "$image_wip" \
    && EIMG_WAITED=: /usr/bin/eimg-cleanup-online "$image_wip" \
    && config_network \
    && /usr/libexec/eimg-stop "$image_wip" \
    && virt-sysprep -a "$image_wip" --root-password disabled \
    && /usr/bin/eimg-minimize "$image_wip"
