#!/bin/bash

WORKDIR=$(pwd)
REPO_PATHS=/repo
RELEASE_VER=5.0
ARCH=$(uname -m)
REPOS=""


# create yum repo files from the repo paths
# the repo id will be "photon-" plus a number
# the created repo ids will be stored in $REPOS,
# to be used in create_treefile() below
create-repo-config() {
    index=0
    for r in $(echo ${REPO_PATHS} | tr ',' ' ') ; do
        rm -f ${WORKDIR}/ostree-${index}.repo
        if [[ $r = /* ]] ; then
            r=file://${r}
        fi
        tdnf-config create -f ${WORKDIR}/ostree-${index}.repo ostree-${index} \
            baseurl=${r} \
            enabled=1 \
            name="OSTree repo ${index}"
        if [ -z ${REPOS} ] ; then
            REPOS="ostree-${index}"
        else
            REPOS="${REPOS} ostree-${index}"
        fi
        index=$((index+1))
    done
}

# use the _ks config file for the list of packages,
# and $REPOS for "repos"
# and get the package list from the ks file
create_treefile() {
    PACKAGES=$(poi-pkglist -c ${CFG_FILE})
    cat > ${WORKDIR}/photon-base.json.tmp << EOF
    {
        "comment": "Photon Minimal OSTree",
        "osname": "photon",
        "releasever": "${RELEASE_VER}",
        "ref": "photon/${RELEASE_VER}/${ARCH}/minimal",
        "automatic_version_prefix": "${RELEASE_VER}_minimal",
        "selinux": false,
        "initramfs-args": ["--no-hostonly"],
        "bootstrap_packages": ["filesystem"],
        "documentation": false,
        "tmp-is-dir": true,
        "units": ["sshd-keygen.service", "sshd.service"]
    }
EOF
    jq --arg repos "$REPOS" --arg packages "${PACKAGES}" \
        '. | .repos=($repos | split(" ")) | .packages=($packages | split("\n"))' \
        < ${WORKDIR}/photon-base.json.tmp \
        > ${WORKDIR}/photon-base.json
}

create_ostree()
{
    mkdir -p ${WORKDIR}/rpm-ostree/repo
    ostree --repo=${WORKDIR}/rpm-ostree/repo init --mode=archive-z2 || exit 1
    rpm-ostree compose tree --repo=${WORKDIR}/rpm-ostree/repo photon-base.json || exit 1
    ostree summary --repo=${WORKDIR}/rpm-ostree/repo --update
    ostree summary -v --repo=${WORKDIR}/rpm-ostree/repo
}

tar_ostree()
{
    tar -zcf ${WORKDIR}/ostree-repo.tar.gz -C ${WORKDIR}/rpm-ostree/repo .
}

cleanup()
{
    rm -rf ${WORKDIR}/rpm-ostree
    rm -f ${WORKDIR}/photon-base.json.tmp
    rm -f ${WORKDIR}/ostree-*.repo
}

usage() {
    echo "Usage: $0"
    echo "          [-c|--config <config-file>] (required)"
    echo "          [-v|--releasever <version>] (default is ${RELEASE_VER})"
    echo "          [--repo-paths] (default ${REPO_PATHS})"
}

OPTS=$(getopt -o hc:v: --long config:,releasever:,repo-paths: -n $0 -- "$@")
if [ $? != 0 ] ; then
    usage
    echo "Terminating." >&2
    exit 1
fi

eval set -- "$OPTS"

while true; do
    case "$1" in
        -c | --config)     CFG_FILE=${2} ; shift 2 ;;
        -h)                usage ; exit 0;;
        --repo-paths)      REPO_PATHS=${2} ; shift 2 ;;
        -v | --releasever) RELEASE_VER=${2} ; shift 2 ;;
        --) shift; break ;;
        *) break ;;
    esac
done

if [ -z "${CFG_FILE}" ] ; then
    echo "need a config file" >&2
    usage
    echo "Terminating." >&2
    exit 1
fi

create-repo-config
create_treefile
create_ostree
tar_ostree
cleanup
