/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/


#include "lib_sys.h"


/* From baselib.cpp (kernel)
// Récupère la première ligne; retourne un pointeur vers la ligne suivante ou
// NULL si terminé ou erreur*/
char *stdGetLine (char *src, char *buf, int n)
{
  int c,i;

  while((c=255&(*(src++)))<32) if (c==0) return NULL;
  i=0;
  buf[i++]=c;
  while(((c=255&(*(src++)))>=32)&&(c)&&(i<n)) buf[i++]=c;
  if (c==0) src--;
  if (i==n) return NULL;
  buf[i]=0;
  return src;
}

/* From baselib.cpp (kernel)
// Decoupage d'une chaine en mots (retourne le nombre de mots) format strbuild*/
int strCutting(char *comm, char **argv)
{
  int i,j,k,argc;

  i=0;
  j=-1;
  argc=0;

  while(comm[i])
  {
	  if (comm[i]==32)
	  {
		  if (j>=0) comm[j]=0;
		  j=-1;
		  i++;
	  }
	  else
	  {
		  if (j==-1)
		  {
			  j=i;
			  argv[argc++]=&comm[i];
		  }
		  if (comm[i]=='\\')
		  {
			  i++;
              if ((comm[i]>='0')&&(comm[i]<='9'))
			  {
				  k=comm[i]-'0';
				  i++;
				  if ((comm[i]>='0')&&(comm[i]<='9'))
				  {
					  k=k*10+comm[i]-'0';
					  i++;
					  if ((comm[i]>='0')&&(comm[i]<='9'))
					  {
						  k=k*10+comm[i]-'0';
						  i++;
					  }
				  }
				  comm[j++]=k;
			  }
			  else if (comm[i]=='n')
			  {
				  comm[j++]=10;
				  i++;
			  }
			  else if (comm[i]=='z')
			  {
				  comm[j++]=0;
				  i++;
			  }
			  else
			  {
				  comm[j++]=comm[i];
				  if (comm[i]) i++;
			  }
		  }
		  else comm[j++]=comm[i++];
	  }
  }
  if (j>=0) comm[j]=0;
  return argc;
}


/* Return the path of the Scol executable */
char * lib_sys_getPathScol (char *pathscol)
{
    /*pathscol = getcwd (pathscol, SIZESIGN);*/
    char *s;
    s = getcwd (pathscol, SIZESIGN);
    pathscol = malloc (sizeof (char) * SIZESIGN);
    pathscol = memcpy (pathscol, s, SIZESIGN);
    free (s);
    if (pathscol == NULL)
    {
        MMechostr (0, "lib_sys_getDirScol error : pathscol is NULL\n");
        return NULL;
    }
    return pathscol;
}

/* Return the Scol directory (by default under Windows, this directory is 'Scol Voyager') */
char * lib_sys_getDirScol (char *dirscol)
{
    char *pathscol = NULL;

    pathscol = lib_sys_getPathScol (pathscol);
    dirscol = strrchr (pathscol, SEP);
    free (pathscol);
    return dirscol;
}

/* return the content of the usm.ini file */
char * lib_sys_getUSM (char *usm, size_t lenusm)
{
    FILE *f;

    char *pathusm = NULL, *pathscol = NULL;

    pathusm = malloc (sizeof (char) * SIZESIGN);
    pathscol = lib_sys_getPathScol (pathscol);
    snprintf (pathusm, SIZESIGN-1, "%s%cusm.ini", pathscol, SEP);
    free (pathscol);

    f = fopen (pathusm, "r");
    if (f == NULL)
    {
        MMechostr (0, "lib_sys_getUSM error : unable to open usm.ini : %s\n", pathusm);
        return NULL;
    }
    usm [fread (usm, 1, lenusm, f)] = 0;
    fclose (f);
    f = NULL;
    free (pathusm);
    return usm;
}


char * lib_sys_getPluginsNames (char *argv [SIZESIGN], char *key, int n, char *libname, size_t lenmax)
{
    if ((n > 1) && (!strcasecmp (argv[0], key)))
        snprintf (libname, lenmax-1, "%s", argv[1]);
    return libname;
}

/* get the absolute path of the admin partition (or partition under Linux) */
char * lib_sys_getPathAdminPartition (char *argv [SIZESIGN], char *key, int n, char *partition, size_t lenmax)
{
    char *userpath = NULL;

#if ((defined _WIN32) || (defined __WIN32__))
    userpath = lib_sys_getPathScol (userpath);
#else
    userpath = lib_sys_getPathScol (userpath);
#endif

    if ((n > 1) && (!strcasecmp (argv[0], "diska")))
    {
#if ((defined _WIN32) || (defined __WIN32__))
        snprintf (partition, lenmax-1, "%s/%s/", userpath, basename (argv[1]));
#else
        snprintf (partition, lenmax-1, "%s/%s/", userpath, basename (argv[1]));
#endif
        /*(*partition)[lenmax-1] = '\0';*/
#if ((defined _WIN32) || (defined __WIN32__))
        partition = replaceChar (partition, '/', '\\');
#endif
        strncpy (spPartition->admin , (partition), SIZESIGN);
    }
    else if ((n > 2) && (!strcasecmp (argv[0], "disk")) && (!strcasecmp (argv[2], "0")))
    {
        snprintf (partition, lenmax-1, "%s/%s/", userpath, basename (argv[1]));
        (partition)[lenmax-1] = '\0';
        strncpy (spPartition->admin , (partition), SIZESIGN);
    }
    free (userpath);
    return partition;
}

/* get the absolute path of the user partition (or partition under Linux) */
char * lib_sys_getPathPartition (char *argv [SIZESIGN], char *key, int n, char *partition, size_t lenmax)
{
    char *userpath = NULL;

#if ((defined _WIN32) || (defined __WIN32__))
    char *dirscol = NULL;

    userpath = malloc (sizeof (char) * SIZESIGN);
    dirscol = lib_sys_getDirScol (dirscol);
    SHGetFolderPath (NULL, CSIDL_PERSONAL, NULL, 0, userpath);
    userpath[strlen (userpath)] = '\0';
#else
    userpath = lib_sys_getPathScol (userpath);
#endif

    if ((n > 1) && (!strcasecmp (argv[0], "disku")))
    {
#if ((defined _WIN32) || (defined __WIN32__))
        snprintf (partition, SIZESIGN, "%s%s/%s/", userpath, dirscol, basename (argv[1]));
#else
        snprintf (partition, SIZESIGN, "%s/%s/", userpath, basename (argv[1]));
#endif
        /*partition[SIZESIGN-1] = '\0';*/
#if ((defined _WIN32) || (defined __WIN32__))
        partition = replaceChar (partition, '/', '\\');
#endif
        strncpy (spPartition->user , partition, SIZESIGN);
    }
    else if ((n > 2) && (!strcasecmp (argv[0], "disk")) && (!strcasecmp (argv[2], "0")))
    {
        snprintf (partition, lenmax-1, "%s/%s/", userpath, basename (argv[1]));
        (partition)[lenmax-1] = '\0';
        strncpy (spPartition->user , partition, SIZESIGN);
    }
    free (userpath);
    return partition;
}

/* get a value from a usm key */
char * lib_sys_getDatasFromUsm (char *data, char *key, size_t lenmax, char * (*f) (char *a[SIZESIGN], char *key, int n, char *d, size_t l))
{
    int n;
    char *line, *src;
    char* argv[SIZESIGN];
    char buf[SIZESIGN];

    src = malloc (sizeof (char) * lenmax);
    src = lib_sys_getUSM (src, lenmax);

    line = src;
    while ((line = stdGetLine (line, buf, SIZESIGN)))
    {
        if ((n = strCutting (buf, argv)))
        {
            data = (*f) (argv, key, n, data, lenmax);
            /*strncpy (data, (*f) (argv, n, data, lenmax), SIZESIGN);*/
        }
        argv[0] = argv[1] = argv[2] = NULL;
    }
    free (src);
    return data;
}


void lib_sys_getListFromUsm (char libfiles[SIZESIGN][SIZESIGN], int *i, char *key)
{
    int n;
    char *line, *src;
    char *argv[SIZESIGN];
    char buf[SIZESIGN];

    src = malloc (sizeof (char) * 16656);
    src = lib_sys_getUSM (src, 16656);

    line = src;
    while ((line = stdGetLine (line, buf, SIZESIGN)))
    {
        if ((n = strCutting (buf, argv)))
        {
            if ((!strcasecmp (argv[0], key)) && (n > 1) && (*i < SIZESIGN))
            {
                snprintf (libfiles[*i], SIZESIGN-1, "%s", argv[1]);
                (*i)++;
            }
        }
    }
    return;
}


