/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 02-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.core.programs.fileUtils;

import java.io.File;
import java.util.Map;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentInstance;
import uk.ac.ic.doc.kevlar.core.types.textType.IFilenameType;
import uk.ac.ic.doc.kevlar.publicInterfaces.AbstractProgram;
import uk.ac.ic.doc.kevlar.publicInterfaces.IInputPipe;
import uk.ac.ic.doc.kevlar.publicInterfaces.IOutputPipe;

/**
 * @author Tristan Allwood
 *
 */
public class MoveFiles extends AbstractProgram {

    public void run(IArgumentInstance args, Map<String,IInputPipe> inputs, Map<String,IOutputPipe> outputs) {

        Boolean b = args.getBool("Use-Input-Lists");
        
        if( b != null && b.booleanValue() ) {
            
            IInputPipe src = inputs.get("From-Files");
            IInputPipe dest = inputs.get("To-Files"); 
            
            IOutputPipe succ = outputs.get("Success");
            IOutputPipe err = outputs.get("Error");
            
            
            if( src == null ) {
                exit(2);
                return;
            }
            
            if( dest == null ) {
                exit(3);
                return;
            }
            
            try {
                while (true) {
                    
                    IFilenameType sF = (IFilenameType) src.read();
                    
                    IFilenameType dF = (IFilenameType) dest.read();
                    
                    if( handleAFile(sF.getFile(),dF.getFile()) ) {
                        try {
                        if( succ != null ) {
                            succ.write(sF);
                        }
                        } catch (IllegalStateException ise ) {
                            succ = null;
                            //minor optimisation
                        }
                    } else {

                        try {
                            if( err != null ) {
                                err.write(dF);
                            }
                            } catch (IllegalStateException ise ) {
                                err = null;
                                //minor optimisation
                            }
                    }
                    
                }
            } catch(InterruptedException ie) {
                /* not good but stuff it*/
            } catch( IllegalStateException ise) {
                /* we're done */
            }
            
            return;
        } else {
            String fromFile = args.getText("From-File");
            String toFile = args.getText("To-File");
            
            if ( fromFile == null || toFile == null ) {
                exit(1);
                return;
            }
            
            File src = new File(fromFile);
            File dest = new File(toFile);
            
            exit( handleAFile(src,dest) ? 0 : 1 );
        }
    
    }
    
    
    private boolean handleAFile( File sF, File dF ) {
        
        return sF.renameTo(dF);
    }
}
