1{ Directory cleaner - command-line interface
2
3  Copyright (C) 2007 Michael Van Canneyt michael@freepascal.org
4
5  This source is free software; you can redistribute it and/or modify it under
6  the terms of the GNU General Public License as published by the Free
7  Software Foundation; either version 2 of the License, or (at your option)
8  any later version.
9
10  This code is distributed in the hope that it will be useful, but WITHOUT ANY
11  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  details.
14
15  A copy of the GNU General Public License is available on the World Wide Web
16  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
17  to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18  Boston, MA 02110-1335, USA.
19}
20program cleandir;
21
22{$mode objfpc}{$H+}
23
24uses
25  Classes,
26  SysUtils,
27  CustApp,
28  dircleaner;
29
30ResourceString
31  SErrNoConfig      = 'No configuration file found.';
32  SErrInvalidConfig = 'Configuration file "%s" does not exist.';
33  SErrNoDirectories = 'No directories to clean';
34  SUsage0 = 'Usage: %s [options] directory1 ... directoryN';
35  SUsage1 = 'Where options is one or more of:';
36  SUsageh = '-h --help         This help message';
37  SUsagec = '-c --config=file  Filename for configuration data';
38  SUsagev = '-v --verbose      Log more messages';
39  SUsagel = '-l --logonly      Only log actions, do not execute commands';
40  SUsageq = '-q --quiet        Do not log actions (disables -l)';
41  SUsages = '-s --stoponerror  Stop at the first error.';
42  SUsager = '-r --recurse      Recurse directories specified on command-line';
43  SUsaged = '-d --standarddirs Clean standard dirs from configuration file';
44
45
46
47
48Type
49
50  { TCleanDirs }
51  TRunMode = (rmHelp,rmExecute);
52
53  TCleanDir = Class (TCustomApplication)
54  private
55    FCleaner: TCleanDirs;
56    Function ProcessCommandLine : TRunMode;
57  Public
58    Constructor Create(AOwner : TComponent); override;
59    Procedure DoRun; override;
60    Procedure Usage;
61    Procedure OnLog(Msg : String);
62    Property Cleaner : TCleanDirs Read FCleaner;
63  end;
64
65{ TCleanDir }
66
67Var
68  Application : TCleanDir;
69
70function TCleanDir.ProcessCommandLine: TRunMode;
71
72Const
73  OptCount = 8;
74  Opts : Array[1..OptCount] of string =
75    ('help','config','verbose','logonly','quiet',
76     'stoponerror','recurse','standarddirs');
77  OptChars = 'hcvlqsrd';
78
79Var
80  FN : String;
81  OptL,Dirs : TStrings;
82  I : integer;
83
84begin
85  Result:=rmHelp;
86  If HasOption('h','help') then
87    Exit;
88  Dirs:=TStringList.Create;
89  try
90    OptL:=TStringList.Create;
91    try
92      For I:=1 to OptCount do
93        OptL.Add(Opts[i]);
94      FN:=CheckOptions(OptChars,OptL,Nil,Dirs);
95    finally
96      OptL.Free;
97    end;
98    If (FN<>'')  then
99      begin
100      Writeln(StdErr,FN);
101      Exit;
102      end;
103    FN:='';
104    If HasOption('c','config') then
105      begin
106      FN:=Self.GetOptionValue('c','config');
107      If Not FileExists(FN) then
108        begin
109        Writeln(StdErr,format(SErrInvalidConfig,[FN]));
110        Exit;
111        end;
112      end
113    else
114      begin
115      FN:=GetAppConfigFile(false,false);
116      If Not FileExists(FN) then
117        begin
118        FN:=GetAppConfigFile(True,false);
119        If Not FileExists(FN) then
120          FN:='';
121        end;
122      If (FN='') then
123        begin
124        Writeln(StdErr,SErrNoConfig);
125        Exit;
126        end;
127      end;
128    FCleaner.LoadFromFile(FN);
129    If Not HasOption('q','quiet') then
130      begin
131      FCleaner.OnLog:=@Self.OnLog;
132      FCleaner.LogAllFiles:=HasOption('v','verbose');
133      FCleaner.LogOnly:=HasOption('l','logonly');
134      end;
135    If Hasoption('s','stoponerror') then
136      FCleaner.StopOnError:=True;
137    if Not HasOption('d','standarddirs') then
138      FCleaner.Directories.Clear;
139    If (Dirs.Count>0) then
140      begin
141      For I:=0 to Dirs.Count-1 do
142        With FCleaner.Directories.AddDirectory do
143          begin
144          Path:=Dirs[i];
145          Enabled:=True;
146          Recurse:=HasOption('r','recurse');
147          end;
148      end;
149    If (FCleaner.Directories.Count=0) then
150      Writeln(StdErr,SErrNoDirectories)
151    else
152      Result:=rmExecute;
153  Finally
154    Dirs.Free;
155  end;
156end;
157
158constructor TCleanDir.Create(AOwner: TComponent);
159begin
160  inherited Create(AOwner);
161  FCleaner:=TCleanDirs.Create(Self);
162  StopOnException:=True;
163end;
164
165procedure TCleanDir.DoRun;
166begin
167  Case ProcessCommandLine of
168    rmHelp    : Usage;
169    rmExecute : FCleaner.Execute;
170  end;
171  Terminate;
172end;
173
174procedure TCleanDir.Usage;
175begin
176  Writeln(Format(SUsage0,[ExtractFileName(paramstr(0))]));
177  Writeln(SUsage1);
178  Writeln(SUsagec);
179  Writeln(SUsaged);
180  Writeln(SUsageh);
181  Writeln(SUsagel);
182  Writeln(SUsageq);
183  Writeln(SUsager);
184  Writeln(SUsages);
185  Writeln(SUsagev);
186end;
187
188procedure TCleanDir.OnLog(Msg: String);
189begin
190  Writeln(Msg);
191end;
192
193begin
194  OnGetApplicationName:=@CleanDirApp;
195  Application:=TCleandir.Create(nil);
196  Application.Title:='cleandisk';
197  Application.Run;
198  Application.Free;
199end.
200
201