/*
 *
 *
 * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation. 
 * 
 * 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 version 2 for more details (a copy is
 * included at /legal/license.txt). 
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA 
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions. 
 */

package com.sun.midp.scriptutil;

import javax.microedition.midlet.MIDlet;

import com.sun.midp.midletsuite.*;

/**
 * Removes a suite.
 */
public class SuiteRemover extends MIDlet implements Runnable {
    /** ID of the suite. */
    int suiteId;

    /**
     * Create and initialize the MIDlet.
     */
    public SuiteRemover() {
        // The arg-<n> properties are generic command arguments
        String suiteArg = getAppProperty("arg-0");

        if (suiteArg == null) {
            System.err.println("Suite ID argument missing.");
            System.err.println("Suite remover arguments: <suite ID>");
            notifyDestroyed();
            return;
        }

        try {
            suiteId = Integer.parseInt(suiteArg);
        } catch (Throwable t) {
            System.err.println("Suite ID: " + t.getMessage());
            notifyDestroyed();
        }            

        new Thread(this).start();
    }

    /**
     * Start.
     */
    public void startApp() {
    }

    /**
     * Pause; there are no resources that need to be released.
     */
    public void pauseApp() {
    }

    /**
     * Destroy cleans up.
     *
     * @param unconditional is ignored; this object always
     * destroys itself when requested.
     */
    public void destroyApp(boolean unconditional) {
    }

    /** Remove a MIDlet suite. */
    public void run() {
        try {
            MIDletSuiteStorage midletSuiteStorage =
                MIDletSuiteStorage.getMIDletSuiteStorage();

            midletSuiteStorage.remove(suiteId);
            System.out.println("Suite removed");
        } catch (MIDletSuiteLockedException msle) {
            System.err.println("Error: MIDlet suite is locked");
        } catch (Throwable t) {
            System.err.println("Error removing suite " + suiteId);
            t.printStackTrace();
        }

        notifyDestroyed();
    }
}
