#!/bin/bash
#
# move-dsk.sh - Move and rename .DSK files from subdirectories into parent,
#               then remove the subdirectories.
#
# Usage:
#   ./move-dsk.sh [--dry-run]
#
# If --dry-run is specified, actions are printed instead of executed.

DRYRUN=false
if [[ "$1" == "--dry-run" ]]; then
    DRYRUN=true
fi

shopt -s nullglob

for dir in */ ; do
    [[ -d "$dir" ]] || continue
    folder="${dir%/}"
    files=( "$dir"*.DSK )
    count=${#files[@]}

    if (( count == 0 )); then
        continue
    elif (( count == 1 )); then
        src="${files[0]}"
        dst="${folder}.DSK"

        if $DRYRUN; then
            echo mv -i "$src" "$dst"
        else
            mv -i "$src" "$dst"
        fi
    else
        for src in "${files[@]}"; do
            base=$(basename "$src")
            dst="${folder}-${base}"
            if $DRYRUN; then
                echo mv -i "$src" "$dst"
            else
                mv -i "$src" "$dst"
            fi
        done
    fi

    # Remove directory (force, even if non-empty)
    if $DRYRUN; then
        echo rm -rf "$dir"
    else
        rm -rf "$dir"
    fi
done

