/**
   * Program entry point. Displays the clock an main menu.
   *
   * @param args (not used)
   */
  public static void main(String[] args) {
    // lights on!
    jcontrol.io.Backlight.setBrightness(jcontrol.io.Backlight.MAX_BRIGHTNESS);

    lcd = new Display();
    key = new Keyboard();
    lcd.setFont(null);
    {
      String d = Management.getProperty("system.standbytimer");
      if (d != null) Management.powerOff(Integer.parseInt(d)); // get power off delay
    }
    int select = 0;
    Clock clk;
    for (; ; ) { // main loop
      lcd.clearDisplay();
      if (RTC.isAlarm()) {
        Time wakeup = new Time();
        RTC.getAlarm(wakeup);
        RTC.resetAlarm();
        Date alarmDate = Date.getNextDate(wakeup);
        if (alarmDate != null) {
          lcd.drawString(time2string(alarmDate.time), 0, 45);
          lcd.drawString("$".equals(alarmDate.text) ? "no subject" : alarmDate.text, 0, 56);
          updateAlarm(alarmDate.time);
        }
        clk = new Clock(45, 0, 40, 40, lcd, true); // show the alarm clock
      } else {
        clk = new Clock(35, 2, 60, 80, lcd, false); // show the normal clock
      }
      while (clk.isRunning()) {
        try {
          ThreadExt.sleep(500);
        } catch (InterruptedException e) {
          clk.stop();
        }
        if (key.getKey() != 0) clk.stop();
      }
      if (RTC.isAlarm()) continue; // begin from above...
      key:
      for (; ; ) { // the menu loop
        try {
          lcd.drawImage(new Resource("organizer.jcif"), 0, 0); // draw the background image
        } catch (java.io.IOException e) {
        }
        lcd.setFont(null);
        select =
            MenuSelector.listSelect(
                new String[] { // let the user select one of the specified menu items
                  "display time", "new appointment", "view appointments", "turn off"
                },
                select,
                lcd,
                key);
        Date date = null;
        switch (select) {
          case -1:
          case 0: // show the current time
            select = 0;
            break key;
          case 2: // adjust appointment
            date = listDates(); // show all stored dates and return date to change
            if (date == null) break;
          case 1: // new appointment
            date =
                DateInput.getDate(
                    lcd, key,
                    date); // get a new date (date==null) or modify an existing (date!=null)
            if (date != null) { // if the new date is valid
              try {
                date.store();
              } catch (IOException e) {
              }
              date = null; // this object may be garbage collected now
              updateAlarm(null);
            }
            break;
          case 3: // standby
            lcd.clearDisplay();
            while (key.getKey() != 0) {}
            jcontrol.system.Management.powerOff(-1);
            break;
        }
      }
    }
  }