#!/bin/sh
# Nico Schottelius
# Build release tar
# Date: Sat Oct 15 21:38:29 CEST 2005
# Last Changed: ls -l

# only release if I tell it to release
[ "$1" = "now" ] || exit 23

#if [ $# -ne 2 ]; then
#   echo "This is a maintainer-only script to release cinit"
#   echo $(basename $0): source-dir version
#   exit 1
#fi

DDIR="$(cd $(dirname $0)/../../; pwd -P)"
DIR="$(cd $(dirname $0)/../; pwd -P)"
RDIR="$(basename "$DIR")"

VERSION="$(awk -F= '/^release=/ { print $2 }' configure.in )"
#VERSION="$(cd "$DIR"; pwd -P | sed 's;.*/gpm-;;')"
#VERSION_CONF="$(head -n1 $(dirname $0)/../../conf/version | sed 's/^"\(.*\)"$/\1/')"
#VERSION_CHANGES="$(head -n1 "$(dirname $0)/../../CHANGES" | sed 's/cinit-\(.*\):$/\1/')"

echo -n "=> Version:    $VERSION; ok?"
read ok
#echo "=> Version directory:    $VERSION"
#echo "=> Version conf/version: $VERSION_CONF"
#echo "=> Version CHANGES:      $VERSION_CHANGES"

#if [ "$VERSION" = "$VERSION_CONF" -a "$VERSION" = "$VERSION_CHANGES" ]; then
#   echo "=> Version confirmed."
#fi

#DIR=$1
#VERSION=$2
TAR_NAME="gpm-${VERSION}.tar.bz2"
OUT_NAME="$DDIR/$TAR_NAME"
D_HOST=home.schottelius.org
D_BASE=www/org/schottelius/unix/www/gpm
D_DIR=${D_BASE}/archives/
D_SOURCE=${D_BASE}/browse_source/
LINK=current

echo "$VERSION in $DIR to $DDIR with $RDIR?"
echo "Copying to ${D_HOST}:${D_DIR}"
echo -n "=> Continue? "
read yes

if [ ! -d "$DIR" ]; then
   echo "$DIR is not a directory"
   exit 1
fi

#( cd "$DIR" && make dist ) || exit 1

echo "Creating bzip2 compressed tar"
tar cj -X "$DIR/.exclude" -C "$DDIR" -f "$OUT_NAME" "$RDIR"
chmod 0644 "$OUT_NAME"
echo "Transfering to $D_HOST"
scp "$OUT_NAME" "$D_HOST:$D_DIR"
echo "Extracting to $D_SOURCE"
ssh "$D_HOST" "tar xfj $D_DIR/$OUT_NAME -C $D_SOURCE"
echo "Correcting paranoid permissions"
ssh "$D_HOST" "find \"$D_SOURCE/$RDIR\" -type f -exec chmod 0644 {} \\;"
ssh "$D_HOST" "find \"$D_SOURCE/$RDIR\" -type d -exec chmod 0755 {} \\;"
ssh "$D_HOST" "cd \"$D_SOURCE\" && rm -f \"$LINK\" && ln -s \"$RDIR\" \"$LINK\""
echo "Please do not forget to update the website..."
