package com.jscape.util.file;

import com.jscape.util.FileInfo;
import com.jscape.util.SuppressObfuscation;

/**
 * @author Alex Usun
 */
@SuppressObfuscation
public class PosixFilePermissions {

    public final boolean ownerCanRead;
    public final boolean ownerCanWrite;
    public final boolean ownerCanExecute;
    public final boolean groupCanRead;
    public final boolean groupCanWrite;
    public final boolean groupCanExecute;
    public final boolean otherCanRead;
    public final boolean otherCanWrite;
    public final boolean otherCanExecute;
    public final boolean setUserIdOnExecution;
    public final boolean setGroupIdOnExecution;
    public final boolean directoryDeletionRestricted;

    public static PosixFilePermissions permissionsOf(FileInfo info) {
        return new PosixFilePermissions(info.isReadable(),
                                        info.isWritable(),
                                        false,
                                        info.isReadable(),
                                        info.isWritable(),
                                        false,
                                        info.isReadable(),
                                        info.isWritable(),
                                        false,
                                        false,
                                        false,
                                        false);
    }

    public static PosixFilePermissions ownerDirectoryPermissions() {
        return new PosixFilePermissions(true, true, true, false, false, false, false, false, false, false, false, false);
    }

    public PosixFilePermissions(boolean ownerCanRead,
                                boolean ownerCanWrite,
                                boolean ownerCanExecute,
                                boolean groupCanRead,
                                boolean groupCanWrite,
                                boolean groupCanExecute,
                                boolean otherCanRead,
                                boolean otherCanWrite,
                                boolean otherCanExecute,
                                boolean setUserIdOnExecution,
                                boolean setGroupIdOnExecution,
                                boolean directoryDeletionRestricted) {
        this.ownerCanRead = ownerCanRead;
        this.ownerCanWrite = ownerCanWrite;
        this.ownerCanExecute = ownerCanExecute;
        this.groupCanRead = groupCanRead;
        this.groupCanWrite = groupCanWrite;
        this.groupCanExecute = groupCanExecute;
        this.otherCanRead = otherCanRead;
        this.otherCanWrite = otherCanWrite;
        this.otherCanExecute = otherCanExecute;
        this.setUserIdOnExecution = setUserIdOnExecution;
        this.setGroupIdOnExecution = setGroupIdOnExecution;
        this.directoryDeletionRestricted = directoryDeletionRestricted;
    }

    public boolean executable() {
        return this.ownerCanExecute || this.groupCanExecute || this.otherCanExecute;
    }

    public boolean writable() {
        return this.ownerCanWrite || this.groupCanWrite || this.otherCanWrite;
    }

    @Override
    public String toString() {
        return "PosixFilePermissions {"
                + "ownerCanRead=" + this.ownerCanRead
                + ", ownerCanWrite=" + this.ownerCanWrite
                + ", ownerCanExecute=" + this.ownerCanExecute
                + ", groupCanRead=" + this.groupCanRead
                + ", groupCanWrite=" + this.groupCanWrite
                + ", groupCanExecute=" + this.groupCanExecute
                + ", otherCanRead=" + this.otherCanRead
                + ", otherCanWrite=" + this.otherCanWrite
                + ", otherCanExecute=" + this.otherCanExecute
                + ", setUserIdOnExecution=" + this.setUserIdOnExecution
                + ", setGroupIdOnExecution=" + this.setGroupIdOnExecution
                + ", directoryDeletionRestricted=" + this.directoryDeletionRestricted
                + '}';
    }
}
