#!/bin/bash
# 2017-01-26

# Various stats from UoB style exam + hw results.
#
#		    $1		$2		$3			$4				$5				$6	    $7
# Use as: uobestat file [no of A questions] [no of B questions] [no of HW sheets] [total achievable marks from HW's combined] [percentage of HW] <options>
# !!! It is always assumed that students can achieve 100 marks on the exam. !!!
# options are listed if no correct number of them given
# File is of the format Cand.No;desk;A1;A2;A3;A4;A5;B1;B2;HW1;HW2;comment
# only lines starting with a number are considered.
#

if [ "$7" = "a" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" 'BEGIN {
  printf("C.no	desk	")
  for (i=1; i<(1+ano); i+=1) printf("A"i"	");
  print("Sum A")
 }
 {
  if ($1 + 0 == $1) {
   asum=0
   printf($1"	"$2"	")
   for (i=3; i<(3+ano); i+=1) {
    asum=asum+$i
    printf($i"	")
   }
   print(asum)
  };
 }'

elif [ "$7" = "b" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" -v bno="$3" 'BEGIN {
  printf("C.no	desk	")
  for (i=1; i<(1+bno); i+=1) printf("B"i"	");
  print("Sum B")
 }
 {
  if ($1 + 0 == $1) {
   bsum=0
   printf($1"	"$2"	")
   for (i=(3+ano); i<(3+ano+bno); i+=1) {
    bsum=bsum+$i
    printf($i"	")
   }
   print(bsum)
  };
 }'

elif [ "$7" = "w" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" -v bno="$3" -v hwno="$4" 'BEGIN {
  printf("C.no	desk	")
  for (i=1; i<(1+hwno); i+=1) printf("HW"i"	");
  print("Sum HW")
 }
 {
  if ($1 + 0 == $1) {
   hwsum=0
   printf($1"	"$2"	")
   for (i=(3+ano+bno); i<(3+ano+bno+hwno); i+=1) {
    hwsum=hwsum+$i
    printf($i"	")
   }
   print(hwsum)
  };
 }'

elif [ "$7" = "abs" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" -v bno="$3" -v hwno="$4" -v hwmax="$5" -v hwpc="$6" 'BEGIN {
  printf("C.no	desk		")
  for (i=1; i<(1+ano); i+=1) printf("A"i"	");
  printf("	")
  for (i=1; i<(1+bno); i+=1) printf("B"i"	");
  printf("	")
  for (i=1; i<(1+hwno); i+=1) printf("HW"i"	");
  printf("	")
  print("Sum A	Sum B	Sum HW		Sum")
 }
 {
  if ($1 + 0 == $1) {
   asum=0
   bsum=0
   hwsum=0
   printf($1"	"$2"		")
   for (i=3; i<(3+ano); i+=1) {
    asum=asum+$i
    printf($i"	")
   }
   printf("	")
   for (i=(3+ano); i<(3+ano+bno); i+=1) {
    bsum=bsum+$i
    printf($i"	")
   }
   printf("	")
   for (i=(3+ano+bno); i<(3+ano+bno+hwno); i+=1) {
    hwsum=hwsum+$i
    printf($i"	")
   }
   printf("	")
   print(asum"	"bsum"	"hwsum"		"(asum+bsum)*(1-hwpc/100)+hwsum*hwpc/hwmax)
  };
 }'

elif [ "$7" = "s" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" -v bno="$3" -v hwno="$4" -v hwmax="$5" -v hwpc="$6" 'BEGIN {
  print("C.no	desk	Sum")
 }
 {
  if ($1 + 0 == $1) {
   sum=0
   hwsum=0
   for (i=3; i<(3+ano+bno); i+=1) sum=sum+$i;
   for (i=3+ano+bno; i<(3+ano+bno+hwno); i+=1) hwsum=hwsum+$i;
   printf($1"	"$2"	")
   print(sum*(1-hwpc/100)+hwsum*hwpc/hwmax)
  };
 }'

elif [ "$7" = "v" ]; then
 cat "$1" |
 awk -F\; -v ano="$2" -v bno="$3" -v hwno="$4" -v hwmax="$5" -v hwpc="$6" 'BEGIN {
  for (i=3; i<(3+ano+bno+hwno); i+=1) su[i]=0;
  nos=0
  for (i=1; i<=ano; i+=1) printf("A"i"	");
  for (i=1; i<=bno; i+=1) printf("B"i"	");
  for (i=1; i<=hwno; i+=1) printf("HW"i"	");
  print("Sum A	Sum B	Sum HW	Sum	No of students")
 }
 {
  if ($1 + 0 == $1) {
   for (i=3; i<(3+ano+bno+hwno); i+=1) su[i]=su[i]+$i;
   nos=nos+1
  };
 }
 END {
  for (i=3; i<(3+ano+bno+hwno); i+=1) av[i]=su[i]/nos;
  suma=0
  sumb=0
  sumhw=0
  for (i=3; i<(3+ano+bno+hwno); i+=1) {
   printf("%3.2f %s",av[i],"	")
  }
  for (i=3; i<(3+ano); i+=1) {
   suma=suma+av[i]
  }
  for (i=(3+ano); i<(3+ano+bno); i+=1) {
   sumb=sumb+av[i]
  }
  for (i=(3+ano+bno); i<(3+ano+bno+hwno); i+=1) {
   sumhw=sumhw+av[i]
  }
  printf("%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.0f\n",suma,"	",sumb,"	",sumhw,"	",(suma+sumb)*(1-hwpc/100)+sumhw*hwpc/hwmax,"	",nos)
 }'

elif [ "$7" = "h" ]; then

 elej=${1%.*}
 #k=v3$elej`date +%N`
 k=v3$elej`date +%y%m%d_%H%M%S`_`strings /dev/urandom | tr -dc a-z-0-9 | tr -d - | head -c5`
 mkdir /tmp/$k
 chmod 700 /tmp/$k/
 gnufile=/tmp/$k/"$elej"_gnu.txt
 rawfile=/tmp/$k/"$elej"_raw.csv
 hwfile=/tmp/$k/"$elej"_hw.csv
 rawfilehw=/tmp/$k/"$elej"_raw_hw.csv
 scaledfile=/tmp/$k/"$elej"_raw_scaled.csv
 scaledfilehw=/tmp/$k/"$elej"_raw_scaled_hw.csv

 cat "$1" |
 awk -F\; ' {
  if ($1 + 0 == $1) {
   sum=0
   for (i=3; i<(3+ano+bno); i+=1) sum=sum+$i;
   print(sum)
  };
 }' ano="$2" bno="$3" > "$rawfile"

 msarg=`echo "markscaler \"$8\" \"$9\" $rawfile"`
 eval "$msarg"

 nol=`wc -l "$rawfile" | awk '{ print($1) }'`

 #Now let's add HW's in
 #hwfile has the hwsums only
 cat "$1" |
 awk -F\; ' {
  if ($1 + 0 == $1) {
   hwsum=0
   for (i=(3+ano+bno); i<(3+ano+bno+hwno); i+=1) hwsum=hwsum+$i;
   print(hwsum)
  };
 }' ano="$2" bno="$3" hwno="$4" > "$hwfile"

 paste -d \	 "$rawfile" "$hwfile" |

 awk -v hwmax="$5" -v hwpc="$6" ' {
  whw=$1*(1-hwpc/100)+$2*hwpc/hwmax
  print(whw)
 }' > "$rawfilehw"

 paste -d \	 "$scaledfile" "$hwfile" |
 awk -v hwmax="$5" -v hwpc="$6" ' {
  whw=$1*(1-hwpc/100)+$2*hwpc/hwmax
  print(whw)
 }' > "$scaledfilehw"

 paste -d \	 "$scaledfilehw" "$rawfilehw" |
 
 sort -g |

 awk -F $'\t' -v nol="$nol" 'BEGIN {
 print("Scaled	   %	Raw		"nol" students")
 raws=0
 scs=0
 fl=0
 th=0
 sii=0
 si=0
 f=0
 hf=0
 }
 {
 printf("%3.0f%s%3.0f%s%3.2f\n",$1,"	",100*NR/nol,"%	",$2)
 scs=scs+$1
 raws=raws+$2
 if ($1 >= 84.5 ) hf=hf+1;
 if ($1 >= 69.5 && $1 < 84.5) f=f+1;
 if ($1 >= 59.5 && $1 < 69.5) si=si+1;
 if ($1 >= 49.5 && $1 < 59.5) sii=sii+1;
 if ($1 >= 39.5 && $1 < 49.5) th=th+1;
 if ($1 < 39.5) fl=fl+1;
 }
 END {
 print("Averages (of "NR" students):")
 printf("%3.2f%s%3.2f\n",scs/NR,"		",raws/NR)
 print("")
 print("Classes:")
 printf("%s%3.0f%s%3.0f%s\n","85+:	",hf,"	",hf/NR*100,"%")
 printf("%s%3.0f%s%3.0f%s\n","1:	",f,"	",f/NR*100,"%")
 printf("%s%3.0f%s%3.0f%s\n","2i:	",si,"	",si/NR*100,"%")
 printf("%s%3.0f%s%3.0f%s\n","2ii:	",sii,"	",sii/NR*100,"%")
 printf("%s%3.0f%s%3.0f%s\n","3:	",th,"	",th/NR*100,"%")
 printf("%s%3.0f%s%3.0f%s\n","fail:	",fl,"	",fl/NR*100,"%")
 }'

 cat "$scaledfilehw" |

 awk -F\; 'BEGIN {
  for (i=0; i<=100; i+=1) h[i]=0;
 }
 {
  for (i=0; i<=100; i+=1) {
   if ($1 >= i-0.5 && $1 < i+0.5)
    h[i]=h[i]+1 ;
  }
 }
 END {
  for (i=0; i<=100; i+=1) print(i" "h[i]" ");
 }' > "$gnufile"

gnuplot -persist <<EOF
set xrange [-1:101]
set xtics 5
set style fill solid 0.3
set xtics rotate out
plot "$gnufile" using 1:2 with boxes notitle
EOF

 l=1
 while [ -n "$l" ]; do
  sleep 1
  l=`lsof 2>/dev/null +d /tmp/$k`
 done

 #read -n1 -s
 rm -f -R /tmp/$k

else

 echo "Use as: uobestat <file> [no of A questions] [no of B questions] [no of HW sheets] [total achievable marks from HW's combined] [percentage of HW] <options>"
 echo "File must be of the format Cand.No;desk;A1;A2;A3;A4;A5;B1;B2;HW1;HW2;comment"
 echo "!!! It is always assumed that students can achieve 100 marks on the exam. !!!"
 echo "Only lines starting with a number are considered. Field separator must be ; ."
 echo "Options are (no - please):"
 echo "a	print sum of section A marks for individual students"
 echo "b	print sum of section B marks for individual students"
 echo "w	print sum of homework marks for individual students"
 echo "s	print total sum for individual students"
 echo "abs	print all four above"
 echo "v	print averages"
 echo "h	show scaled histogram"
 echo "		needs \"f_1 f_2 ... f_k\" \"t_1 t_2 .. t_k\""
 echo "		where k is any integer at least 2, f_1 is the smallest possible raw mark, f_1...f_k is a strictly increasing sequence, f_k is the highest possible raw mark,"
 echo "		t_1...t_k is a non-decreasing sequence of scaled marks that correspond in order to the raw marks f_1...f_k."
 echo "		Only integers are allowed in scaling scheme."
 echo "		Scaling is only performed on the exam marks, homework is added afterwards."
fi

