sub print_sheets {
  my $retval;
  my $error=6;
  local($debug=1);
  #           0  1  2  3  4  5  6  7
#  local(@run=(0, 0, 1, 0, 0, 0, 0, 0));
  local(@run=(0, 1, 1, 1, 1, 1, 1, 1));

  if (Constants->get(REGISTRATION_OPEN)) {
    Error::RegistrationStillOpen();
  }
 
  # ################ PS functions
  
  # String dance_title
  # Should print out String in 12 or 15 tb.
  
  # twocol
  # Should modify /shiftover to handle two columns per page.
  
  # String go1
  # should print go1 and rmoveto ( ) stringwidth over, CRLF if necessary
  
  # String(name) String(number) go2
  # name and number data for printing registration money sheets
  
  # ################
  
  # out1:
  # List of couples' numbers & names in each event, sorted by number.
  # Updates to this list are carried to MC and Scrut from Reg Desk.
  # Copies to Reg Desk, MC, Scrut.
  #
  # out2:
  # List of couples' numbers in each event, sorted by number, for
  # Programme.

  General::log("Starting do_sheets12 (error=$error)") if $debug;
  if ($retval=&do_sheets12()) {

    $error--;
    # List of all couples, sorted by number.  Copies to MC and Reg Desk.
    General::log("Starting do_sheets3 (error=$error)") if $debug;
    if ($retval=&do_sheets3()) {
      
      $error--;
      # List of all couples, sorted by name.  Copies to Reg Desk.
      General::log("Starting do_sheets4 (error=$error)") if $debug;
      if ($retval=&do_sheets4()) {
	
	$error--;
	# List of all couples, sorted by school, with $ data.  Copies to
	# Reg Desk.
	General::log("Starting do_sheets5 (error=$error)") if $debug;
	if ($retval=&do_sheets5()) {

	  $error--;
	  # List of events, by round in order.  Copies to MC, DJ, Scrut, and
	  # Reg Desk.
	  General::log("Starting do_sheets6 (error=$error)") if $debug;
	  if ($retval=&do_sheets6()) {
	    
	    $error--;
	    # List of couples, sorted by name, including partners and
	    # registered dances.  Copies to team captains.
	    General::log("Starting do_sheets7 (error=$error)") if $debug;
	    return(&do_sheets7());
	  }
	}
      }
    }
  }
  return($error);
} # print_sheets()
 
# ################################################################ #

sub do_sheets12 {
  my ($i, $j, $v, $s, $coup, $master_ref, @data);

  if ($run[1]==0 && $run[2]==0) {
    General::log("run[]=0; punting...");
    return(1);
  }
  # out1:
  # List of couples' numbers & names in each event, sorted by number.
  # Updates to this list are carried to MC and Scrut from Reg Desk.
  # Copies to Reg Desk, MC, Scrut.
  
  # out2:
  # List of couples' numbers in each event, sorted by number, for
  # Programme.
  
  open(OUT1, ">".Constants::get(LIST1)) ||
    Error::CantOpenFile(Constants::get(LIST1));
  open(OUT2, ">".Constants::get(LIST2)) ||
    Error::CantOpenFile(Constants::get(LIST2));
  General::PrintTemplate(Constants::get(TEMPLATE_LIST1), \*OUT1) ||
    Error::CantOpenFile(Constants::get(TEMPLATE_LIST1));
  General::PrintTemplate(Constants::get(TEMPLATE_LIST2), \*OUT2) ||
    Error::CantOpenFile(Constants::get(TEMPLATE_LIST2));

  $master_ref=Constants::get(MASTER);
#  print OUT1 "twocol\n";
  for ($i=0;$i<=$#$master_ref;$i++) {
    print OUT1 "(".$$master_ref[$i]->getFullName().") dance_title\n";
    print OUT2 "(".$$master_ref[$i]->getFullName().") dance_title\n";
    
    $data_ref=Database::getNumberSortedCouplesByDance($master[$i]);
    if ($#$data_ref>-1) {
      for ($j=0;$j<=$#$data_ref;$j++) {
	$coup=$$data_ref[$j];
	print OUT1 "(".$coup->getLeader()->getNumber().") stab (".
	  $coup->getNames().") return\n";
	print OUT2 "(".$coup->getLeader()->getNumber().") go1\n";
      }
    } else {
      print OUT1 "(-none-) return\n";
    }
    print OUT1 "shiftover\n";
    print OUT1 "setup-norm\n";

    print OUT2 "enter\n";
#    print OUT2 "enter\n\enter";
  }
  
  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT1) || return(0);
  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT2) || return(0);
  close(OUT1);
  close(OUT2);
  
  return(1);
} # do_sheets12()
 
sub do_sheets3() {
  my ($ret_ref, $coup, $i);
 
  # List of all couples, sorted by number.  Copies to MC and Reg Desk.
  if ($run[3]==0) {
    General::log("run[]=0; punting...");
    return(1);
  }
 
  open(OUT, ">".Constants::get(LIST3)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_LIST3), \*OUT) || return(0);
  
  print OUT "twocol";
  $ret_ref=Database::getNumberSortedCouples();
  if ($#$ret_ref>-1) {
    for ($i=0;$i<=$#$ret_ref;$i++) {
      $coup=$$ret_ref[$i];
      print OUT "(".$coup->getLeader->getNumber().") stab (".
	$coup->getNames().")\n";
      if ($coup->getLeader->getSchool() eq $coup->getFollower->getSchool()) {
	print OUT " stab (".$coup->getLeader->getSchool().") return\n";
      } else {
	print OUT " stab (".$coup->getLeader->getSchool()."/".
	  $coup->getFollower->getSchool().") return\n";
      }
    }
  }

  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT) || return(0);
  close(OUT);
  return(1);
} # do_sheets3()

sub do_sheets4 {
  my ($i, $ret_ref, $comp);
  
  # List of all couples, sorted by name.  Copies to Reg Desk.
  if ($run[4]==0) {
    General::log("run[]=0; punting...");
    return(1);
  }
  
  open(OUT, ">".Constants::get(LIST4));
  General::PrintTemplate(Constants::get(TEMPLATE_LIST4), \*OUT) || return(0);
  
  $ret_ref=Database::getNameSortedCompetitors();
  if ($#$ret_ref>-1) {
    for ($i=0;$i<=$#$ret_ref;$i++) {
#    $comp=Competitor->blessme($ret[$i]);
      $comp=$$ret_ref[$i];
      if ($comp->getSchool() == -1) {
	$school=Constants::get(HTML_MSG30);
      } else {
	$school=$comp->getSchoolAbbrev();
      }
      if ($comp->getNumber()==-1) {
	print OUT "(".$comp->getName().") stab tab ($school) return\n";
      } else {
	print OUT "(".$comp->getName().") stab (".$comp->getNumber().
	  ") stab ($school) return\n";
      }
    }
  }
  
  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT) || return(0);
  close(OUT);
  return(1);
} # do_sheets4
 
sub do_sheets5 {
  my ($ret1_ref, $ret2_ref, $i, $j, $school, $comp);

  # List of all couples, sorted by school, with $ data.  Copies to
  # Reg Desk.
  if ($run[5]==0) {
    General::log("run[]=0; punting...");
    return(1);
  }

  open(OUT, ">".Constants::get(LIST5)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_LIST5), \*OUT) || return(0);
  
  $ret1_ref=Database::getSortedSchools();
  if ($#$ret1_ref>-1) {
    for($i=0;$i<=$#$ret1_ref;$i++) {
      $school=$$ret1_ref[$i];

      $ret2_ref=Database::getNameSortedCompetitorsBySchool($school);
      if ($#$ret2_ref>-1) {
	if ($school==-1) {
	  print OUT "\n\n(".Constants::get(HTML_MSG29).") school\n";
	} else {
	  print OUT "\n\n(".$school.") school\n";
	}
	for ($j=0;$j<=$#$ret2_ref;$j++) {
	  $comp=$$ret2_ref[$j];
	  print OUT "(".$comp->getName().") (".$comp->getNumber().") go2\n";
	}
      }
    }
  }
  
  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT) || return(0);
  close(OUT);
  return(1);
} # do_sheets5

sub do_sheets6 {
  # List of events, by round in order.  Copies to MC, DJ, Scrut, and
  # Reg Desk.
  
  # OK, this is too hard to implement at the moment.  :)
  if ($run[6]==0) {
    General::log("run[]=0; punting...");
    return(1);
  }

  return(1);
}

sub do_sheets7 {
  my ($ret1_ref, $ret2_ref, $i, $j, $school, $comp);

  # List of couples by school, sorted by name, including partners and
  # registered dances.  Copies to team captains.
  
  # Right now, only lists number and name.

  # Should also have: partners and registered dances.
  if ($run[7]==0) {
    General::log("run[]=0; punting...");
    return(0); # Not 1.
  }

  open(OUT, ">".Constants::get(LIST7)) || return(0);
  
  $ret1_ref=Database::getSortedSchools();
  
General::log("list7=".Constants::get(LIST7));
  if ($#$ret1_ref>-1) {
    General::PrintTemplate(Constants::get(TEMPLATE_LIST7), \*OUT) || return(0);
    for ($i=0;$i<=$#$ret1_ref;$i++) {
      $school=$$ret1_ref[$i];
      print OUT "\n\npage_setup\n";
      print OUT "(".$school.") school\n";
      $ret2_ref=Database::getNameSortedCompetitorsBySchool($school);
      for ($j=0;$j<=$#$ret2_ref;$j++) {
	$comp=$$ret2_ref[$j];

	$partners_ref=Database::getFollowers($comp);
	if ($#$partners_ref>-1) {
	  for ($k=0;$k<=$#$partners_ref;$k++) {
	    $follower=$$partners_ref[$k];
	    $lschool=$comp->getSchoolAbbrev();
	    $fschool=$follower->getSchoolAbbrev();
	    
	    $coup=Couple->new($comp, $follower);
	    $dances_ref=Database::getDancesByCouple($coup);
	    
	    if ($#$dances_ref==-1) {
	      General::log("-1 sizeof: ".$comp->getName()." ".
			   $follower->getName());
	      next;
	    }
	    
	    $blank_file=0;
	    print OUT "enter\n";
	    print OUT "(".$comp->getNumber()." ) show fsize bold\n";
	    print OUT "(".$comp->getName().") stab tab fsize roman\n";
	    print OUT "(".$follower->getName().") return\n";
	    
	    if ($#dances==0) {
	      print OUT "(".$$dances_ref[0]->getAbbrev().") return\n";
	    } else {
	      print OUT "(".$$dances_ref[0]->getAbbrev().", ) go1\n";
	      for ($l=1;$l<$#$dances_ref;$l++) {
		print OUT "(".$$dances_ref[$l]->getAbbrev().", ) go1\n";
	      }
	      print OUT "(".$$dances_ref[$l]->getAbbrev().") return\n";
	    }
	  }
	}
	
	$partners_ref=Database::getLeaders($comp);
	if ($#$partners_ref>-1) {
	  for ($k=0;$k<=$#$partners_ref;$k++) {
	    $leader=$$partners_ref[$k];
	    $lschool=$leader->getSchoolAbbrev();
	    $fschool=$comp->getSchoolAbbrev();
	    
	    $coup=Couple->new($leader, $comp);
	    $dances_ref=Database::getDancesByCouple($coup);
	    
	    if ($#$dances_ref==-1) {
	      General::log("-1 sizeof: ".$comp->getName()." ".
			   $leader->getName());
	      next;
	    }
	    
	    $blank_file=0;
	    print OUT "enter\n";
	    print OUT "(".$leader->getNumber()." ) show \n";
	    print OUT "(".$leader->getName().") stab tab fsize bold\n";
	    print OUT "(".$comp->getName().") return fsize roman\n";
	    
	    if ($#$dances_ref==0) {
	      print OUT "(".$$dances_ref[0]->getAbbrev().") return\n";
	    } else {
	      print OUT "(".$$dances_ref[0]->getAbbrev().", ) go1\n";
	      for ($l=1;$l<$#$dances_ref;$l++) {
		print OUT "(".$$dances_ref[$l]->getAbbrev().", ) go1\n";
	      }
	      print OUT "(".$$dances_ref[$#$dances_ref]->getAbbrev().") return\n";
	    }
	  }
	}    
      }
      print OUT "shiftover\n";
    }
  }
  
  General::PrintTemplate(Constants::get(PS_END_TEMPLATE), \*OUT) || return(0);
  close(OUT);
  return(0); # not 1
}

1;
