#! /bin/csh


rm ./results-sale/*
#rm ./mix/data*
cp ./awk/pingthrput.out ./results-sale/.
cp ./awk/runplot.com ./results-sale/.

#OVDE number of TCP flows
set flowstcp = 1;


foreach rate1 ( 0 )
 foreach length1 ( 0 )
  foreach rate2 ( 3 ) 
#orig is 1.5
#1.5 2 
   foreach length2 ( 200 )
#orig is 100
#30 50 70 90 100 150 200
    foreach period ( 400 500 600 700 800 900 1000 1050 1100 1150 1200 1250 1300 1350 1400 1450 1500 1550 1600 1700 1800 1900 2000 3000 4000 5000 )
#foreach period ( 1000 1050 1100 1150 1200 1250 1300 )
#500 600 700 800 900 1000 1050 1100 1150 1200 1250 1300 1350 1400 1450 1500 1550 1600 1650 1700 1750 1800 1850 1900 2000 3000 4000 5000 
     foreach pack_size ( 500 )


#rm previous bin file
rm -f ./ascii/file.bin

#make a txt DoS file
./mk_dos_trace.out $rate1 $length1 $rate2 $length2 $period $pack_size  ./ascii/ascii-$rate1-$length1-$rate2-$length2-$period-$pack_size.txt ./cwnd/dos-$length2-$period

#remove cwnd file
rm -f ./cwnd/dos-$length2-$period

#make bin file
./binary_read_file.out ./ascii/file.bin ./ascii/ascii-$rate1-$length1-$rate2-$length2-$period-$pack_size.txt

#rm txt file
rm -f ./ascii/ascii-$rate1-$length1-$rate2-$length2-$period-$pack_size.txt



#RED
ns-choke1 red-pd.tcl one netMix Mix enable 0 verbose -1 plotq 0 time 400
#ns red-pd.tcl one netMix Mix enable 0 verbose -1 plotq 0 time 400

#OVDE i<=$flowstcp+1
awk '{ if ($4 != 0) { if ($2==190) start[$4]=$6; if ($2==390) bw[$4]=($6 - start[$4])*8/(200*1000000)}} END {for (i=1; i<=2; i++) print i, bw[i]}' one.netMix.Mix.0.flows > data-$rate1-$length1-$rate2-$length2-$period-$pack_size.false

cp data-$rate1-$length1-$rate2-$length2-$period-$pack_size.false data.false



awk '{sum=sum+$2/1.5;  printf("rnd 0 %d thruput %f\n", $1,$2/1.5)} END {printf("rnd 0 %d thruput %f\n", 3, sum-$2/1.5)}' data-$rate1-$length1-$rate2-$length2-$period-$pack_size.false >> ./results-sale/xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size.false


if ( $period == 1100 ) then

@ tot = 3 + $flowstcp
@ dos = 2 + $flowstcp

echo "set size 1.0,0.6" >> ./results-sale/plotcmd
echo "set key top right" >> ./results-sale/plotcmd 
echo "set xrange [0:2]" >> ./results-sale/plotcmd
echo "set yrange [0:1.2]" >> ./results-sale/plotcmd
echo "set xlabel %Inter-period (sec)% %Helvetica,24%" >> ./results-sale/plotcmd
echo "set ylabel %Throughput (normalized)% %Helvetica,24%"  >> ./results-sale/plotcmd
echo "set terminal postscript color %Helvetica,18%" >> ./results-sale/plotcmd 
echo "set output %fig-$length1-$rate1-$length2-$rate2.RED.ps%" >> ./results-sale/plotcmd
echo "plot 'thr-$length1-$rate1-$length2-$rate2.false' using 1:$tot title 'tot-tcp-$length1-$rate1-$length2-$rate2.RED' w lp lw 2, 'thr-$length1-$rate1-$length2-$rate2.false' using 1:$dos title 'dos-$length1-$rate1-$length2-$rate2.RED' w lp lw 2" >> ./results-sale/plotcmd 


#OVDE - 0 1 2 ... $flowstcp-1
foreach i (0)
@ no = 2 + $i
echo ", 'thr-$length1-$rate1-$length2-$rate2.false' using  1:$no title 'tcp$i-$length1-$rate1-$length2-$rate2.RED' w lp lw 2" >> ./results-sale/plotcmd
end
echo "\n" >> ./results-sale/plotcmd

endif

echo "pingthrput.out 6 ./xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size.false a ./xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size-norm.false 1 1.5 thr-$length1-$rate1-$length2-$rate2.false $period $flowstcp" >> ./results-sale/norm

chmod 777 ./results-sale/norm



#RED-PD

###ns red-pd.tcl one netMix Mix verbose -1 plotq 0 time 400
###
####OVDE i<=$flowstcp+1
###awk '{ if ($4 != 0) { if ($2==190) start[$4]=$6; if ($2==390) bw[$4]=($6 - start[$4])*8/(200*1000000)}} END {for (i=1; i<=2; i++) print i, bw[i]}' one.netMix.Mix.1.flows > data-$rate1-$length1-$rate2-$length2-$period-$pack_size.true

###cp data-$rate1-$length1-$rate2-$length2-$period-$pack_size.true data.true
###
###
###
###awk '{sum=sum+$2/1.5;  printf("rnd 0 %d thruput %f\n", $1,$2/1.5)} END {printf("rnd 0 %d thruput %f\n", 3, sum-$2/1.5)}' data-$rate1-$length1-$rate2-$length2-$period-$pack_size.true >> ./results-sale/xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size.true
###
###
###if ( $period == 1100 ) then
###
###@ tot = 3 + $flowstcp
###@ dos = 2 + $flowstcp
###
###echo "set size 1.0,0.6" >> ./results-sale/plotcmd
###echo "set key top right" >> ./results-sale/plotcmd 
###echo "set xrange [0:2]" >> ./results-sale/plotcmd
###echo "set yrange [0:1.2]" >> ./results-sale/plotcmd
###echo "set xlabel %Inter-period (sec)% %Helvetica,24%" >> ./results-sale/plotcmd
###echo "set ylabel %Throughput (normalized)% %Helvetica,24%"  >> ./results-sale/plotcmd
###echo "set terminal postscript color %Helvetica,18%" >> ./results-sale/plotcmd 
###echo "set output %fig-$length1-$rate1-$length2-$rate2.RED-PD.ps%" >> ./results-sale/plotcmd
###echo "plot 'thr-$length1-$rate1-$length2-$rate2.true' using 1:$tot title 'tot-tcp-$length1-$rate1-$length2-$rate2.RED-PD' w lp lw 2, 'thr-$length1-$rate1-$length2-$rate2.true' using 1:$dos title 'dos-$length1-$rate1-$length2-$rate2.RED-PD' w lp lw 2" >> ./results-sale/plotcmd 
###
###
####OVDE - 0 1 2 ... $flowstcp-1
###foreach i (0)
###@ no = 2 + $i
###echo ", 'thr-$length1-$rate1-$length2-$rate2.true' using  1:$no title 'tcp$i-$length1-$rate1-$length2-$rate2.RED-PD' w lp lw 2" >> ./results-sale/plotcmd
###end
###echo "\n" >> ./results-sale/plotcmd
###
###endif
###
###echo "pingthrput.out 6 ./xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size.true a ./xmit-$rate1-$length1-$rate2-$length2-$period-$pack_size-norm.true 1 1.5 thr-$length1-$rate1-$length2-$rate2.true $period $flowstcp" >> ./results-sale/norm
###





mv one.netMix.Mix* data* mix
cd mix
#gnuplot mix.gp
#gv mix.ps &

cd ..



     end
     #pack_size
    end
    #period
   end
   #length2
  end
  #rate2
 end
 #length1
end
#rate1


#rm bin file
rm -f ./ascii/file.bin























