#include <libraries/asl.h>
#include <stdio.h>

struct Library *AslBase = NULL;
struct FileRequester *MyAsl = NULL;
struct FILE *Variable = NULL;

BOOL   Ergebnis;
char   varname[30];
int    l;

main(argc,argv)
int argc;
char *argv[];
{
   if(argc!=4)
   {
      printf("Aufruf: %s <Titel> <Directory> <Env-Variable>\n",argv[0]);
      exit(20);
   }

   OpenAsl();

   MyAsl = (struct FileRequester *)AllocFileRequest();
   if(!MyAsl)
   {
      printf("Requester-Struktur konnte nicht angelegt werden !\n");
      CloseAll(20);
   }

   Ergebnis = AslRequestTags(MyAsl, ASL_Hail,argv[1],ASL_Dir,argv[2],TAG_DONE,NULL);

   if(!Ergebnis) CloseAll(5);

   sprintf(varname,"ENV:%s",argv[3]);
   Variable = fopen(varname,"w");
   if(!Variable)
   {
      printf("Variable konnte nicht geschrieben werden !\n");
      CloseAll(20);
   }
   if((MyAsl->rf_Dir[strlen(MyAsl->rf_Dir)-1]!=':')&&(strlen(MyAsl->rf_Dir)!=0))
   {
      fprintf(Variable,"%s/%s\n",MyAsl->rf_Dir,MyAsl->rf_File);
   }
   else
   {
      fprintf(Variable,"%s%s\n",MyAsl->rf_Dir,MyAsl->rf_File);
   }
   CloseAll(0l);
}


OpenAsl()
{
   AslBase=(struct Library *)OpenLibrary(AslName,37);
   if(!AslBase)
   {
      printf("asl.library konnte nicht geöffnet werden !\n");
      exit(20);
   }
}

CloseAll(ECode)
int ECode;
{
   if(Variable)     fclose(Variable);
   if(MyAsl)  FreeFileRequest(MyAsl);
   if(AslBase) CloseLibrary(AslBase);
   exit(ECode);
}
