#!/bin/bash

echo "STARTING"
FILES=$(find . -name "*.dem")
FILES+=$(find . -name "*.zip")

for FILE in $FILES; do

    BASENAME="${FILE:0:-4}"
    OUTFILE="${BASENAME}.xz"

    # if [ "$(find . -name "$BASENAME".dem.xz > /dev/null 2>&1 && echo found)" ] || [ "$(find . -name "$BASENAME".xz > /dev/null 2>&1 && echo found)" ]; then

    #     echo "removing duplicate '$FILE' (already compressed)..."
    #     rm "$FILE"
    #     continue
    # fi

    if [ "${FILE: -4}" == ".dem" ]; then
        echo "Compressing '$FILE'..."
        xz -9 --memory=6GiB -T0 "$FILE"
        mv "$FILE".xz "$OUTFILE"
        continue

    elif [ "${FILE: -4}" == ".zip" ]; then

        # Get the inner file name
        INNER_FILE=$(unzip -Z1 "$FILE")

        echo "Compressing '$INNER_FILE' from '$FILE' to '$OUTFILE'..."

        # Stream unzip directly to xz
        unzip -p "$FILE" "$INNER_FILE" | xz -9 --memory=6GiB -T0 -c > "$OUTFILE"

        # Move into place and remove original zip
        rm "$FILE"
        continue
    fi

done

echo "DONE"
