# include "data.h"

int tvchannel (int argc, char **argv) {
  
  int N, kapa, Nchannel;
  char *name;
  KapaImageData data;

  name = NULL;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    name = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (&data, &kapa, name)) return (FALSE);
  FREE (name);

  // use the currently-set zero,range values if not supplied
  if (argc != 2) {
    gprint (GP_ERR, "USAGE: tvchannel (channel)\n");
    return (FALSE);
  }

  Nchannel = atoi (argv[1]);
  if (Nchannel == 0) {
    // try the string values R/Red, G/Green, B/Blue
    if (!strcasecmp (argv[1], "R") || !strcasecmp (argv[1], "RED")) {
      Nchannel = 1;
    }
    if (!strcasecmp (argv[1], "G") || !strcasecmp (argv[1], "GREEN")) {
      Nchannel = 2;
    }
    if (!strcasecmp (argv[1], "B") || !strcasecmp (argv[1], "BLUE")) {
      Nchannel = 3;
    }
  }
  if ((Nchannel < 1) || (Nchannel > 3)) {
    gprint (GP_ERR, "invalid channel : use 1 - 3 or (R)ed, (G)reen, (B)lue\n");
    return (FALSE);
  }
    
  KiiSetChannel (kapa, Nchannel - 1);
  return (TRUE);
}
