/*
 * NOTE: This program depends on reading the ACL file under OS/2,
 * where lines are terminated with CR LF (2 bytes). Thus, the file
 * is being read in binary mode.
 * Under unix-alike operating systems this will not work, as here
 * a line is terminated only LF and so with only one byte.
 */

/* ***** BEGIN LICENSE BLOCK *****
* Version: CDDL 1.0
*
* The contents of this file are subject to the COMMON DEVELOPMENT AND
* DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at http://www.sun.com/cddl/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is
* "netlabs.org Open Source Archive Administrator/Client"
*
* The Original Distribution Package is named
* "netlabs.org Open Source Archive Administrator/Client"
* and maintained and distributed by the Initial Developer
* and/or netlabs.org only.
*
* In addition to the CDDL the following applies:
* If you modify the Original Code, you may distribute it only
* as a part of a distribution package where
*  - the name of the package, created directories or
*    OS2.INI entries do not contain any of the terms
*    - "netlabs.org Open Source Archive"
*    - "NOSA"
*    - "NOSAC"
*    - "NOSAADM"
*  - neither netlabs.org nor the Initial Developer is stated as
*    the vendor or originator of the resulting Distribution Package,
*    which contains the Modified Code.
*
* The Initial Developer of the Original Code is
* netlabs.org: Christian Langanke <cla@clanganke.de>.
* Portions created by the Initial Developer are Copyright (C) 1999-2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>

#define INCL_DOS
#define INCL_ERRORS
#include <os2.h>

#include "cvsenv.h"
#include "cvsstr.h"

// --------------------------------------------------------

PCVSENV GetEnvironment( VOID)
{
         PCVSENV        pcvsenvResult = NULL;
static   CVSENV         cvsenv;
         APIRET         rc = NO_ERROR;
         CHAR           szRootDir;
static   PSZ            pszLocalTag = ":local:";
         CHAR           szMissingVars[ 32];
         PSZ            pszSlash;

do

   {
   // initialize
   memset( &cvsenv, 0, sizeof( cvsenv));


   cvsenv.pszCvsRootDir = getenv( "CVSROOT");
   cvsenv.pszUser       = getenv( "USER");
   if (cvsenv.pszCvsRootDir)
      {
      // separate root directory
      cvsenv.pszCvsRootDir = strstr( cvsenv.pszCvsRootDir, pszLocalTag);
      if (cvsenv.pszCvsRootDir)
         cvsenv.pszCvsRootDir += strlen( pszLocalTag);
      if (!*cvsenv.pszCvsRootDir)
         cvsenv.pszCvsRootDir = NULL;

      // remove trailing slash
      pszSlash = cvsenv.pszCvsRootDir + strlen( cvsenv.pszCvsRootDir) - 1;
      if (strchr( "\\/", *pszSlash))
         *pszSlash = 0;
      }

   if ((!cvsenv.pszCvsRootDir) || (!cvsenv.pszUser))
      {
      szMissingVars[ 0] = 0;
      if (!cvsenv.pszCvsRootDir) strcpy( szMissingVars, " CVSROOT");
      if (!cvsenv.pszUser)       strcat( szMissingVars, " USER");
      printf( "error: environment variable(s)%s missing or not properly set.", szMissingVars);
      rc = ERROR_ENVVAR_NOT_FOUND;
      break;
      }

    // strip off blanks
    stripblanks( cvsenv.pszCvsRootDir);
    stripblanks( cvsenv.pszUser);

   // define filenames for writeinfo handling
   sprintf( cvsenv.szWriteInfoFile, "%s\\CVSROOT\\writeinfo",     cvsenv.pszCvsRootDir);
   sprintf( cvsenv.szWriteInfoLog,  "%s\\CVSROOT\\writeinfo.log", cvsenv.pszCvsRootDir);

   // set all slashes to dos alike backslashes
   dosslash( cvsenv.pszCvsRootDir);
   dosslash( cvsenv.szWriteInfoFile);
   dosslash( cvsenv.szWriteInfoLog);

   strupr( cvsenv.pszCvsRootDir);

   // hand over result
   pcvsenvResult = &cvsenv;


   } while (FALSE);

// cleanup
return pcvsenvResult;
}


