/************************************************************************/
/*    Copyright (C) 2004  Michael C. Shultz               */
/*                                    */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU 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 General Public License for more details.           */
/*                                    */
/* You should have received a copy of the GNU 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.                          */
/*                                    */
/* Michael C. Shultz                          */
/* ringworm@inbox.lv                          */
/* Box 3238 Landers, CA 92285                     */
/************************************************************************/
#include  <portmanager.h>

#define OFF       -2
#define HELP      0
#define   PACKAGE_ADD 1
#define VERSION   2
#define PMSTATUS  3
#define PMUPGRADE 4



int   rPkgAdd( char* port );
void  rHelp( char* id );
int    main( int argc, char** argv )
{
    char    cacheFile[] = DATADIR PORTS_CACHE_DB;
    char    h[]     = "-h";
    char    help[]      = "--help";
    char    pkgadd[]    = "-pkgadd";
    char    id[]        = "portmanager";
    char    s[]     = "-s";
    char    status[]    = "--status";
    char    u[]     = "-u";
    char    upgrade[]   = "--upgrade";
    char    v[]     = "-v";
    char    version[]   = "--version";
    int errorCode   = 0;
    int idx     = 0;    
    int selection   = 0;

    /*
    Parse command line options
    */
    while( idx < argc )
    {
        if( !strcmp( argv[idx], help ) || !strcmp( argv[idx], h ) )
        {
            selection   = HELP;
            break;
        }

        if( !strcmp( argv[idx], pkgadd ) )
        {
            selection   = PACKAGE_ADD;
            break;
        }

        if( !strcmp( argv[idx], status ) || !strcmp( argv[idx], s ) )
        {
            selection   = PMSTATUS;
            break;
        }

        if( !strcmp( argv[idx], upgrade ) || !strcmp( argv[idx], u ) )
        {
            selection   = PMUPGRADE;
            break;
        }

        if( !strcmp( argv[idx], version ) || !strcmp( argv[idx], v ) )
        {
            selection   = VERSION;
            break;
        }
        idx++;
    }
    PMGRrReadConfigure();

    switch( selection )
    {
        case   PACKAGE_ADD:
        {
            rPkgAdd( argv[idx+1] );
        }
        case   HELP:
        {
            rHelp( id );
            break;
        }
        case   VERSION:
        {
            fprintf( stdout, "\n%s %s version info:\n", id, ver );
            break;
        }
        case   PMSTATUS:
        {
            system( "pmStatus" );
            if( !MGrIfFileExist( cacheFile ) )
            {
                /* if here then the cache is reset, try 1 more time */
                system( "pmStatus" );
            }
            break;
        }
        case   PMUPGRADE:
        {
            errorCode   = system( "pmupgrade" );
            break;
        }
     default:
            rHelp( id );
            break;
    }
    return( errorCode );
}

int rPkgAdd( char* port )
{
    char*   command;
    int errorCode   = 0;

    if( port == NULL )
    {
        exit( 1 );
    }

    command = (char*)malloc(strlen("pkg_add -f") + strlen( port ) + 1 );
    strncpy( command, "pkg_add -f ", strlen("pkg_add -f ") + 1 );
    strncat( command, port, strlen( port ) + 1 );
    fprintf( stdout, "executing: %s\n", command );
    errorCode = system( command );
    if( errorCode != 127 && errorCode != 256 )
    {
        printf( "OK 1 errorCode = %d\n", errorCode );
        errorCode   = system( "pmupgrade" );
        exit( errorCode );
    }
    else
    {
        command = (char*)malloc(strlen("pkg_add -rf") + strlen( port ) + 1 );
        strncpy( command, "pkg_add -rf ", strlen("pkg_add -rf ") + 1 );
        strncat( command, port, strlen( port ) + 1 );
        fprintf( stdout, "executing: %s\n", command );
        errorCode = system( command );
        if( errorCode != 127 && errorCode != 256 )
        {
            printf( "OK 2 errorCode = %d\n", errorCode );
            errorCode   = system( "pmupgrade" );
            exit( errorCode );
        }
        printf( "ERR 3 errorCode = %d\n", errorCode );
        exit( errorCode );
    }
    printf( "ERR 4 unknown %d\n", errorCode );
    exit( errorCode );
}

void  rHelp( char* id )
{
            fprintf( stdout, "%s %s help:\n", id, ver );
            fprintf( stdout,
        "\n to add package from /usr/ports/packages/All or from remote:\t-pkgadd\n" ); 
            fprintf( stdout,
                "\n for status of installed ports:\t-s or --status\n" ); 
            fprintf( stdout, 
                "\n to upgrade ports:\t-u or --upgrade\n\n" ); 
            fprintf( stdout,
                "\n for version information:\t-v or --version\n" ); 
            fprintf( stdout, 
"see portmanager(1), /usr/local/share/doc/portmanager/portmanager.1.html or \
http://portmanager.sourceforge.net/ for more information.\n\n" );
    return;
}