001/*
002 * Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
003 *
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 2.1 of the License, or (at your option) any later version.
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012 * Lesser General Public License for more details.
013 *
014*/
015/**
016 * Main class to run the BZip program from.
017 * Mostly just uses the command line and performs the appropriate operation.
018 * Calls stuff in the BZip class to do the compress/decompress operations.
019 * If the gui is used it call the BZipGui class.
020 *
021 * TODO:        Deal properly with stdout/stdin
022 *                      Improve flag handling.
023 */
024package com.aftexsw.util.bzip;
025
026import java.io.*;
027import java.lang.reflect.*;
028
029final public class Main {
030        final private static void usage() {
031                System.out.println("java com.aftexsw.util.bzip.Main [-g] [-d] [-h] [-v] [-l] [-t] [-1..-9] filein [fileout]");
032                System.out.println("java com.aftexsw.util.bzip.Main [--help] [--version] [-license]");
033                System.out.println("java com.aftexsw.util.bzip.Main [--decompress] filein [fileout]");
034                System.out.println("-g use GUI to run BZip");
035                System.out.println("-d --decompress decompress the input file (otherwise compress)");
036                System.out.println("-o --stdout direct output to std out");
037                System.out.println("-i --stdin direct input to std in");
038                System.out.println("-h --help display help");
039                System.out.println("-v --version display version");
040                System.out.println("-l --license display license");
041                System.out.println("-t --test test the input file");
042                System.out.println("-1 .. -9 multiple of 100k to use for the block size");
043        }
044
045        final private static void license() {
046                System.out.println("BZip2GUI for Java version 1.0");
047                System.out.println("by Keiron Liddle, Aftex Software <keiron@aftexsw.com>");
048        }
049
050        final private static void version() {
051                System.out.println("BZip2 for Java version 1.0");
052                System.out.println("by Keiron Liddle, Aftex Software <keiron@aftexsw.com>");
053        }
054
055        final public static void gui() {
056                BZipGUI bg = new BZipGUI();
057                bg.setVisible(true);
058        }
059
060        public static void main(String[] args) {
061                try {
062                        Class clazz = Class.forName("com.aftexsw.util.bzip.Mac");
063                        Method meth = clazz.getMethod("setupForMac", new Class[] {});
064                        Boolean bool = (Boolean)meth.invoke(null, new Object[] {});
065                        if(bool.booleanValue())
066                                return;
067                } catch(Throwable t) {}
068                if(args.length == 0) {
069                        usage();
070                        return;
071                }
072                String fileIn, fileOut;
073                boolean compFlag = true;
074                boolean testFlag = false;
075                boolean tostdout = false;
076                boolean fromstdin = false;
077                int blockSize100k = 9;
078                for(int count = 0; count < args.length; count++) {
079                        if(args[count].startsWith("-")) {
080                                if(args[count].equals("--help")) {
081                                        usage();
082                                        return;
083                                } else if(args[count].equals("-h")) {
084                                        usage();
085                                        return;
086                                } else if(args[count].equals("-g")) {
087                                        gui();
088                                        return;
089                                } else if(args[count].equals("--license")) {
090                                        license();
091                                        return;
092                                } else if(args[count].equals("-l")) {
093                                        license();
094                                        return;
095                                } else if(args[count].equals("--version")) {
096                                        version();
097                                        return;
098                                } else if(args[count].equals("-v")) {
099                                        version();
100                                        return;
101                                } else if(args[count].equals("--stdout")) {
102                                        tostdout = true;
103                                } else if(args[count].equals("-o")) {
104                                        tostdout = true;
105                                } else if(args[count].equals("--stdin")) {
106                                        fromstdin = true;
107                                } else if(args[count].equals("-i")) {
108                                        fromstdin = true;
109                                } else if(args[count].equals("-1")) {
110                                        blockSize100k = 1;
111                                } else if(args[count].equals("-2")) {
112                                        blockSize100k = 2;
113                                } else if(args[count].equals("-3")) {
114                                        blockSize100k = 3;
115                                } else if(args[count].equals("-4")) {
116                                        blockSize100k = 4;
117                                } else if(args[count].equals("-5")) {
118                                        blockSize100k = 5;
119                                } else if(args[count].equals("-6")) {
120                                        blockSize100k = 6;
121                                } else if(args[count].equals("-7")) {
122                                        blockSize100k = 7;
123                                } else if(args[count].equals("-8")) {
124                                        blockSize100k = 8;
125                                } else if(args[count].equals("-9")) {
126                                        blockSize100k = 9;
127                                } else if(args[count].equals("--decompress")) {
128                                        compFlag = false;
129                                } else if(args[count].equals("-d")) {
130                                        compFlag = false;
131                                } else if(args[count].equals("-t")) {
132                                        testFlag = true;
133                                } else {}
134                        }
135                        else {
136                                break;
137                        }
138                }
139                if((args.length == 1) || (args[args.length - 2].startsWith("-"))) {
140                        if(args[args.length - 1].startsWith("-")) {
141                                usage();
142                                return;
143                        }
144                        fileIn = args[args.length - 1];
145                        if(compFlag) {
146                                fileOut = fileIn + ".bz2";
147                        } else {
148                                if(fileIn.endsWith(".bz2")) {
149                                        fileOut = fileIn.substring(0, fileIn.length() - 4);
150                                } else {
151                                        fileOut = fileIn + ".out";
152                                }
153                        }
154                } else {
155                        fileIn = args[args.length - 2];
156                        fileOut = args[args.length - 1];
157                }
158                if(testFlag) {
159                        if(BZip.test(fileIn)) {
160                                System.out.println("the file apears to be OK");
161                        } else {
162                                System.out.println("the file appears to be corrupted!");
163                                //System.out.println("try java com.aftexsw.util.bzip.Main -r (file) to recover good blocks");
164                        }
165                }
166                else {
167                        if(tostdout) {
168                                if(fromstdin) {
169                                        if(compFlag) {
170                                                BZip.compress(System.in, System.out, blockSize100k, BZip.dummyProg);
171                                        } else {
172                                                BZip.decompress(System.in, System.out, BZip.dummyProg);
173                                        }
174                                } else {
175                                        if(compFlag) {
176                                                BZip.compress(new File(fileIn), System.out, blockSize100k, BZip.dummyProg);
177                                        } else {
178                                                BZip.decompress(new File(fileIn), System.out, BZip.dummyProg);
179                                        }
180                                }
181                        } else {
182                                if(fromstdin) {
183                                        if(compFlag) {
184                                                BZip.compress(System.in, new File(fileOut), blockSize100k, BZip.dummyProg);
185                                        } else {
186                                                BZip.decompress(System.in, new File(fileOut), BZip.dummyProg);
187                                        }
188                                } else {
189                                        if(compFlag) {
190                                                BZip.compress(new File(fileIn), new File(fileOut), blockSize100k, BZip.dummyProg);
191                                        } else {
192                                                BZip.decompress(new File(fileIn), new File(fileOut), BZip.dummyProg);
193                                        }
194                                }
195                        }
196                }
197                System.out.println("finished BZip");
198        }
199}