#ident "$Id: getRoot.c,v 1.1 2006/11/08 21:58:32 craig Exp $"
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * Functions common to one or more partition commands.
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#else
#define fseeko(file, where, whence)     fseeko64(file, where, whence)
#define ftello(file)                    ftello64(file)
#endif

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>

#if !defined(IRD)
extern bool     Verbose;
#endif
extern char     *ProgName;
static void     convertBackwardToForwardSlashes(char *string);

/* ------------------------------------------------------------------------ */

/*
 * Get the proper location of the root directory, underneath which
 * is found the binaries.  Look in REVOLUTION_SDK_ROOT, then ROOT.
 * If both are not defined, print an error message and return false;
 *
 * The user of this function should free the returned strings
 * after it is done.
 *
 * INPUT: None
 */

#define PLAIN_BIN_PATH "/usr/bin/x86/"
#define PLAIN_PKI_PATH "/usr/etc/pki_data/"

#define RVL_BIN_PATH   "/x86/bin/"
#define RVL_PKI_PATH   "/x86/bin/pki_data/"
bool
getRoot(char **binRoot, char **pkiRoot)
{
    char        *root;
    size_t      rootLen, mallocLen;

    if ((root = getenv("REVOLUTION_SDK_ROOT")) != NULL) {
#if !defined(IRD)
        if (Verbose) {
            printf ("%s: Using REVOLUTION_SDK_ROOT to find binaries\n",
                    ProgName);
        }
#endif
        convertBackwardToForwardSlashes(root);
        rootLen = strlen(root);
        // Chop off trailing / in environment variable, if any
        if (root[rootLen - 1] == '/') {
            root[rootLen - 1] = '\0';
        }

        mallocLen = strlen(root) + strlen(RVL_BIN_PATH) + 1;
        if ((*binRoot = malloc(mallocLen)) == NULL) {
            fprintf(stderr, "%s: Malloc of %u bytes failed\n",
                    ProgName, mallocLen);
            return(false);
        }
        strcpy(*binRoot, root);
        strcat(*binRoot, RVL_BIN_PATH);

        mallocLen = strlen(root) + strlen(RVL_PKI_PATH) + 1;
        if ((*pkiRoot = malloc(mallocLen)) == NULL) {
            fprintf(stderr, "%s: Malloc of %u bytes failed\n",
                    ProgName, mallocLen);
            return(false);
        }
        strcpy(*pkiRoot, root);
        strcat(*pkiRoot, RVL_PKI_PATH);
    }
    else if ((root = getenv("ROOT")) != NULL) {
#if !defined(IRD)
        if (Verbose) {
            printf ("%s: Note - REVOLUTION_SDK_ROOT not defined, using ROOT\n",
                     ProgName);
        }
#endif
        convertBackwardToForwardSlashes(root);
        rootLen = strlen(root);
        // Chop off trailing / in environment variable, if any
        if (root[rootLen - 1] == '/') {
            root[rootLen - 1] = '\0';
        }

        mallocLen = strlen(root) + strlen(PLAIN_BIN_PATH) + 1;
        if ((*binRoot = malloc(mallocLen)) == NULL) {
            fprintf(stderr, "%s: Malloc of %u bytes failed\n",
                    ProgName, mallocLen);
            return(false);
        }
        strcpy(*binRoot, root);
        strcat(*binRoot, PLAIN_BIN_PATH);

        mallocLen = strlen(root) + strlen(PLAIN_PKI_PATH) + 1;
        if ((*pkiRoot = malloc(mallocLen)) == NULL) {
            fprintf(stderr, "%s: Malloc of %u bytes failed\n",
                    ProgName, mallocLen);
            return(false);
        }
        strcpy(*pkiRoot, root);
        strcat(*pkiRoot, PLAIN_PKI_PATH);
    }
    else {
        fprintf (stderr, "%s: Must set environment variable REVOLUTION_SDK_ROOT or ROOT\n",
                 ProgName);
        return(false);
    }
    return(true);
} // getRoot
/* ------------------------------------------------------------------------ */
void
convertBackwardToForwardSlashes(char *str)
{
    char *ptrToSlash;

    while ( (ptrToSlash = index(str, '\\')) != NULL) {
        *ptrToSlash = '/';
    }
}
/* ------------------------------------------------------------------------ */
