#!/bin/bash
#
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

if [ -f ${TARGET_ROOT}/.extra_settings ] ; then
    . ${TARGET_ROOT}/.extra_settings
fi
ARCH=${ARCH:-x86_64}
if [ $ARCH = amd64 ]; then
    ARCH=x86_64
fi
# Calling elements will need to set DISTRO_NAME and DIB_RELEASE
DIB_YUMCHROOT_EXTRA_ARGS=${DIB_YUMCHROOT_EXTRA_ARGS:-}
YUMCHROOT_TARBALL=$DIB_IMAGE_CACHE/yumchroot-${DISTRO_NAME}-${DIB_RELEASE}-${ARCH}.tar.gz
# TODO Maybe deal with DIB_DISTRIBUTION_MIRROR
http_proxy=${http_proxy:-}

set -x
if [ -n "$DIB_OFFLINE" -o -n "${DIB_YUMCHROOT_USE_CACHE:-}" ] && [ -f $YUMCHROOT_TARBALL ] ; then
    echo $YUMCHROOT_TARBALL found in cache. Using.
    sudo tar -C $TARGET_ROOT --numeric-owner -xzf $YUMCHROOT_TARBALL
else
    sudo mkdir -p $TARGET_ROOT/var/lib/rpm
    sudo rpm --root $TARGET_ROOT --initdb

    WORKING=$(mktemp --tmpdir=${TMP_DIR:-/tmp} -d)
    EACTION="rm -r $WORKING"
    trap "$EACTION" EXIT

    yumdownloader \
            --releasever=$DIB_RELEASE \
            --setopt=reposdir=$TMP_HOOKS_PATH/yum.repos.d \
            --destdir=$WORKING \
        ${DISTRO_NAME}-release
    RELEASE_RPMS="${DISTRO_NAME}-release"
    if [ $DISTRO_NAME = fedora ] ; then
        yumdownloader \
                --releasever=$DIB_RELEASE \
                --setopt=reposdir=$TMP_HOOKS_PATH/yum.repos.d \
                --destdir=$WORKING \
            ${DISTRO_NAME}-repos
        RELEASE_RPMS="${RELEASE_RPMS} ${DISTRO_NAME}-repos"
    fi
    sudo rpm --root $TARGET_ROOT -ivh $WORKING/*rpm

    YUM_CACHE=$DIB_IMAGE_CACHE/yum

    # Install yum into the chroot but use the gpg keys we've installed
    # directly into the chroot for the purpose
    sudo sed -i "s,/etc/pki/rpm-gpg,$TARGET_ROOT/etc/pki/rpm-gpg,g" $TARGET_ROOT/etc/yum.repos.d/*repo
    sudo yum -y \
            --setopt=cachedir=$YUM_CACHE/$ARCH/$DIB_RELEASE \
            --setopt=reposdir=$TARGET_ROOT/etc/yum.repos.d \
            --installroot $TARGET_ROOT \
        install yum

    # Set gpg path back because subsequent actions will take place in the chroot
    sudo sed -i "s,$TARGET_ROOT/etc/pki/rpm-gpg,/etc/pki/rpm-gpg,g" $TARGET_ROOT/etc/yum.repos.d/*repo

    # We have to do this next bit outside of the chroot to get far enough
    # that dib-run-parts can operate
    sudo cp /etc/resolv.conf $TARGET_ROOT/etc/resolv.conf

    # Same logic as in the yum element to provide for yum caching
    # copied here because the sequencing is wrong otherwise
    sudo mkdir -p $TMP_MOUNT_PATH/tmp/yum
    sudo mount --bind $YUM_CACHE $TMP_MOUNT_PATH/tmp/yum
    sudo chroot $TARGET_ROOT yum -y --releasever=$DIB_RELEASE \
        --setopt=cachedir=/tmp/yum/$ARCH/$DIB_RELEASE \
        install $RELEASE_RPMS

    sudo chroot $TARGET_ROOT yum makecache
    sudo chroot $TARGET_ROOT yum -y \
        --setopt=cachedir=/tmp/yum/$ARCH/$DIB_RELEASE \
        install passwd findutils sudo util-linux-ng

    # RPM doesn't know whether files have been changed since install
    # At this point though, we know for certain that we have changed no
    # config files, so anything marked .rpmnew is just a bug.
    for newfile in $(sudo find $TARGET_ROOT -type f -name '*rpmnew') ; do
        sudo mv $newfile $(echo $newfile | sed 's/.rpmnew$//')
    done
    sudo rm $TARGET_ROOT/etc/resolv.conf
    sudo umount $TMP_MOUNT_PATH/tmp/yum

    echo Caching result in $YUMCHROOT_TARBALL
    sudo tar --numeric-owner -C $TARGET_ROOT -zcf $YUMCHROOT_TARBALL --exclude='./tmp/*' .
fi

sudo rm -f ${TARGET_ROOT}/.extra_settings
