#!/bin/sh
# Requires maketest.sh

# Checks for argument. If there are less than 2, show error message.
if [ $# -lt 2 ]
then
    echo "Usage: $0 arg1=filename arg2=number_of_files"
    exit
fi

# Checks for number of files requested.
# If user input is less than 3, show error message.
if [ $2 -lt 3 ]
then
    echo "This script requires more than 3 requested numbers"
    exit
fi

spawn=3
# Spawn 3+1 (+1 is for remainders) processes to write files using maketest.sh
# Requires 3 or more files
start=1
((spawnWork=$2/$spawn))
((spawnWorkRemainder=$2%$spawn))

sh maketest.sh a $spawnWork &
sh maketest.sh b $spawnWork &
sh maketest.sh c $spawnWork &

if [ $spawnWorkRemainder -gt 0 ]
then
    sh maketest.sh d $spawnWorkRemainder
fi

# Waits for the last pid to finish
wait $!

# method: create 'done' files; check if exist; if not then wait
#sleep 10

# Put all the spawned files' random numbers to final file.
cat aa.txt >> $1$1.txt
cat bb.txt >> $1$1.txt
cat cc.txt >> $1$1.txt
cat dd.txt >> $1$1.txt
