/**************************************************************************************
 *
 *     Project:    kvmstoreserver-tools
 *     File name:  main.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver-tools.
 *
 * kvmstoreserver-tools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver-tools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver-tools.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package main

import (
	"fmt"
	"generator"
	"time"
	"utils"
)

func main() {

	var (
		chunkSize  int
		outerLoops int
		rest       int
		objCounter int
	)

	chunkSize = 10000

	cmdArgs := utils.NewCmdController()
	synchronizer := utils.NewParaController()

	if cmdArgs.GetObjects() < chunkSize {
		chunkSize = cmdArgs.GetObjects()
		outerLoops = 1
		rest = 0
	} else {
		outerLoops = cmdArgs.GetObjects() / chunkSize
		rest = cmdArgs.GetObjects() % chunkSize
	}

	start := time.Now()

	for outerIdx := 0; outerIdx < outerLoops; outerIdx++ {

		for innerIdx := 0; innerIdx < chunkSize; innerIdx++ {
			synchronizer.Wg.Add(1)
			generator.NewWorker(objCounter, cmdArgs.GetTargetRef(), synchronizer).
				WriteIntoTarget()

			objCounter++
		}

		synchronizer.Wg.Wait()
	}

	if rest > 0 {
		for innerIdx := 0; innerIdx < rest; innerIdx++ {
			synchronizer.Wg.Add(1)
			generator.NewWorker(objCounter, cmdArgs.GetTargetRef(), synchronizer).
				WriteIntoTarget()

			objCounter++
		}

		synchronizer.Wg.Wait()
	}

	fmt.Printf("%v generic JSON objects were generated in %v and stored in path -> %v\n",
		objCounter, time.Since(start), *cmdArgs.GetTargetRef())

}
