#!/bin/bash -
# @configure_input@
# virt-p2v-make-kiwi
# Copyright (C) 2016 SUSE.
#
# 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-kiwi"
version="@PACKAGE_VERSION@"

# Parse the command line arguments.
shortopts=o:V
longopts=help,inject-ssh-identity:,long-options,output:,short-options,version
TEMP=`getopt \
        -o "$shortopts" \
        --long "$longopts" \
        -n $program -- "$@"`
if [ $? != 0 ]; then
    echo "$program: problem parsing the command line arguments"
    exit 1
fi
eval set -- "$TEMP"

usage ()
{
    echo "Usage:"
    echo "  $program [--options] [-o kiwi-folder]"
    echo
    echo "Read $program(1) man page for more information."
    exit $1
}

output=p2v.kiwi
ssh_identity=

while true; do
    case "$1" in
        --inject-ssh-identity)
            ssh_identity="$2"
            shift 2;;
        -o|--output)
            output="$2"
            shift 2;;

        # help etc.
        --help)
            usage 0;;
        -V|--version)
            echo "$program $version"
            exit 0;;
        --short-options)
            echo -n "$shortopts" |
                @SED@ -e 's/://g' -e 's/\(.\)/-\1\n/g'
            exit 0;;
        --long-options)
            echo "$longopts" |
                @SED@ -e 's/,/\n/g' -e 's/:$//mg' -e 's/\(.*\)/--\1/mg' |
                grep -v -E -- "--(short|long)-options"
            exit 0;;
        --)
            shift
            break;;
        *)
            echo "internal error ($1)"
            exit 1;;
    esac
done

set -e

if [ -n "$VIRT_P2V_DATA_DIR" ]; then
    datadir="$VIRT_P2V_DATA_DIR"
    libdir="$VIRT_P2V_DATA_DIR"
else
    datadir="@datadir@/virt-p2v"
    libdir="@libdir@/virt-p2v"
fi

# Dependencies.  Since kiwi is SUSE-specific, only include
# dependencies.suse here.
depsfile="$datadir/dependencies.suse"
if [ ! -f "$depsfile" ]; then
    echo "$0: cannot find dependencies file ($depsfile)"
    exit 1
fi
dependencies=
while read line; do
    if [ -n "$line" ]; then
        depname=$(echo $line | awk '{gsub(/ /, "", $0); print}')
        pkg="    <package name=\"$depname\"/>"
        if [ -z "$dependencies" ]; then
            dependencies="$pkg"
        else
            dependencies="$dependencies
$pkg"
        fi
    fi
done < $depsfile

# Compute the distro-dependent pieces for kiwi
branding=
release_pkg=
base_pattern=
kiwi_boot=
repos=
. /etc/os-release
case "$NAME" in
    SLES)
        branding="SLE"
        release_pkg="sles-release"
        base_pattern="patterns-sles-Minimal"
        case "$VERSION_ID" in
            12)
                kiwi_boot="SLES12"
                repos="http://download.suse.de/ibs/SUSE:/SLE-12:/Update/standard
                      http://download.suse.de/ibs/SUSE:/SLE-12:/GA/standard"
                ;;
            12.1)
                kiwi_boot="SLES12"
                repos="http://download.suse.de/ibs/SUSE:/SLE-12-SP1:/Update/standard
                      http://download.suse.de/ibs/SUSE:/SLE-12-SP1:/GA/standard
                      http://download.suse.de/ibs/SUSE:/SLE-12:/Update/standard
                      http://download.suse.de/ibs/SUSE:/SLE-12:/GA/standard"
                ;;
            *)
                echo "Unsupported distribution $NAME $VERSION_ID"
                exit 1;;
        esac;;
    openSUSE*)
        branding="openSUSE"
        release_pkg="openSUSE-release"
        base_pattern="patterns-openSUSE-base"
        case "$VERSION" in
            13.1\ *)
                kiwi_boot="13.1"
                repos="obs://13.1/repo/oss"
                ;;
            13.2\ *)
                kiwi_boot="13.2"
                repos="obs://13.2/repo/oss"
                ;;
            42.1)
                kiwi_boot="leap42.1"
                repos="obs://leap/42.1/repo/oss"
                ;;
            *\ \(Tumbleweed\))
                kiwi_boot="leap42.1"
                repos="obs://openSUSE:Factory/standard"
                ;;
            *)
                echo "Unsupported distribution $NAME $VERSION_ID"
                exit 1;;
        esac;;
    *)
        echo "Unsupported distribution $NAME $VERSION"
        exit 1;;
esac

# Compute the repos
repos_xml=
for repo in $repos; do
    repos_xml="  <repository type=\"rpm-md\">\n    <source path=\"$repo\"/>\n  </repository>\n$repos_xml"
done

mkdir $output
cp $datadir/kiwi-config.sh $output/config.sh

mkdir -p $output/root/etc/sysconfig/network
cat >$output/root/etc/sysconfig/network/ifcfg-eth0 << EOF
BOOTPROTO='dhcp'
MTU=''
REMOTE_IPADDR=''
STARTMODE='onboot'
EOF

mkdir -p $output/root/etc/systemd/system
cp $datadir/p2v.service $output/root/etc/systemd/system

mkdir -p $output/root/etc/udev/rules.d
cat >$output/root/etc/udev/rules.d/70-persistent-net.rules <<EOF
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="?*", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="?*", NAME="eth0"
EOF

cp $datadir/issue $output/root/etc/issue

mkdir -p $output/root/usr/bin
cp $datadir/launch-virt-p2v $output/root/usr/bin
xzcat $libdir/virt-p2v.xz > $output/root/usr/bin/virt-p2v

if test "z$ssh_identity" != "z"; then
    mkdir -p $output/root/var/tmp
    cp $ssh_identity $output/root/var/tmp/id_rsa
    chmod 0600 $output/root/var/tmp/id_rsa
fi

# Now generate the final kiwi config, substituting as necessary.
@AWK@ \
  -v "dependencies=$dependencies" \
  -v "md5sum_virt_p2v=$md5sum_virt_p2v" \
  -v "branding=$branding" \
  -v "release_pkg=$release_pkg" \
  -v "base_pattern=$base_pattern" \
  -v "kiwi_boot=$kiwi_boot" \
  -v "repos=$repos_xml" \
  '{
    gsub (/__PACKAGE_NAME__/, "@PACKAGE_NAME@");
    gsub (/__PACKAGE_VERSION__/, "@PACKAGE_VERSION@");
    gsub (/__PACKAGE_VERSION_FULL__/, "@PACKAGE_VERSION_FULL@");
    gsub (/<!--__DEPENDENCIES__-->/, dependencies);
    gsub (/__BRANDING__/, branding);
    gsub (/__RELEASE_PKG__/, release_pkg);
    gsub (/__BASE_PATTERN__/, base_pattern);
    gsub (/__KIWI_BOOT__/, kiwi_boot);
    gsub (/<!--__REPOS__-->/, repos);
    print;
  }' \
  $datadir/kiwi-config.xml.in > $output/config.xml

echo "kiwi config folder written to $output"
