#!/bin/sh
# Just a lot of randomly positioned bodies
set -eu

if [ $# = 0 ]
then
    1>&2 echo "Usage: $0 [nbody...]";
    exit 1
fi

SIZE=$(basename "$0" | sed 's/-\([0-9]\+\).sh$/\1/')
seq "$SIZE" | awk -v OFS=, '{ print 10*rand(), 10*rand(), 10*rand() }' | xargs "$@" -t 1e-3 -s 1000000 -n 1000000000 -- > "random-$SIZE.dat"

gnuplot <<EOF
set terminal gif animate delay 10
set output "|ffmpeg -hide_banner -loglevel error -stats -y -i - -row-mt 1 random-$SIZE.webm"

set size square
set xrange [-10:30]
set yrange [-10:30]
set zrange [-10:30]
set grid
unset key

steps = int(system("awk '/^$/ {i++} END { print (i/2)-1 }' random-$SIZE.dat"))
do for [i=0:steps] {
	splot "random-$SIZE.dat" index i with points pointtype 7
}
EOF
