#!/bin/bash
KolTargets=0
MaxKolTargets=9
RangeXa=20000
RangeYa=14000
UskorenieMax=500
TempDirectory=/tmp/GenTargets
DirectoryData="$TempDirectory/.Data"
DirectoryTargets="$TempDirectory/Targets"
DestroyDirectory="$TempDirectory/Destroy"
LogFile=$TempDirectory/GenTargets.log
rm -rf $TempDirectory/.Data 2>/dev/null
mkdir $TempDirectory >/dev/null 2>/dev/null; mkdir $DirectoryData >/dev/null 2>/dev/null; mkdir $DirectoryTargets >/dev/null 2>/dev/null; mkdir $DestroyDirectory >/dev/null 2>/dev/null
while : 
  do
	if [[ "$KolTargets" -le "$MaxKolTargets" ]]
		then 
			Gran=$RANDOM ; let "Gran %= 4"
			if [ $Gran -eq 0 ]
			then 	
				Xkoord=0 
				Ykoord=$RANDOM 
				let "Ykoord%=$RangeYa" 
				UskorenieY=$RANDOM  
				let "UskorenieY %= $UskorenieMax*2" 
				let UskorenieY=$UskorenieY-$UskorenieMax 
				UskorenieX=$RANDOM 
				let "UskorenieX %= $UskorenieMax" 
			fi 
			if [ $Gran -eq 1 ]
			then 
				Xkoord=$RangeXa
				Ykoord=$RANDOM 
				let "Ykoord %= $RangeYa"
				UskorenieY=$RANDOM 
				let "UskorenieY %= $UskorenieMax*2"
				let UskorenieY=$UskorenieY-$UskorenieMax
				UskorenieX=$RANDOM 
				let "UskorenieX %= $UskorenieMax"
				let UskorenieX=$UskorenieX-$UskorenieMax
			fi 
			if [ $Gran -eq 2 ]
				then Ykoord=0
				Xkoord=$RANDOM
				let "Xkoord %= $RangeXa"
				UskorenieY=$RANDOM
				let "UskorenieY %= $UskorenieMax"
				let UskorenieY=$UskorenieY
				UskorenieX=$RANDOM
				let "UskorenieX %= $UskorenieMax*2"
				let UskorenieX=$UskorenieX-$UskorenieMax
			fi 
			if [ $Gran -eq 3 ]
			then 
				Ykoord=$RangeYa
				Xkoord=$RANDOM
				let "Xkoord %= $RangeXa"
				UskorenieY=$RANDOM
				let "UskorenieY %= $UskorenieMax" 
				let UskorenieY=$UskorenieY-$UskorenieMax
				UskorenieX=$RANDOM
				let "UskorenieX %= $UskorenieMax*2"
				let UskorenieX=$UskorenieX-$UskorenieMax
			fi 
			echo Xkoord=$Xkoord          >  $DirectoryData/$KolTargets.Target
			echo Ykoord=$Ykoord          >> $DirectoryData/$KolTargets.Target
			echo XSkorost=$UskorenieX    >> $DirectoryData/$KolTargets.Target
			echo YSkorost=$UskorenieY    >> $DirectoryData/$KolTargets.Target
			BASE_STR=`mcookie` ;NameTarget=${BASE_STR:11:6};
			echo TargetName=$NameTarget  >> $DirectoryData/$KolTargets.Target
			KolTargets=$[KolTargets+1]
		fi
	KolTargets=$(ls $DirectoryData/ |wc -l)
	for i in 0 1 2 3 4 5 6 7 8 9
		do
 			if [ -f "$DirectoryData/$i.Target" ]
				then 
					source $DirectoryData/$i.Target
					let Xkoord=$Xkoord+$XSkorost
					let Ykoord=$Ykoord+$YSkorost
					source $DestroyDirectory/$TargetName 2>/dev/null
					if [ "$DestroyID" == "$TargetName" ]
						then 
							echo $DestroyID Destoy
							rm $DestroyDirectory/$TargetName 2>/dev/null
							rm $DirectoryData/$i.Target 2>/dev/null
							continue 
						fi
					echo Xkoord=$Xkoord >  $DirectoryData/$i.Target
					echo Ykoord=$Ykoord >> $DirectoryData/$i.Target
					echo XSkorost=$XSkorost >> $DirectoryData/$i.Target
					echo YSkorost=$YSkorost >> $DirectoryData/$i.Target
					echo TargetName=$TargetName >> $DirectoryData/$i.Target
					if [[  "$Xkoord" -ge "$RangeXa" || "$Ykoord" -ge "$RangeYa" || "$Xkoord" -le 0 || "$Ykoord" -le 0 ]]
						then	
							rm $DirectoryData/$i.Target 2>/dev/null
							let KolTargets=$i 
					fi		
				else 
					let KolTargets=$i 
				fi
			FileData=`mcookie | sed -e '2p'`
			cp $DirectoryData/$i.Target "$DirectoryTargets/Target.id.$TargetName.$FileData" 2>/dev/null
			KolFile=$(ls $DirectoryTargets/ |wc -l)
			if  [ "$KolFile" -ge  100 ]
				then
					let KolFile=$[KolFile-100]
					ls -t $DirectoryTargets/ |tail -n $KolFile |cat>/tmp/listfile
					NameFile=$(tail -n 1 /tmp/listfile)
					rm $DirectoryTargets/$NameFile 2>/dev/null
					rm /tmp/listfile 2>/dev/null
					let KolFile=$[KolFile-99]
					ls -t $DirectoryTargets/ |tail -n $KolFile |cat>/tmp/listfile
					NameFile=$(tail -n 1 /tmp/listfile)
					rm $DirectoryTargets/$NameFile 2>/dev/null
					rm /tmp/listfile 2>/dev/null
				fi
		done
  sleep 3
  done
