#!/bin/bash
#
# makefeduprepo - quick script to make a fedup-usable instrepo for testing
#
# Copyright (C) 2012 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, see <http://www.gnu.org/licenses/>.
#
# Author: Will Woods <wwoods@redhat.com>


# constants and helper functions!
# -------------------------------
export PLYMOUTH_THEME_NAME=fedup
KERNELPATH=vmlinuz
UPGRADEPATH=upgrade.img
die() { echo $(basename $0): error: $@ >&2; exit 1; }
sha256() { set -- $(sha256sum $1); echo "sha256:$1"; }


# argument validation stuff!
# --------------------------
repodir="$1"
kver="${2:-$(uname -r)}"

[ -z "$1" ] && echo "usage: makefeduprepo REPODIR [KERNELVER]" && exit 1
[ -d "$repodir" ] || die "directory $repodir doesn't exist. create it first."
[ -f $repodir/repodata/repomd.xml ] || createrepo=1

source /etc/os-release 2>/dev/null

kernel="/boot/vmlinuz-$kver" # NOTE: distro-specific

# sanity checks!
# --------------
[ -f "$kernel" ] || die "can't find kernel $kernel"
[ -d "/lib/modules/$kver" ] || die "can't find modules for kernel $kver"

command -v dracut >/dev/null || die "can't find dracut"
dracut --list-modules 2>/dev/null | grep -q system-upgrade || \
    die "dracut can't find system-upgrade module. install fedup-dracut."

if [ $createrepo ]; then
    command -v createrepo >/dev/null || die "can't find createrepo"
fi



# actual repo creation!
# ---------------------
# make leading dirs
echo "creating fedup-capable repo at $repodir"
mkdir -p "$repodir/$(dirname $KERNELPATH)" \
         "$repodir/$(dirname $UPGRADEPATH)" || \
    die "failed to make required directories."

# copy kernel into place
echo "* copying kernel to $KERNELPATH"
cp -f $kernel "$repodir/$KERNELPATH" || \
    die "couldn't copy kernel."

# build upgrade.img
sudo -n true &>/dev/null || echo "need root to build initrd."
sudo true || die "failed to become root."
echo "* building $UPGRADEPATH (this will take a moment..)"
sudo sh -c "
     export PLYMOUTH_THEME_NAME=$PLYMOUTH_THEME_NAME
     dracut --conf /dev/null --confdir $repodir \
            --no-hostonly --nolvmconf --nomdadmconf \
            --add system-upgrade \
            --xz --force \"$repodir/$UPGRADEPATH\" $kver
     chown $(id -u):$(id -g) \"$repodir/$UPGRADEPATH\"" || \
     die "dracut failed to build upgrade.img."

# write .treeinfo
echo "* writing .treeinfo"
arch=$(uname -m)
cat > $repodir/.treeinfo << __EOT__ || die "couldn't write .treeinfo"
[general]
family = fedup
timestamp = $(date '+%s')
arch = $arch
version = ${VERSION_ID:-0}

[images-$arch]
kernel = $KERNELPATH
upgrade = $UPGRADEPATH

[checksums]
$KERNELPATH = $(sha256 "$repodir/$KERNELPATH")
$UPGRADEPATH = $(sha256 "$repodir/$UPGRADEPATH")
__EOT__


# create repodata if necessary
if [ $createrepo ]; then
    echo "* creating repodata"
    createrepo $repodir >/dev/null || die "createrepo failed."
else
    echo "* repodata exists, skipping createrepo"
fi

chmod -R a+r $repodir

cat > $repodir/serve << '__EOT__'
#!/bin/bash
PORT=${1:-8000}
echo "This repo will be reachable at:"
echo
for ip in $(ip addr | sed -rn 's| *inet ([0-9.]+)/.*|\1|p'); do
    echo "    http://$ip:$PORT/"
done
echo
cat | python - $PORT <<__PYTHON_END__
from SimpleHTTPServer import test
try:
    test()
except KeyboardInterrupt:
    print("exiting")
__PYTHON_END__
__EOT__
chmod 0755 $repodir/serve

echo "done."

if [ -z "$VERSION_ID" ]; then
    echo "You should probably set 'version' in $repodir/.treeinfo"
fi
