; called by marker

set sym = $_
set act = $_


; get optionnal arg
if ($act s= "dra") then         ; draw
   set col = $_
endif

; get list of coord in ff1[] and ff2[]
set np = 0
if ($sym s= "Symm") then      ; get all coord in one bag (ff1 and ff2 are equiv)
   while ($arg)  ; make the array of diff. coord in arg_list
      set ff = $_
      set found = (1==0)
      for i = 1 to $np
         set found = ($found | (abs($ff-$ff1[$i])<$tol_align) )   ; found ?
      endfor
      if (!$found) set np = (%+1)   set ff1[$np] = $ff  set ff2[$np] = $ff ; if not found, add it to the list
   endwhile
else
   while ($arg)  ; make the array of diff. coord in arg_list
      set np = (%+1) set ff1[$np] = $_ set ff2[$np] = $_ 
   endwhile
endif


; then parse
if ($act s= "dra") then		; draw
   for i = 1 to $np
   for j = 1 to $np
   set f1 = $ff1[$i]
   set f2 = $ff2[$j]
   set ss = $scolor    scolor (head($col))
      Showline 1 (ptoi($f1,2,1)) $si2_2d (ptoi($f1,2,1))
      Showline (ptoi($f2,2,2)) 1 (ptoi($f2,2,2)) $si1_2d
   endfor
   endfor
   scolor $ss
elsif ($act s= "sho") then		; show related peaks
   refmacro 1
   for i = 1 to $np
   for j = 1 to $np
   set f1 = $ff1[$i]
   set f2 = $ff2[$j]
      find att 2 $f1 $f2
      if ($pk_fnd_dst <= $tol_align ) then
        print ("Peak";$pk_fnd) 
        set pk = (tail(tail(tail($att[$pk_fnd]))))
        if (tolower(head($pk)) s! "unk" | tolower(head(tail($pk))) s! "unk")  alert ("Peak"; $pk_fnd; "already assigned")
;        center (ptoi($f1,2,1)) (ptoi($f2,2,2))
;        Showline 1 (ptoi($f1,2,1)) $si2_2d (ptoi($f1,2,1))
;        Showline (ptoi($f2,2,2)) 1 (ptoi($f2,2,2)) $si1_2d
;        alert "Next"
        show_att $pk_fnd
      endif
   endfor
   endfor
   refmacro 0
elsif ($act s= "cre") then		; create missing peaks
   refmacro 1
   for i = 1 to $np
   for j = 1 to $np
   set f1 = $ff1[$i]
   set f2 = $ff2[$j]
      find att 2 $f1 $f2
      if ($pk_fnd_dst > $tol_align ) then
        center (ptoi($f1,2,1)) (ptoi($f2,2,2))
        Showline 1 (ptoi($f1,2,1)) $si2_2d (ptoi($f1,2,1))
        Showline (ptoi($f2,2,2)) 1 (ptoi($f2,2,2)) $si1_2d
        gm/al_yesno ('Do you want to create a new peak at : F1'; $f1; "F2"; $f2)
        if ($returned s= 'yes') then
           @add_att $f1 $f2
        endif
      endif
   endfor
   endfor
   refmacro 0
elsif ($act s= "add") then		; add to build_list related peaks
   for i = 1 to $np
   for j = 1 to $np
   set f1 = $ff1[$i]
   set f2 = $ff2[$j]
      find att 2 $f1 $f2           ; get coord
      if ($pk_fnd_dst <= $tol_align ) then   ; found
        if (index($build_list,$pk_fnd) == 0) then       ; not in list yet
          print $pk_fnd
          set pk = (tail(tail(tail($att[$pk_fnd]))))
          if (tolower(head($pk)) s! "unk" | tolower(head(tail($pk))) s! "unk") then
            gm/al_yesno ("Peak"; $pk_fnd; "already assigned, do you wish to add it ?")
            if ($returned s= "yes") set build_list := ($build_list;$pk_fnd)
          else
            set build_list := ($build_list;$pk_fnd)
          endif
        endif
      endif
   endfor
   endfor
else
   Print "Command unknown"
endif
