#!/bin/bash

# #### mkprob ###
# Utility script for creating USACO TRAINING problem program files.
# Might also be suitable for computing olympiads.
#
# See more:
# - http://cerberus.delos.com:790/usacogate
# - http://www.usaco.org/
#
# Author: Magnus Teekivi (mt.taevasinine.eu)

ID=""  # insert your USACOGATE ID here

if [ $# != 1 ]; then
	echo "usage: $0 FILE" >&2
	exit 1
fi

prog="$(echo $1 | cut -d . -f 1)"
case "$(echo $1 | cut -d . -f 2)" in
	c ) cat << EOC
/*
ID: $ID
LANG: C
PROG: $prog
*/

#include <stdio.h>

int main(int argc, char *argv[])
{
	FILE *in, *out;

	in = fopen("$prog.in", "r");
	out = fopen("$prog.out", "w");

	if (!in || !out) {
		fputs("Failed to open file(s)...\n", stderr);
		return 1;
	}

	fclose(in);
	fclose(out);

	return 0;
}
EOC
		;;
	c++ ) cat << EOC
/*
ID: $ID
LANG: C++
PROG: $prog
*/

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
	ifstream in("$progname.in");
	ofstream out("$progname.out");

	if (!in.is_open() || !out.is_open()) {
		cerr << "Failed to open file(s)." << endl;
		return 1;
	}

	return 0;
}
EOC
		;;
	java ) cat << EOC
/*
ID: $ID
LANG: JAVA
PROG: $prog
*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class $prog {
	public static void main(String[] args) {
		BufferedReader in = null;
		PrintWriter out = null;

		try {
			in = new BufferedReader(new FileReader("$prog.in"));
			out = new PrintWriter("$prog.out");

		} catch (IOException e) {
			System.err.println("Failed to perform I/O: " + e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				System.err.println("Failed to close file(s): " + e.getMessage());
			}
		}
	}
}
EOC
		;;
	pas ) cat << EOC
{
ID: $ID
LANG: PASCAL
PROG: $prog
}

program $prog;

var fin, fout: text;

begin
	assign(fin, '$prog.in');
	reset(fin);

	assign(fout, '$prog.out');
	reset(fout);

	close(fin);
	close(fout);
end.
EOC
		;;
	* ) echo "Invalid file type entered..." >&2; exit 2; ;;

esac > $1
