##
# Base functions
# - Finally
##

## Global variables

P_ROOT=../../..
PACKAGES=$P_ROOT/out/pkgs/${target_arch}/${whatami}

## Failsafe checks

if [ ! -d $PACKAGES ]; then
    echo "This is not runned in needed development env"
    echo " "
    echo "The problem: ${PACKAGES} is missing"
    echo " "
    echo "This dir holds arch sepcific new packages"
    echo "Check out: https://git.martinvlba.eu/Evolinx/internal_manifest"

    # Now hard fail
    exit
fi

## The main thing

run_noraml_update() {
    create_temp_layout

    copy_new_packages

    run_db_update

    restore_the_layout
}

create_temp_layout() {
    mkdir -p pool

    mv packages/*${target_arch}*.gz pool/
}

copy_new_packages() {
    echo
    mv -fv ${PACKAGES}/*${target_arch}*.gz pool/
}

run_db_update() {
    cd packages

    # Remove old db entry
    rm $whatami.db* $whatami.files*

    # Do the update
    repo-add $whatami.db.tar.gz ../pool/*${target_arch}*.gz

    rm $whatami.db $whatami.files

    # Rename the finished db ( so it works as needed for me )
    cp -f $whatami.db.tar.gz $whatami.db
    cp -f $whatami.files.tar.gz $whatami.files

    cd ..
}

restore_the_layout() {
    mv pool/*.gz packages/

    rm -r pool
}
