
// Copyright (c) 2001 Oracle Corporation
package oracle.portal.provider.tools.v1;

import oracle.webdb.utils.SimpleStringBuffer;
import oracle.portal.utils.v1.MultiByteEncoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

/**
 * A Class class.
 * <P>
 * @author Chris Broadbent
 */
public class RenameCustomizationFiles extends Object
{

    /**
     * Constructor
     */
    public RenameCustomizationFiles()
    {
    }

    /**
     * main
     * @param args
     */
    public static void main(String[] args)
    {
        if (args.length != 2 || args[0].equals("") || args[1].equals(""))
        {
            System.out.println("Usage: java oracle.portal.provider.tools.RenameCustomizationFiles <start directory> <log file>");
            System.exit(1);
        }

        File startDir = new File(args[0]);
        if (startDir.exists() == false || startDir.isDirectory() == false)
        {
            System.out.println("start path does not exist or is not a directory");
            System.exit(1);
        }

        PrintWriter pw = null;
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(args[1]);
            pw = new PrintWriter(fos);
        }
        catch (Exception e)
        {
            System.out.println("Failed to open log file");
            System.exit(1);
        }

        RenameCustomizationFiles process = new RenameCustomizationFiles();
        process.findFiles(startDir, pw);
        pw.flush();
        pw.close();
    }

    private void findFiles(File directory, PrintWriter out)
    {
        out.println("Starting to process directory: " + directory.toString());
        String[] files = directory.list();
        for(int i=0;i < files.length; i++)
        {
            SimpleStringBuffer fullname = new SimpleStringBuffer(directory.getAbsolutePath());
            fullname.append(File.separator);
            fullname.append(files[i]);
            
            File f = new File(fullname.toString());
            if (f.isDirectory())
            {
                findFiles(f, out);
                out.println("Completed processing directory: " + f.toString());
            }
            else if (f.getName().indexOf('@') > 0)
            {
                renameFile(f, out);
            }
            else
            {
                out.println("File does not need renaming: " + f.toString());
            }
        }
    }

    private void renameFile(File file, PrintWriter out)
    {
        File newFile = null;
        try
        {
            // filename is in a hashed directory - need to
            // - remove the current hash code from the directory path
            // - encode the file name
            // - recalculate the hashcode
            // - create the directory for the hashcode
            // - rename the file to it's new name/location

            // get the file path minus the existing hashcode directory
            File parentDirectory = new File(file.getParent());
            String grandParentPath = parentDirectory.getParent();
            String oldName = file.getName();

            // calculate the new name for the hash directory based on the
            // encoded filename
            String newName = MultiByteEncoder.encodeUTF(oldName);
            int hashValue = Math.abs(newName.hashCode() % 251);
            String hashDirName = Integer.toString(hashValue, 16);
            SimpleStringBuffer fullNewName = new SimpleStringBuffer(grandParentPath);
            fullNewName.append(File.separator);
            fullNewName.append(hashDirName);

            File hashDir = new File(fullNewName.toString());

            fullNewName.append(File.separator);
            fullNewName.append(newName);

            newFile = new File(fullNewName.toString());

            if (hashDir.exists() == false)
            {
                out.println("Creating directory: " + hashDir.toString());
                hashDir.mkdir();
            }

            file.renameTo(newFile);

            out.println("Renamed file: " + file.toString() +
                        " to: " + newFile.toString());
        }
        catch(Exception e)
        {
            out.println("ERROR: Failed to rename file: " + file.toString() +
                        " to: " + newFile.toString());
        }
    }
}

