#!/bin/bash

# Copyright 2016-2018 See AUTHORS file
#
# 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 3 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/>.

set -e
. /sbin/sabayondevkit-functions.sh

SAB_WORKSPACE="${SAB_WORKSPACE:-$PWD}"
entropysrv=$(mktemp)
createrepo=$(mktemp)
REPOSITORY_NAME="${REPOSITORY_NAME:-default}"
REPOSITORY_DESCRIPTION="${REPOSITORY_DESCRIPTION:-My Sabayon repository}"
DOCKER_IMAGE="${DOCKER_IMAGE:-sabayon/eit-amd64}"
PORTAGE_ARTIFACTS="${PORTAGE_ARTIFACTS:-$SAB_WORKSPACE/portage_artifacts}"
OUTPUT_DIR="${OUTPUT_DIR:-$SAB_WORKSPACE/entropy_artifacts}"
DOCKER_OPTS="${DOCKER_OPTS:--ti --rm}"

args=("$@")
EDITOR=cat

docker_env=(-e "EDITOR=$EDITOR"
            -e "REPOSITORY=$REPOSITORY_NAME"
            -e "LC_ALL=en_US.UTF-8")

docker_volumes=(
            -v "$OUTPUT_DIR:/sabayon/artifacts"
            -v "$createrepo:/sabayon/bin/create_repo.sh"
            -v "$entropysrv:/etc/entropy/server.conf"
            -v "$PORTAGE_ARTIFACTS:/usr/portage/packages" )

[ -z "$PUBKEY" ] || docker_volumes+=(-v "$PUBKEY:/etc/entropy/mykeys/key.pub")
[ -z "$PRIVATEKEY" ] || docker_volumes+=(-v "$PRIVATEKEY:/etc/entropy/mykeys/private.key")

check_docker_requirements

echo "Repository: $REPOSITORY_NAME"
echo "Repository Description: $REPOSITORY_DESCRIPTION"
echo "Remove: ${args[@]}"

# Creating the building script on-the-fly
cat >$createrepo <<EOF
#!/bin/bash
set -e
repo="\${REPOSITORY:-default}"

if [ -d "/sabayon/artifacts/standard" ]; then
  eit unlock \$repo || true
  eit pull --quick \$repo || true
  #eit sync \$repo
else
  echo "ouch no repository there" && exit 3;
fi

[ -f "/etc/entropy/mykeys/key.pub" ] && [ -f "/etc/entropy/mykeys/private.key" ] && { ! eit key status \$repo; } && \
eit key import \$repo /etc/entropy/mykeys/private.key /etc/entropy/mykeys/key.pub && { eit key sign \$repo || true; } && { echo "=== Repository key imported successfully ==="; }

eit clean --quick || { echo "ouch unable to remove" && exit 3; }
eit vacuum --quick || { echo "ouch unable to remove" && exit 3; }

echo "Yes Yes Yes" | eit commit --quick
eit push --quick --force

EOF
chmod +x $createrepo

# Creating the entropy repository configuration on-the-fly
cat >$entropysrv <<EOF
# expiration-days = <internal value>
community-mode = enable
weak-package-files = disable
database-format = bz2
# sync-speed-limit =
# server-basic-languages = en_US C
# disabled-eapis = 1,2
# expiration-based-scope = disable
# nonfree-packages-directory-support = disable
rss-feed = disable
changelog = disable
rss-name = packages.rss
rss-base-url = http://packages.sabayon.org/?quicksearch=
rss-website-url = http://www.sabayon.org/
max-rss-entries = 10000
# max-rss-light-entries = 100
rss-light-name = updates.rss
managing-editor =
broken-reverse-deps = disable
default-repository = $REPOSITORY_NAME
repository=$REPOSITORY_NAME|$REPOSITORY_DESCRIPTION|file:///sabayon/artifacts
EOF


function cleanup {
  rm -rf $createrepo
  rm -rf $entropysrv
}

trap cleanup EXIT


echo "docker run $DOCKER_OPTS --entrypoint /bin/bash ${docker_env[@]} ${docker_volumes[@]} $DOCKER_IMAGE /sabayon/bin/create_repo.sh"

docker run $DOCKER_OPTS  --entrypoint /bin/bash "${docker_env[@]}" "${docker_volumes[@]}" $DOCKER_IMAGE /sabayon/bin/create_repo.sh || true
