#!/bin/bash

set -u
set -e

if [ $# -lt 5 ]; then
	echo "Usage: $0 filename version arch changes_file suite"
	exit 1
fi

export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
. $SCRIPTVARS

WIN32_LOADER_PATH="$1"	# win32-loader_${VERSION}_${ARCH}{.exe,txt}
WIN32_LOADER_FILE="${WIN32_LOADER_PATH##*/}"
VERSION="$2"
ARCH="$3"
CHANGES="$4"	# Changes file for the upload
SUITE="$5"

case $SUITE in
    unstable|sid|*proposed-updates)
	    : # nothing to do
	    ;;
    *)
	    SUITE="${SUITE}-proposed-updates"
	    ;;
esac

case "${WIN32_LOADER_FILE}" in
    win32-loader_*.exe|win32-loader_*.txt)
        : # okay
        ;;
    *)
        echo "Error: invalid filename for byhand-win32-loader"
        exit 1
        ;;
esac

# This must end with /
TARGET="${ftpdir}/tools/win32-loader/${SUITE}/"

# Prepare the target filename by removing the version and arch parts;
# transforms any/path/win32-loader_${VERSION}_${ARCH}.${extension} to win32-loader.${extension}
TARGET_FILENAME="${WIN32_LOADER_FILE%%_*}.${WIN32_LOADER_FILE##*.}"

# Check validity of the target directory
if [ ! -d "$TARGET" ]; then
	mkdir -p "$TARGET"
fi

# Put said file into the tools directory
# Use --remove-destination to avoid problems with the fact that the target may
# be a hardlink and in that case we'd update multiple suites together
cp --remove-destination "$WIN32_LOADER_PATH" "${TARGET}${TARGET_FILENAME}"

# Chmod it correctly
chmod 0644 "${TARGET}${TARGET_FILENAME}"

exit 0
