package org.bucksch.zeipis.importer;
import org.bucksch.zeipis.all.*;
import org.bucksch.zeipis.recorder.*;
import java.io.*;

/**
 * Starts the importers from the command line / as new process.
 * Contains the command line parsing and handling.
 */
public class Importer
{
  public static void main(String[] args)
  {
    // commandline processing
    boolean updateonly = false;
    boolean addonly = false;
    // XMLTV
    String xmltvfilename = null;
    // channel.conf
    String channelsconffilename = null;
    String format = null;
    // DVB channel and EIT scan
    boolean dvbchannelscan = false;
    boolean dvbschedule = false;
    int cardid = 1;
    int channelid = -1;
    int transponderid = -1;
    boolean allchannels = false;
    boolean newchannels = false;
    for (int i = 0; i < args.length; i++)
    {
      if (args[i].equals("--update-only"))
        updateonly = true;
      else if (args[i].equals("--add-only"))
        addonly = true;
      else if (args[i].equals("--xmltv") &&
          ++i < args.length)
        xmltvfilename = args[i];
      else if (args[i].equals("--channels-conf") &&
          ++i < args.length)
        channelsconffilename = args[i];
      else if (args[i].equals("--format") &&
              ++i < args.length)
        format = args[i];
      else if (args[i].equals("--channel-scan"))
        dvbchannelscan = true;
      else if (args[i].equals("--dvb-eit-schedule"))
        dvbschedule = true;
      else if (args[i].equals("--card") &&
              ++i < args.length)
        cardid = Integer.parseInt(args[i]);
      else if (args[i].equals("--transponder") &&
              ++i < args.length)
        transponderid = Integer.parseInt(args[i]);
      else if (args[i].equals("--channel") &&
              ++i < args.length)
      {
        if (args[i].equals("all-db"))
          allchannels = true;
        else if (args[i].equals("all"))
        {
          allchannels = true;
          newchannels = true;
        }
        else
        {
          channelid = Integer.parseInt(args[i]);
        }
      }
    }
    try {
      if (xmltvfilename != null)
        xmltv(xmltvfilename, updateonly);
      else if (channelsconffilename != null && format != null)
        channelsconf(cardid, channelsconffilename, format, updateonly, addonly);
      else if (dvbchannelscan && (channelid != -1 || transponderid != -1 || allchannels))
        dvbchannelscan(cardid, channelid, transponderid, allchannels, newchannels, updateonly, addonly);
      else if (dvbschedule && (channelid != -1 || allchannels))
        dvbschedule(cardid, channelid, allchannels);
      else
      {
        usage();
        System.exit(1);
      }
      System.exit(0);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
  static public void xmltv(String filename, boolean updateonly)
    throws Exception
  {
    new CoreData();
    InputStream s = new FileInputStream(new File(filename));
    new XMLTVImporter(DB.me.database(), s);
  }
  static public void channelsconf(int cardid, String filename, String format, boolean updateonly, boolean addonly)
    throws Exception
  {
    new CoreData();
    DVBCard card = (DVBCard) CoreData.me.myCards.get(cardid);
    InputStream stream = new FileInputStream(new File(filename));
    new ChannelsConfImporter(card, stream, format, updateonly, addonly);
  }
  static public void dvbchannelscan(int cardid, int channelid, int transponderid,
          boolean allchannels, boolean newchannels,
          boolean updateonly, boolean addonly)
    throws Exception
  {
    try {
      new CoreData();
      DVBCard card = (DVBCard) CoreData.me.myCards.get(cardid);
      DVBTransponder transponder = null;
      if (allchannels)
        ;
      else if (channelid != -1)
      {
        DVBChannel channel = (DVBChannel) CoreData.me.myChannels.get(channelid);
        if (channel == null)
          throw new Exception("Channel ID " + channelid + " not found");
        transponder = channel.transponder();
      }
      else if (transponderid != -1)
      {
        transponder = CoreData.me.myDVBTransponders.get(transponderid);
        if (transponder == null)
          throw new Exception("Transponder ID " + transponderid + " not found");
      }

      new DVBChannelImporter(DB.me.database(), card, updateonly, addonly)
        .start(allchannels ? null : transponder, allchannels, newchannels);

      System.exit(0);
    } catch (Exception e) {
      Util.error(e);
      System.exit(1);
    }
  }
  static public void dvbschedule(int cardid, int channelid, boolean allchannels)
  {
    try {
      new CoreData();
      DVBCard card = (DVBCard) CoreData.me.myCards.get(cardid);
      DVBChannel channel = null;
      if ( ! allchannels)
        channel = (DVBChannel) CoreData.me.myChannels.get(channelid);
      else throw new RuntimeException("'--channels all' not yet supported here");

      new DVBEIT(card)
        .tuneAndReadEIT(channel);

      System.exit(0);
    } catch (Exception e) {
      Util.error(e);
      System.exit(1);
    }
  }
  static public void usage()
  {
    System.err.println("Parameters missing or incorrect");
    System.err.println("You need to pass one of --xmltv, --channels-conf, --channel-scan or --dvb-schedule");
    System.err.println("Please use");
    System.err.println("--xmltv <filename>");
    System.err.println("--channels-conf <filename>");
    System.err.println("--format 'zap' or 'init'   (Required for --channels-conf)");
    System.err.println("     channels.conf file format:");
    System.err.println("     'init'   (Preferred) The initial tuning files in /usr/share/dvb/, input to |scan|.");
    System.err.println("     'szap'   ~/.szap/channels.conf, output of |scan| for satellite");
    System.err.println("     'czap'   ~/.czap/channels.conf, output of |scan| for cable");
    System.err.println("     'tzap'   ~/.tzap/channels.conf, output of |scan| for terrestrial");
    System.err.println("--update-only  (optional)    Do not add new channels");    
    System.err.println("--add-only  (optional)    Do add datasets, don't update existing data");    
    System.err.println("--channel-scan");
    System.err.println("--card <cardid>  (Optional, defaults to 1.)   ID of DVB card in database. Usually 1 or 2.");
    System.err.println("--channel <channelid> or 'all' or 'all-known' (Required for --channel-scan and --dvb-schedule):");
    System.err.println("    the ID (in database) of the channel to start scan from, or");
    System.err.println("    'all' for full scan, starting with all channels in database. Takes long, up to 1 hour.");
    System.err.println("    'all-db' for scanning all transponders in database, updating and adding channels on them. Takes maybe 2-10 minutes.");
    System.err.println("--dvb-eit-schedule   Search for TV guide information on the channel itself.");
  }
}
