program setuplan;
uses crt, dos;

TYPE
   chset      = array [1..11] of byte;

const
    framechar: chset = ($C9, $BB, $C8, $BC, $CD, $BA, $CB, $CA, $CC, $B9, $CE );
    backgr: byte = $B0;
    headmsg:String[27]  = 'Westwood Studios -- Dune II';
    languages: Array[0..2] of String[8] = ('English ', 'Fran‡ais','Deutsch ');
    strSave: Array[0..2] of String[18] = ('  Save successful ',
                                          'Sauvegarde r‚ussie',
                                          '     Gesichert    ');
    cfgfile: String[8] = 'DUNE.CFG';       


var
    edfile:file;

procedure resetCursor;
begin
  gotoxy(1,25);
end;

procedure cntin;
begin
   writeln;
   writeln;
   write('Press any key to continue. . . ');
   repeat until keypressed;
    ReadKey;
   while keypressed do ReadKey; {Clears the keyboard buffer}
end; {cntin}


function openFile(filename:String) : boolean;
var
  status:integer;
begin
  assign(edfile,filename);
  {$I-}
  reset(edfile,1);
  status:=IOResult;
  if status<>0 then
   begin
    writeln('Error opening "',filename,'".');
    openFile:=false;
    {$I+}
   end
  else openFile:=true;
end; {openFile}

procedure closeFile;
begin
  close(edfile);
  {$I+}
end; {closeFile}

procedure writeByte(address:Integer;data:byte);
var
  actual:word;
begin
  seek(edfile,address);
  blockwrite(edfile,data,1,actual);
end;


function toString(b:byte):String;
var
  strn:String;
begin
  str(b,strn);
  toString:=strn;
end;

procedure drawLineV(x,y,len:integer; c:char);
var count:integer;
begin
   for count:=0 to (len-1) do
    begin
      gotoxy(x,y+count);
      write(c);
    end;
end;

procedure drawLineH(x,y,len:integer; c:char);
var count:integer;
begin
  gotoxy(x,y);
  for count:=0 to len-1 do write(c);
end;

procedure clearBlock(x,y,w,h:integer; c:char);
var count:integer;
begin
  for count:=0 to h-1 do
   begin
     gotoxy(x,y+count);
     drawLineH(x,y+count,w,c);
   end;
end;

procedure drawFrame(x,y,w,h,splith,splitv:integer;fill:boolean );
begin
  gotoxy(x,y);
  write(char(framechar[1]));
  gotoxy(x+w-1,y);
  write(char(framechar[2]));
  gotoxy(x,y+h-1);
  write(char(framechar[3]));
  gotoxy(x+w-1,y+h-1);
  write(char(framechar[4]));
  drawLineH(x+1,y,w-2,char(framechar[5]));
  drawLineH(x+1,y+h-1,w-2,char(framechar[5]));
  drawlineV(x,y+1,h-2,char(framechar[6]));
  drawlineV(x+w-1,y+1,h-2,char(framechar[6]));
  if fill then clearBlock(x+1,y+1,w-2,h-2,' ');

  if (splith>1) then
   begin
     gotoxy(x,y+splith-1);
     write(char(framechar[9]));
     gotoxy(x+w-1,y+splith-1);
     write(char(framechar[10]));
     drawLineH(x+1,y+splith-1,w-2,char(framechar[5]));
   end;

  if (splitv>1) then
   begin
     gotoxy(x+splitv-1,y);
     write(char(framechar[7]));
     gotoxy(x+splitv-1,y+h-1);
     write(char(framechar[8]));
     drawLineV(x+splitv-1,y+1,h-2,char(framechar[6]));
     if (splith>1 ) then
      begin
        gotoxy(splith,splitv);
        write(char(framechar[11]));
      end;
   end;
end; {drawFrame}

procedure drawShadow(x,y,w,h:integer; c:char);
begin
  drawLineV(x+w,y+1,h,c);
  drawLineV(x+w+1,y+1,h,c);
  drawLineH(x+2,y+h,w,c);
end;

procedure drawUI;
var i:byte;
begin
  TextColor(15);
  TextBackground(1);
  clearBlock(1,1,80,24,char(backgr));
  drawFrame(21,5,40,7,3,-1,true);
  TextColor(7);
  TextBackground(0);
  drawShadow(21,5,40,7,char(backgr));
  TextColor(15);
  TextBackground(1);
  gotoxy(27,6);
  write(headmsg);
  for i:=0 to 2 do
   begin
     gotoxy(35,8+i);
     write(toString(i+1) + '. ' + languages[i]);
   end;
  resetCursor;
end;

procedure drawUI2(msg:string);
begin
  TextColor(15);
  TextBackground(1);
  clearBlock(1,1,80,24,char(backgr));
  drawFrame(26,11,30,3,-1,-1,true);
  TextColor(7);
  TextBackground(0);
  drawShadow(26,11,30,3,char(backgr));
  TextColor(15);
  TextBackground(1);
  gotoxy(32,12);
  write(msg);
  resetCursor;
  readKey;
end;


procedure getLan;
var
    key:char;
    lan:byte;
begin
  key:=' ';
  while ((key<>'1') and (key<>'2') and (key<>'3') and (byte(key)<>27)) do
    key:=ReadKey;
  if byte(key)<>27 then
  begin
    lan:=byte(key)-49;
    writeByte(8,lan);
    drawUI2(strSave[lan]);
  end
end;

begin
  clrscr;
  gotoxy(1,1);
  if not (openfile(cfgfile)) then cntin
  else
    if filesize(edfile)<>10 then
     begin
       writeln('"',cfgfile,'" has an incorrect size.');
       cntin;
     end
    else
     begin
       drawUI;
       getLan;
       closeFile;
     end;
  TextColor(7);
  TextBackground(0);
  clrscr;
end.



