#!/bin/bash -
# p2v/virt-p2v-make-disk.  Generated from virt-p2v-make-disk.in by configure.
# virt-p2v-make-disk
# Copyright (C) 2014 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.

unset CDPATH

program="virt-p2v-make-disk"
version="1.28.1"

TEMP=`getopt \
        -o o:V \
        --long help,output:,version \
        -n $program -- "$@"`
if [ $? != 0 ]; then
    echo "$program: problem parsing the command line arguments"
    exit 1
fi
eval set -- "$TEMP"

output=

usage ()
{
    echo "Usage:"
    echo "  $program [--options] -o /dev/sdX os-version"
    echo
    echo "Read $program(1) man page for more information."
    exit $1
}

while true; do
    case "$1" in
        -o|--output)
            output="$2"
            shift 2;;
        -V|--version)
            echo "$program $version"
            exit 0;;
        --help)
            usage 0;;
        --)
            shift
            break;;
        *)
            echo "internal error ($1)"
            exit 1;;
    esac
done

if [ -z "$output" ]; then
    echo "$program: You must set the -o (--output) option."
    exit 1
fi

if [ $# -ne 1 ]; then
    echo "$program: Missing os-version.  See $program(1)."
    exit 1
fi

osversion="$1"

# Create a temporary directory and clean it up when we finish.
tmpdir="$(mktemp -d)"
cleanup ()
{
    rm -rf $tmpdir
}
trap cleanup INT QUIT TERM EXIT ERR

# The dependencies are:
#
#   - pcre (library only)
#   - libxml2 (library only)
#   - gtk2 (library only)
#   - ssh
#   - qemu-nbd
#   - xinit (script, part of X)
#   - Xorg (or another X server, but only Xorg has been tested)
#   - Xorg drivers
#   - some fonts
#   - hardware support (firmware etc, RHBZ#1157679)
#   - metacity (window manager, another could be used)
#   - NetworkManager
#   - nm-connection-editor
#   - nm-applet
#   - dbus-x11 (required by nm-applet, but not specified as a dep in Fedora)
#
# Note that libguestfs is NOT a dependency.
case "$osversion" in
    centos-*|fedora-*|rhel-*|scientificlinux-*)
        deps=pcre,libxml2,gtk2,/usr/bin/xinit,/usr/bin/ssh,/usr/bin/qemu-nbd,/usr/bin/Xorg,xorg-x11-drivers,xorg-x11-fonts-Type1,metacity,NetworkManager,nm-connection-editor,network-manager-applet,dbus-x11,hwdata,@hardware-support
        cat > $tmpdir/p2v.conf <<'EOF'
add_drivers+=" usb-storage "
EOF
        cat > $tmpdir/post-install <<'EOF'
#!/bin/bash
# Rebuild the initramfs.
version=` rpm -q kernel | sort -rV | head -1 | sed 's/kernel-//' `
dracut -f --kver $version
EOF
        # Double quotes because we want $tmpdir to be expanded:
        extra_args="
          --selinux-relabel
          --upload $tmpdir/p2v.conf:/etc/dracut.conf.d/
          --run $tmpdir/post-install
        "
        ;;
    debian-*|ubuntu-*)
        deps=libpcre3,libxml2,libgtk2.0-0,openssh-client,qemu-utils,xorg,xserver-xorg-video-all,metacity,network-manager,network-manager-gnome,network-manager-applet,dbus-x11,hwdata
        ;;
    archlinux-*)
        deps=pcre,libxml2,gtk2,openssh,qemu,xorg-xinit,xorg-server,xf86-video-*,metacity,NetworkManager,nm-connection-editor,network-manager-applet,dbus-x11,hwdata
        ;;
    opensuse-*|suse-*)
        deps=pcre,libxml2,gtk2,/usr/bin/ssh,/usr/bin/qemu-nbd,/usr/bin/xinit,/usr/bin/Xorg,xf86-video-*,metacity,NetworkManager,nm-connection-editor,network-manager-applet,dbus-x11,hwdata
        ;;
    *)
        echo "$program: internal error: could not work out the Linux distro from '$osversion'"
        exit 1
esac

# Deal with stupid autotools libexecdir-not-expandable crap.
prefix="/usr"
exec_prefix="/usr"
libexecdir="/usr/libexec"

if [ -n "$VIRT_P2V_DATA_DIR" ]; then
    datadir="$VIRT_P2V_DATA_DIR"
    host_libexecdir="$VIRT_P2V_DATA_DIR"
else
    datadir="/usr/share/virt-p2v"
    host_libexecdir="/usr/libexec"
fi

# Run virt-builder.  Note we controversially assume systemd here.  We
# could provide a sysvinit fallback if required.
virt-builder "$osversion"                                       \
    --output "$output"                                          \
    --update                                                    \
    --install "$deps"                                           \
    --root-password password:p2v                                \
    --upload "$datadir"/issue:/etc/issue                        \
    --upload "$datadir"/issue:/etc/issue.net                    \
    --mkdir "$libexecdir"                                       \
    --upload "$host_libexecdir"/virt-p2v:"$libexecdir"          \
    --chmod 0755:"$libexecdir"/virt-p2v                         \
    --upload "$datadir"/launch-virt-p2v:/usr/bin/               \
    --chmod 0755:/usr/bin/launch-virt-p2v                       \
    --upload "$datadir"/p2v.service:/etc/systemd/system/        \
    --mkdir /etc/systemd/system/default.target.wants            \
    --link /usr/lib/systemd/system/multi-user.target:/etc/systemd/system/default.target \
    --link /etc/systemd/system/p2v.service:/etc/systemd/system/default.target.wants/p2v.service \
    --edit '/usr/lib/systemd/system/getty@.service:
        s/^ExecStart=(.*)/ExecStart=$1 -a root/
    '                                                           \
    --edit '/etc/systemd/logind.conf:
        s/^[Login]/[Login]\nReserveVT=1\n/
    '                                                           \
    $extra_args

# We have to do this so the cleanup() handler runs.
exit $?
