#!/usr/bin/env bash
#
# split.sh
#
# Simple rule: every blank line starts a new output file. Streams the
# input line-by-line (no whole-block buffering), so it's safe on files
# of any size -- this is the fix for the earlier version getting
# OOM-killed on the real 267MB export.
#
# Usage:
#   ./split.sh <input.csv> [output_dir] [prefix]
#
#   input.csv    the file to split (required)
#   output_dir   directory to write part_001.csv, part_002.csv, ...
#                (default: split_parts)
#   prefix       filename prefix (default: part)
#
# Example:
#   ./split.sh helios_boat_extract_2026-09.csv vessel-data2/
#
# Blank lines are consumed as separators (not written into either file).
# Consecutive blank lines don't create extra empty files. Handles CRLF
# input defensively (strips a trailing \r before checking for blank).

set -euo pipefail

usage() {
    echo "Usage: $0 <input.csv> [output_dir] [prefix]" >&2
    exit 1
}

[ "$#" -ge 1 ] || usage

input="$1"
outdir="${2:-split_parts}"
prefix="${3:-part}"

[ -f "$input" ] || { echo "ERROR: input file not found: $input" >&2; exit 2; }

mkdir -p "$outdir"
rm -f "${outdir:?}/${prefix}"_*.csv

awk -v outdir="$outdir" -v prefix="$prefix" '
BEGIN { idx = 1; started = 0 }
{
    line = $0
    sub(/\r$/, "", line)

    if (line == "") {
        if (started) {
            idx++
            started = 0
        }
        next
    }

    fname = sprintf("%s/%s_%03d.csv", outdir, prefix, idx)
    print line >> fname
    if (!started) {
        printf "creating %s\n", fname
        started = 1
    }
}
END {
    printf "Done. %d file(s) written to %s/\n", idx - (started ? 0 : 1), outdir
}
' "$input"
