#!/usr/bin/env octave-cli

# Run with -i switch to run interactively

addpath("../../octave/")
defaults

args = argv();
dry_run = false;
interactive = false;

for a = args'
	if strcmp(a, "--force-gui")
		break;
		#disp("Running from GUI");
	elseif strcmp(a, "-i")
		disp("interactive");
		interactive = true;
	elseif strcmp(a, "-n")
		disp("dry-run");
		dry_run = true;
	elseif (! strncmp(a, "-", 1) && exist(a{1}, "file"))
		disp("found file");
		filename = a{1};
	endif
endfor

if (! exist("filename", "var"))
	disp("No file specified, exiting");
	return;
endif

# Values from Abarcus
values = csvread(filename);
time = values(:, 1);
z_abarcus = values(:, 2);

# Exact analytical solution
mass = 10;
z0 = 0;
v0 = 0.2;
stiffness = 200;
omega = sqrt(stiffness/mass);

function z = harmonic_motion (t, z0, v0, omega)
	z = z0 .* cos(omega .* t) .+ (v0./omega) .* sin(omega .* t);
endfunction

z_analytic = harmonic_motion(time, z0, v0, omega);

clf;
hold on;
	plot(time, z_abarcus);
	plot(time, z_analytic, "r");
hold off;
xlabel('$t\,[\second]$');
ylabel('$z\,[\metre]$');
axis([0, 30, -0.1-eps, 0.1+eps]);
deccomma;
grid on;
box off;
h = legend("numerický model (Abarcus)", "analytické řešení");
legend_settings(h);
legend("location", "northeast");

if ! dry_run
	output = strrep(filename, ".csv", "");
	print(output, "-depslatex", "-color");
endif

if interactive
	pause;
endif
