// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.util;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

public final class FileUtil
{
    /**
        Fix up file separators for platform independence. Converts
        <B>file</B> to use the appropriate separator character for the
        current platform.

        @param filename a file potentially with non platform
        specific file separator characters ("/" or "\")

        @return the same <b>file</b> but with only
        File.separatorChar as the separator
    */
    public static File fixFileSeparators (File file)
    {
        if (file == null) {
            return null;
        }
        return new File(fixFileSeparators(file.getPath()));
    }

    /**
        Fix up filename separators for platform independence.
        Converts <B>filename</B> to use the appropriate separator
        character for the current platform.

        @param filename a filename potentially with non platform
        specific file separator characters ("/" or "\")

        @return the same <b>filename</b> but with only
        File.separatorChar as the separator
    */
    public static String fixFileSeparators (String filename)
    {
        if (filename == null) {
            return null;
        }
        switch (File.separatorChar) {
          case '/':
            return filename.replace('\\', File.separatorChar);
          case '\\':
            return filename.replace('/', File.separatorChar);
          default:
            Log.util.log(Level.WARNING,
                         "File.seperatorChar {0}",
                         File.separator);
            filename = filename.replace('/', File.separatorChar);
            filename = filename.replace('\\', File.separatorChar);
            return filename;
        }
    }

    /**
        Returns the File for a named directory. If there is no
        directory <B>dir</B> relative to the File <B>base</B>, it and
        all needed parent directories will be created.

        @param base the parent directory of <b>dir</b>
        @param dir the name of the directory to find or create

        @return the directory found or created

        @exception IOException if there was an error finding or
        creating the directory
    */
    public static File directory (File base, String dir) throws IOException
    {
        return directory(base.getPath(), dir);
    }

    /**
        Returns the File for a named directory. If there is no
        directory <B>dir</B> relative to the directory <B>base</B>, it
        and all needed parent directories will be created.

        @param base the parent directory of <b>dir</b>
        @param dir the name of the directory to find or create

        @return the directory found or created

        @exception IOException if there was an error finding or
        creating the directory
    */
    public static File directory (String base, String dir)
      throws IOException
    {
        return directory(new File(new File(base), dir));
    }

    /**
        Returns the File for a named directory. If there is no
        directory it and all needed parent directories will be
        created.

        @param dir the name of the directory to find or create

        @return the directory found or created

        @exception IOException if there was an error finding or
        creating the directory
    */
    public static File directory (String dir) throws IOException
    {
        return directory(new File(dir));
    }

    /**
        Returns the File for a directory. If there is no directory it
        and all needed parent directories will be created.

        @param dir the directory to find or create

        @return the directory found or created

        @exception IOException if there was an error finding or
        creating the directory
    */
    public static File directory (File dir) throws IOException
    {
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                throw new IOException(Fmt.S("can't create directory %s", dir));
            }
        }

        if (!dir.isDirectory()) {
            throw new IOException(Fmt.S("%s is not a directory", dir));
        }

        return dir;
    }

    /**
        Convert a file to canonical form. On error, returns the same
        file. Java doesn't specify what might cause a failure. Most
        likely it is if you have permissions problems on the parent
        directories.

        @return current file converted to canonical format
    */
    public static File getCanonicalFile (File file)
    {
        try {
            return new File(file.getCanonicalPath());
        }
        catch (IOException e) {
            Log.util.log(
                Level.WARNING,
                "Could not create canonical path for {0}: {1}",
                new Object[] {file, e}
                );
            return file;
        }
    }

    /**
        prevent people from creating this class
    */
    private FileUtil ()
    {
    }
}
