#!/usr/bin/env gjs

const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;

const ITERATIONS = 50;

function getContents(count) {
    let contents = '';
    for (let i = 1; i < count; i++) {
	contents += 'Lorem ipsum dolor sit amet\n';
    }
    return contents;
}

let endPath = Gio.file_new_for_path('testfile-'+ (ITERATIONS-1));
if (endPath.query_exists(null)) {
    print("Verifying...");
    for (let i = 0; i < ITERATIONS; i++) {
	let testFile = Gio.file_new_for_path('testfile-' + i);
	let expectedContents = getContents(i);
	let [success, contents, len, etag] = testFile.load_contents(null);
	if (expectedContents != contents)
	    print("" + testFile.get_path() + " differs");
    } 
    print("done.");
} else {
    print("Generating testfiles...");
    for (let i = 0; i < ITERATIONS; i++) {
	let testFile = Gio.file_new_for_path('testfile-' + i);
	let contents = getContents(i);
	GLib.file_set_contents(testFile.get_path(), contents);
    } 
    print("done.");
}
