#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#  sg2ishi.c
#  readme
#  sample01.mgt
#
echo shar: "Now extracting shell archive for sg2ishi files.."
#
if test -f 'sg2ishi.c'
then
	echo shar: "will not overwrite existing file 'sg2ishi.c'"
else
cat << \SHAR_EOF > 'sg2ishi.c'
/**********************************************************************/
/*   Copyright  John  Unger,  all  rights  reserved.  Permission  is  */
/*   given  to  modify  and  distribute  this source code as long as  */
/*   proper  credit  is  given.  In using this software you agree to  */
/*   spirit   of  this  copyright.  No  guarantee  is  made  of  the  */
/*   correctness of this software, and it is used at your own risk.	 */
/**********************************************************************/

/*
sg2ishi, Version 0.5, 12/17/92

program to convert SmartGo (mgt) go files to Many Faces of Go files
*/
  
#include <stdio.h>
#include <ctype.h>
  
#define TRUE 1
#define FALSE 0
  
void main(argc, argv)
int argc;
char **argv;
{
    FILE *in_file, *out_file;
    int k;
    int j=1, i=1;
    int first=FALSE;
    int setboard=FALSE;
    char ch;
    char tok1, tok2;
    if (argc != 3)
    {
        fprintf(stderr,"\t\tTo use the program type:\n");
        fprintf(stderr,"\t\tsg2ishi in_file out_file\n");
        exit(0);
    }
  
    if ((in_file = fopen(argv[1], "rt")) == NULL)
    {
        fprintf(stderr,"%s is unable to open %s for input\n", argv[0], argv[1]);
        exit(0);
    }
    if ((out_file = fopen(argv[2], "wt")) == NULL)
    {
        fprintf(stderr, "%s is unable to open %s for output\n", argv[0], argv[2]);
        exit(0);
    }
  
    while((ch = fgetc(in_file)) != EOF)
    {
    	if (ch == '(')
    		fprintf(out_file,"EVENT %d\n", j++);
    		
        if (isupper(ch))
        {
            tok2 = ' ';
            if (first == TRUE)
            {
                tok2 = ch;
                first = FALSE;
                continue;
            }
            tok1 = ch;
            first = TRUE;
            continue;
        }
  
        if (ch == '[')
        {
            switch (tok1)
            {
                case 'A':
                {
                    if ((tok2 == 'B'))
                    {
                        fputs("SETUP B ", out_file);
                        
                        while((ch = fgetc(in_file)) != '\n')
                        {
                            if ((ch == '[') || (ch == ']'))
                                continue;
                            if (ch >= 'i')
                                ch++;
                            fputc(ch, out_file);
                            fprintf(out_file,"%d ",116-fgetc(in_file));
                        }
                        fputc('\n', out_file);
                    }
                    
                    else if ((tok2 == 'W'))
                    {
                        fputs("SETUP W ", out_file);
                        
                        while((ch = fgetc(in_file)) != '\n')
                        {
                            if ((ch == '[') || (ch == ']'))
                                continue;
                            if (ch >= 'i')
                                ch++;
                            fputc(ch, out_file);
                            fprintf(out_file,"%d ",116-fgetc(in_file));
                        }
                        fputc('\n', out_file);
                    }
                    break;
                }
  
                case 'B':
                {
                    if (tok2 == 'R')
                    {
                        fputs("COM Black's rank is ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                        fputs("ENDCOM\n", out_file);
                        break;
                    }
                    
                    else if (tok2 == 'L')
                        continue;
                    
                    else
                    {
                        fprintf(out_file,"B %d ", i);
                        while((ch = fgetc(in_file)) != ']')
                        {
                            if (ch >= 'i')
                                ch++;
                            fputc(ch, out_file);
                            fprintf(out_file,"%d\n",116-fgetc(in_file));
                        }
                        i++;  /* increment move number */
                        break;
                    }
                }
  
                case 'C':
                {
                    fputs("COM\n", out_file);
                    while((ch = fgetc(in_file)) != ']')
                        fputc(ch, out_file);
                    fputc('\n', out_file);
                    fputs("ENDCOM\n", out_file);
                    break;
                }
  
                case 'D':
                {
                    if ((tok2 == 'T'))
                    {
                        fputs("DATE ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                    }
                    break;
                }
  
                case 'E':
                {
                    fputs("COM\n", out_file);
                    while((ch = fgetc(in_file)) != ']')
                        fputc(ch, out_file);
                    fputc('\n', out_file);
                    fputs("ENDCOM\n", out_file);
                    break;
                }
  
                case 'K':
                {
                    if ((tok2 == 'M'))
                    {
                        fputs("KOMI ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                    }
                    break;
                }
  
                case 'G':
                {
                    if (tok2 == 'N')
                    {
                        fputs("COM\n", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                        fputs("ENDCOM\n", out_file);
                        break;
                    }
                    else
                        break;
                }
  
                case 'L':
                {
                    fputs("MARK ", out_file);
                    k = 0;
                    
                    while((ch = fgetc(in_file)) != '\n')
                    {
                        if ((ch == '[') || (ch == ']'))
                            continue;
                        if (ch >= 'i')
                            ch++;
                        fputc(k+97, out_file);
                        fputc('@', out_file);
                        fputc(ch, out_file);
                        fprintf(out_file,"%d ",116-fgetc(in_file));
                        k++;
                    }
                    fputc('\n', out_file);
                    break;
                }
                    
                case 'M':
                {
                    fputs("MARK ", out_file);
                    
                    while((ch = fgetc(in_file)) != '\n')
                    {
                        if ((ch == '[') || (ch == ']'))
                            continue;
                        if (ch >= 'i')
                            ch++;
                        fputc('x', out_file);
                        fputc('@', out_file);
                        fputc(ch, out_file);
                        fprintf(out_file,"%d ",116-fgetc(in_file));
                    }
                    fputc('\n', out_file);
                    break;
                }
                    
                case 'N':
                {
                    fputs("COM\n", out_file);
                    while((ch = fgetc(in_file)) != ']')
                        fputc(ch, out_file);
                    fputc('\n', out_file);
                    fputs("ENDCOM\n", out_file);
                    break;
                }
  
                case 'P':
                {
                    switch (tok2)
                    {
                        case 'B':
                        {
                            fputs("BLACK ", out_file);
                            while((ch = fgetc(in_file)) != ']')
                                fputc(ch, out_file);
                            fputc('\n', out_file);
                            break;
                        }
  
                        case 'C':
                        {
                            fputs("PLACE ", out_file);
                            while((ch = fgetc(in_file)) != ']')
                                fputc(ch, out_file);
                            fputc('\n', out_file);
                            break;
                        }
  
                        case 'W':
                        {
                            fputs("WHITE ", out_file);
                            while((ch = fgetc(in_file)) != ']')
                                fputc(ch, out_file);
                            fputc('\n', out_file);
                            break;
                        }
                        default:
                            break;
                    } /* end switch */
                    break;
                }
  
                case 'R':
                {
                    if ((tok2 == 'E'))
                    {
                        fputs("RESULT ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                    }
                    break;
                }
  
                case 'S':
                {
                    if ((tok2 == 'Z'))
                    {
                        fputs("BOARDSIZE ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                        setboard = TRUE;
                    }
                    break;
                }
  
                case 'U':
                {
                    fputs("COM\n", out_file);
						  fputs("Game entered by: ", out_file);
                    while((ch = fgetc(in_file)) != ']')
                        fputc(ch, out_file);
                    fputc('\n', out_file);
                    fputs("ENDCOM\n", out_file);
                    break;
                }
  
                case 'W':
                {
                    if (tok2 == 'R')
                    {
                        fputs("COM White's rank is ", out_file);
                        while((ch = fgetc(in_file)) != ']')
                            fputc(ch, out_file);
                        fputc('\n', out_file);
                        fputs("ENDCOM\n", out_file);
                        break;
                    }
                    
                    else if (tok2 == 'L')
                        break;
                    
                    else
                    {
                        fprintf(out_file,"W %d ", i);
                        while((ch = fgetc(in_file)) != ']')
                        {
                            if (ch >= 'i')
                                ch++;
                            fputc(ch, out_file);
                            fprintf(out_file,"%d\n",116-fgetc(in_file));
                        }
                        i++; /* increment move number */
                        break;
                    }
                }
                default:
                    break;
                    
            } /* end switch */
  
            first = FALSE;
  
        } /* end if */
  
    } /* end while */
  
    if (!setboard)
    {
        fprintf(stderr,"The size of the board was not found in the input file\n");
        fprintf(stderr,"I am assuming that the game was played on a 19 x 19 board.\n\n");
    }
    fprintf(stderr,"I have successfully translated %s from Smart-Go format\n",argv[1]);
    fprintf(stderr,"to %s in Ishi Stardard format.\n", argv[2]);
    fclose(in_file);
	fclose(out_file);
}
SHAR_EOF
fi
if test -f 'readme.go'
then
	echo shar: "will not over-write existing file 'readme.go'"
else
cat << \SHAR_EOF > 'readme.go'
I wrote sg2ishi to convert go game files written in the
popular "mgt" format into ones that would be compatible with
the Ishi-standard format used by the Many Faces of Go program.
Actually, sg2ishi uses the subset of Smart-Go commands that
are supported by mgt and that have equivalent commands in the
Ishi-standard formt. (See table 1 below..)

Certain "features" of the mgt format could not be incorporated
into the Ishi-standard as it is represented by Many Faces of GO.

1. If multiple games or problems are contained in one mgt file,
Many Faces of Go will play only the first "event" in that file
after it is converted to the Ishi-standard format by sg2ishi.
This problem is apparently a limitation of the way that Many
Faces of Go incorporates the Ishi-standard format.

2. Variations are not incorporated into sg2ishi yet. The
program will only work for single game records in this version. 

3. The comments in mgt formatted files sometimes include
references to the goban's coordinates using the standard A->T
and 1->19 coordinate system. The board used by Many Faces of Go
does not show coordinates. It's on my "to-do" list to abstract
such information from the comments and actually mark the
points on the goban referred to by the comments.

4. Some mgt files contain problems or partially-played game
situations starting out with some number of stones already
played on the goban. If the mgt file uses the AB (AddBlack) or
AW (AddWhite) notation, then sg2ishi will place all of the
stones on the board at once. However, if the mgt file used the
format:

Black[pd][dp][op][lp]
;
White[qp][dc][po][pl]
;	

the stones will be placed with eight sequential "moves" when
the game is played by Many Faces of Go.


TABLE 1.

mgt notation supported by sg2ishi:
--------------------------------------
AB: add black stones     [point list] 
AW: add white stones     [point list] 
B : Black move           [move]       
BR: Black's rank         [text]       
C : Comment              [Text]       
DT: date                 [text]       
EV: event (tournament)   [text]       
GC: game comment         [text]       
GN: game name            [text]       
KM: komi                 [real]       
L : letters on points    [point list] 
M : marked points        [point list] 
N : Node Name            [Text]       
PB: black player name    [text]       
PC: place                [text]       
PW: white player name    [text]       
RE: result, outcome      [text]       
SZ: board size           [number]     
US: who entered game     [text]
W : White move           [move]       
WR: White's rank         [text]       


mgt notation not supported by sg2ishi
--------------------------------------
AE: add empty stones     [point list] 
GM: game                 [number]     
HA: handicap             [number]     
PL: player to play first [color]      
RO: round                [text]       
SO: source (book, journa [text]       
TM: time limit per playe [text]       
VW: view                              


This program is in a true beta version; I have tested it on
mgt files that I have downloaded from milton.u.washington.edu
and that were created by a variety of players, but I am sure
that there are some input files that will break the program
somewhere. All suggestions, comments, etc., are solicited and
should be sent via email to:
junger@mtn.er.usgs.gov
or, if email is unavailable, I read the Usenet group:
rec.games.go regularly.

As you can see from the source code, I used a very
straightforward approach to writing this program; no tables,
no yacc, no lex, just some switch, case, and if statements. It
should be easy to follow the logic of the code from top to
bottom, as they used to say... Furthermore, nothing in the
program is OS-specific; it should compile and run on any
hardware that has a C compiler. If you want to add some of the
unsupported mgt features, have at it.

The MS-DOS executible in the zipped file was generated with
Borland C++, version 2.0. Since I have not included a Makefile
with the code, perhaps for non-programmers I should add that
to compile sg2ishi on a Unix system you must type the line:

cc sg2ishi.c -o sg2ishi

at the shell prompt. 

The program expects two command line arguements, one for the
input file name and one for the output file name. So to run
the program you type:

sg2ishi mgt_file manyface.go

Note that Many Faces of Go needs the suffix .go so that it can
recognize the file as a game file.
SHAR_EOF
fi
if test -f 'sample01.mgt'
then
	echo shar: "will not over-write existing file 'sample01.mgt'"
else
cat << \SHAR_EOF > 'sample01.mgt'
(
;
GaMe[1]
VieW[]
SiZe[19]
Comment[A figment of a test file for sg2ishi
to test some of the features supported mgt's notation
note: this is a nonsense game...]
REsult[with no komi, black wins by 4]
DaTe[December 8, 1992]
PlayerBlack[John Unger]
PlayerWhite[Honinbo]
PlaCe[Hamilton, VA]
USer[Peter Cottontail, 4-dan]
;
AddBlack[jq][kr][lo][lp][lq][mo][no][oo][pn][pp][qp][qr][rr]
AddWhite[lr][mp][mr][np][op][or][pq][pr][qs]
;
Mark[jq][kr][lo][lp][lq][mo][no][oo][pn][pp][qp][qr][rr]
;
Letter[lr][mp][mr][np][op][or][pq][pr][qs]
;
Black[pd]
;
White[cp]
;
Black[dp]
;
White[dc]
;
Black[od]
;
White[po]
;
Black[ld]
;
White[pl]
;
Black[de]
;
White[cd]
;
Black[ed]
;
White[ce]
;
Black[cf]
;
White[ec]
;
Black[ci]
;
White[fd]
;
Black[fe]
;
White[gd]
;
Black[ph]
;
White[qi]
;
Black[nc]
;
White[cn]
)
SHAR_EOF
fi
exit 0
#  end of shell archive

