
!read names
!read Exindex
exocnt=!itemcnt $exolist
!bound qnum between integer 1 and $exocnt
!bound qcmlevel between integer 1 and 7
!bound random within 0,1 default 1
wims_backslash_insmath=yes
!default format=tex

!if $cmd=reply and $status!=waiting
 error=double_reply
 !exit
!endif

!if $status=$empty
 rank=
 saverank=
 !read oef/exo.init
!endif

nn=!itemcnt $shufflelist
thisexo=!item ($reply_cnt)%$nn+1 of $shufflelist
imagedir=$module_dir/images/$thisexo
deffile=def/$thisexo.def
!if $nn>1 and $status!=waiting
 wims_module_log=$wims_module_log got $thisexo
!endif

!read $deffile

!if $status!=waiting
 !read oef/var.prep
 !read clean
!endif

eqs=
!for i=1 to $eqn
 eqs=!append line $(eq$i) to $eqs
!next i
eqpari=!translate $\
$ to ; in $eqs

!if $cmd=reply and $status=waiting
 !if $rank=back
  rank=$saverank
  saverank=
  !exit
 !endif
 !if $saverank=$empty
  rank=!trim $rank
  !if $rank=$empty
   error=empty_data
   !exit
  !endif
  rank=$[rint($rank)]
  !if NaN isin $rank
   error=bad_data
   !exit
  !endif
  !if $rank<=0
   error=rank_too_small
   !exit
  !endif
  !if $rank>$dim
   error=rank_too_big
   !exit
  !endif
  saverank=$rank
 !else # saverank!=empty
  !for i=1 to $saverank
   v$i=!trim $(v$i)
   !if $(v$i)=$empty
    error=empty_data
    empty_data=$i
    !exit
   !endif
  !next i
  base=
  !read proc.$type
  !if $error!=$empty
   !exit
  !endif
  basepari=!translate $\
$ to ; in $base
  pari_header=eqs=Mat([$eqpari]);base=Mat([$basepari]);ker=matker(eqs)~;
  pari_precision=8
  pariout=!exec pari r1=matrank(eqs);print($dim-r1);\
	print(ker);\
	if(matrank(base)<$saverank,dep=matker(base~)~[1,];print(denominator(dep)*dep));\
	for(i=1,$saverank,t=matrank(concat(ker~,base[i,]~));if(t>$dim-r1,print(i)));
  !distribute lines $pariout into shouldrank,paribase,dep,outer
  dep=!trim $dep
  outer=!trim $outer
  !if $outer!=$empty
   error=outer
   !exit
  !endif
  !if $dep!=$empty
   error=dep
   !exit
  !endif
  !ifval $shouldrank!=$saverank
   error=bad_rank
   !exit
  !endif

  severity=!item $qcmlevel of 0.5,0.7,1,1.4,2,3,4,5,6
  score_got=$[$score_got+1/($severity*$errorcnt+1)]
  !advance score_should
  !advance reply_cnt
  status=done
  !if $reply_cnt>=$qnum
   !ifval $score_should=0
    score_should=1
   !endif
   score=$[max(0,min(10,rint(100*$score_got/$score_should)/10))]
   module_score=$score
   wims_module_log=score $module_score/10
   status=
  !endif
 !endif saverank=empty?
!endif

