#include "archinc.h"
#include "cli.h"
#include "strutil.h"
#include "zarlink.h"
#include "nvstore.h"
#include "trunk.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef CONFIG_INCLUDE_ZARLINK

#define NUM_TRUNKS (2)

typedef struct trunk_t {
  int activeChannel;     // active channel in trunk
  int standbyChannel;    // stby channel in trunk
  int failoverMech;      // not used, for future consideration
  int failoverStatus;    // 0= normal, 1 = failover occured, -1 is not setup
  int linkFailThreshold; // set to >=1, 1=tolerate no failures
  } trunk_t, *pTrunk_t;

static trunk_t trunks[NUM_TRUNKS];

// returns pointer to trunk structure
static pTrunk getTrunk(void)
{
}

// find the trunk that a channel (either active or stby) is in
static int findTrunk(int channel)
{
  int i;

  for(i=0;i<NUM_TRUNKS;i++)
    if ((trunks[i].activeChannel == channel) || 
        (trunks[i].standbyChannel == channel))
          break;

  if (i == NUM_TRUNKS)
    return -1;
  else
    return i;
}


int trunkBuild(int active, int standby, int linkFailThreshold)
{
}

int trunkTeardown(int active, int standby, int linkFailThreshold)
{
}

int trunkToActive(int channel)
{
  int retVal;
    
  if((retVal = findTrunk(channel)) == -1)
    return retVal;

}

int trunkToStandby(int channel)
{
  int retVal;
    
  if((retVal = findTrunk(channel)) == -1)
    return retVal;


}


static int saveTrunkConfig(int trunkId)
{
  // writes data structure to nvram
}

static int loadTrunkConfig(int trunkId)
{
  // writes data structure to nvram
}

static int trunkHandler(int argc, char * argv[], int flags)
{
  int retVal = 0;
  char * pOutBuf;
  int bufLen;

  pOutBuf = clicharGetOutputBuffer(&bufLen);

  if (argc < 4)
    return -1;

  clicharSendOutputBuffer(pOutBuf, bufLen);
  return retVal;
}

int trunkInit(void)
{
  int i;

  for(i=0;i<NUM_TRUNKS;i++) {
    trunks[i].activeChannel = -1;
    trunks[i].standbyChannel = -1;
    trunks[i].failoverMech = 0;
    trunks[i].failoverStatus = -1;
    trunks[i].linkFailThreshold = 0;
    }

  cliRegisterCommand("trunk", trunkHandler, "trunk [active] [standby] [thres] - sets up trunk");

  return 0;
}  

#endif /* CONFIG_INCLUDE_ZARLINK */

