#!/bin/bash
# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

set -eu

if [ $# != 0 ]; then
	echo "usage: make-source" >&2
    exit 2
fi

base=$(cd $(dirname $0); pwd -P)
outdir=$(pwd -P)

# Make a .tar.gz with the source code

cd $base/..

if [ -d .git ]; then
    git archive HEAD --prefix cockpit-wip/ --output $outdir/cockpit-wip.tar
    if [ -d node_modules ]; then
        tar -rf $outdir/cockpit-wip.tar --transform='s|^|cockpit-wip/|S' node_modules
    fi
else
    tar -cf $outdir/cockpit-wip.tar --transform='s|^|cockpit-wip/|S' .
fi

# Make sure to include the .tarball in our tarball
echo "wip" > $outdir/.tarball
tar -rf $outdir/cockpit-wip.tar --transform='s|^|cockpit-wip/|S' -C $outdir .tarball
rm $outdir/.tarball

gzip -f --fast $outdir/cockpit-wip.tar
echo $outdir/cockpit-wip.tar.gz
