program baze;


 type  str=string;
       mem=record
            nam:str;
            rol:str;
       end;
       kamp=array [1..10]of mem;
       art=record
            nam:str;
            year:integer;
            napr:str;
            sost:kamp;
            lyear:integer;

       end;
       typ=file of art;
 var f,g:typ;
     o:boolean;
     but:char;

{---------------------------------------------BI}
 procedure BI;
  begin
  writeln('   ERROR 32: Bad insertion');
  end;


{------------------------------------------UNTER}
 procedure unter;
  begin
   writeln(' Press ENTER to continue');
   readln;
  end;

{YYYR---VVOD-GODA-V-PEREMENNUU-YEAR-------------}
 procedure yyyr(var year:integer;st:str;b:boolean);
 var l:boolean;
     i:integer;
     s:str;
 begin
   repeat
     write(' ',st);
     if b then write(' (',year,')');
     write(':'); readln(s); l:=false;
      if s='n/a' then year:=0 else
       begin
        for i:=1 to length(s) do if ((s[i]>'9')or(s[i]<'0'))then l:=true;
        year:=0;
        if not l then for i:=length(s) downto 1 do
         year:=year+(ord(s[i])-ord('0'))*round(exp(ln(10)*(length(s)-i)))
          else BI;
       end;
    until not l;
 end;


{SREED---BISTRII-VIVOD-SODENGANIA-FAILA-F-------}
 procedure sreed(var f:typ);
  var i:byte;
      kim:art;
  begin
   i:=0;
   reset(f);
   while not eof(f) do
    begin
    inc(i);
    read(f,kim);
    writeln('#',i,': ',kim.nam);
    end;
   close(f);
   Unter;
   end;

{MEMAM---ZAPOLNENIE-GRAFI-MEMBERS----------------}
 procedure memam(var ksost:kamp; b:boolean);
  var i:integer;
  begin
    i:=1;
    repeat
      write(' /Name ');
      if b then write('(',ksost[i].nam,')');
      write(': ');
      readln(ksost[i].nam);
       if ksost[i].nam<>'n/a' then
         begin
         write('   /Role');
         if b then write(' (',ksost[i].rol,')');
         write(': ');
         readln(ksost[i].rol);
         end;
      inc(i);
    until (ksost[i-1].nam='n/a')or(i=10);
    if ksost[i-1].nam='n/a' then for i:=i to 10 do ksost[i].nam:='n/a';
  end;


{MOV---COPIROVANIE-IZ-F-V-G----------------------}
 procedure mov(var f,g:typ);
  var kim:art;
  begin
   rewrite(f); reset(g);
   while not eof(g) do
    begin
    read(g,kim); write(f,kim);
    end;
   close(g);
  end;



{CHAA---IZMENENIE-ZAPISI-FAILA-F-----------------}
 procedure chaa(var f:typ);
  var kim:art;
      i,k,j:integer;
      s:str;
      l:boolean;
  begin
   writeln('What is the number?');
   write('/');
   readln(k);
   reset(f);
   rewrite(g);
   j:=1;
   while not eof(f) do
   begin
    read(f,kim);
    if j<>k then write(g,kim)
     else
     begin
    writeln('   Write changed data:');
    writeln('  n/a is a variant for name, napravlenie,members. Other must be zeros.');
    write(' Artists name (',kim.nam,'):'); readln(kim.nam);
    write(' Napravlenie (',kim.napr,'):'); readln(kim.napr);
    yyyr(kim.year,'First year',true);
    yyyr(kim.lyear,'Last year',true);
    writeln(' Members: ');
    memam(kim.sost,true);
    write(g,kim)
    end;
    inc(j);
    end;
    close(g);close(f);
    mov(f,g); close(f);
   end;


{DELA---UDALENIE-ZAPISI-IZ-FAILA-F-------------}
 procedure dela(var f:typ);
  var i,k:byte;
      kim:art;
  begin
   writeln('   What is the number of recoed to delete?');
   writeln(' use short read for i about Number');
   write('/');
   readln(k);
   reset(f); rewrite(g); i:=1;
   while not eof(f) do
    begin
    read(f,kim);
     if (i<>k) then
       write(g,kim);
    inc(i);
    end;
   close(f); close(g);
   if i<=k then BI
    else
    begin
     mov(f,g);
     unter;
     close(f);
    end;
  end;


{ADA---DOBAVLENIE-NOVOI-ZAPISI-V-F--------------}
 procedure ada(var f:typ);
  var kim:art;
      i:integer;
      s:str;
      l:boolean;
  begin
   mov(g,f);
    writeln('   Write new data:');
    writeln('  n/a is a variant for name, napravlenie,members. Other must be zeros.');
    write(' Artists name: '); readln(kim.nam);
    write(' Napravlenie: '); readln(kim.napr);
    yyyr(kim.year,'First year',false);
    yyyr(kim.lyear,'Last year',false);
    writeln(' Members: ');
    memam(kim.sost,false);
    write(g,kim);close(g);
    mov(f,g); close(f);
   end;

{-------------------------------------CHANGEFILE}
 procedure changefile;
  var coc:char;
  begin
   if o=false then writeln('   ERROR 157 : File not opened/(open file first, even if it not exists)')
    else begin
      writeln(' n - add artist to file');
      writeln(' d - delete artist from file');
      writeln(' c - change artists data');
      write('/'); readln(coc);
      case coc of
        'n':ada(f);
        'd':dela(f);
        'c':chaa(f);
        else writeln('   ERROR 24 : Wrong command');
       end;
  end; end;

{----------------------------------------NEWFILO}
 procedure newfilo;
  var s:str;
  begin
   writeln('   Where to place the file?');
   readln(s);
   assign(f,s);
   rewrite(f); close(f); o:=true;
   repeat
     writeln('   Whould you like to make First record in this file?');
     write('/');
     readln(but);
     case but of
       'y':ada(f);
       'n':;
       else writeln('   ERROR 32 : Wrong insertion')
     end;
   until (but='y') or (but='n')
  end;

{---------------------------------------READFILE}
 procedure readfile(var f:typ);
  var kim:art;
      i,k,p:integer;
  begin
   reset(f);
   writeln('   What is the number? (0 - all)');
   write('/');
   readln(k);
   i:=1;
   While not eof(f) do begin
     read(f,kim); if (i=k)or(k=0)then begin
     writeln('     Artist #',i,':');
     write('  Name: '); if length(kim.nam)>0 then writeln(kim.nam)
                        else writeln('N/a');
     writeln('  Napravlenie: ',kim.napr);
     write('  First year: '); if kim.year>0 then writeln(kim.year)
                            else writeln('N/a');
     write('  Last year: '); if kim.lyear>0 then writeln(kim.lyear)
                         else writeln('N/a');
     write('  Members: '); writeln;
                           p:=0;
                           repeat
                             inc(p);
                             if kim.sost[p].nam<>'n/a' then
                                writeln(kim.sost[p].nam,' - ',kim.sost[p].rol)
                           until kim.sost[p].nam='n/a';
     writeln('-----------------------------------')
     end;
     inc(i);
   end;
   if i<k then writeln('ERROR 404 : Not found');
   close(f);
   unter;
  end;

{OPENFILE---JA-DUMAU-OCHEVIDNO------------------}
 procedure openfile;
  var s:str;
  begin
   writeln('   Write file path');
   write('/');
   readln(s);
   o:=true;
   assign(f,s);
   repeat
     writeln('   Would you like to read it(short)? y/n');
     write('/');
     readln(s);
     case ord(s[1]) of
      ord('y'):sreed(f);
      ord('n'):;
      else writeln('ERROR 32: Bad insertion');
     end;
  until (s='y')or(s='n');
  end;


{-------------------------------------------HELP}
 procedure help;
   begin
   writeln; writeln;
   writeln('   MAIN_MENU:           What would you like?');
   writeln('  o:open file');
   writeln('  j:new file');
   writeln('  r:read chosen file');
   writeln('  c:change chosen file');
   writeln('  q:quit');
   write('/');
   end;

{REDFILE---ZAPROS-NA-PROCHTENIE-FAILA-----------}
 procedure redfile;
   var but:char;
   begin
    repeat
    writeln('   1 - short, 2 - full');
    write('/');
    readln(but);
    case ord(but) of
     ord('1'):SREED(f);
     ord('2'):readfile(f);
     else writeln('   ERROR 32 : Bad insertion');
    end;
    until (but='1') or (but='2');
   end;

{-------------------------------------MAIN--MENU}
 procedure menu(but:char);
   Begin
    case but of
      'o': openfile;
      'r': if o then redfile else bi;
      'j': newfilo;
      'c': if o then changefile else bi;
      'q':;
      else writeln('ERROR 24 : Wrong command')
     end;
   end;


{----------------------------------------PROGRAM}
 BEGIN
  assign(g,'tempproggy.ogr');
  repeat
    help;
    readln(but);
    menu(but);
  until but='q';
 END.
