#!/bin/bash
# -*- Mode:Shell; indent-tabs-mode:nil; tab-width -*-
#
# Copyright 2007 Kenneth Loafman <kenneth@loafman.com>
#
# This file is part of duplicity.
#
# Duplicity 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.
#
# Duplicity 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 duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


# Modify to fit your system
REMOTE="dione.local:///Users/ken/workspace/duplicity-src8/"
LOCAL="/home/ken/workspace/duplicity-src8/"
KEYNAME="firstprime"


set -e

if [ "$#" -ne 0 ]; then
    echo "usage: makesnap"
    exit 2
fi

export PYTHONWARNINGS="ignore::DeprecationWarning"

BASE=`python3 -c "from duplicity import __version__; print(__version__, end='')"`
REVNO=`bzr revno`
VERSION=${BASE}${REVNO}

# get the working source tree
rsync -av \
    --delete \
    --exclude=*.pyc \
    --exclude=*.so \
    --exclude=.DS_Store \
    --exclude=.pytest_cache \
    --exclude=.tox \
    --exclude=__pycache__ \
    --exclude=docs \
    ${REMOTE} \
    ${LOCAL}

# run makedist
cd ${LOCAL}
dist/makedist

# make clean source dir
rm -rf build
mkdir -p build
tar xf duplicity-${VERSION}.tar.gz -C build
cd build/duplicity-${VERSION}

# set version in build copy of snapcraft.yaml
sed -i s/\$VERSION/${VERSION}/g snap/snapcraft.yaml

# make sure we're logged in
snapcraft login --with ~/.snaplogin

# build snap
snapcraft snap

# push to edge
snapcraft push duplicity_${VERSION}*.snap --release edge

# sign snap
# TODO: find reason for gpg timeout on signing
#snapcraft sign-build duplicity_${VERSION}*.snap --key-name ${KEYNAME}

# mv into working source tree
mv duplicity_${VERSION}*.snap* ../..
cd ../..

# clean up
rm -rf build
