loopback-install-20040804
[openafs.git] / src / WINNT / install / loopback / instloop.c
1 /*
2
3 Copyright 2004 by the Massachusetts Institute of Technology
4
5 All rights reserved.
6
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of the Massachusetts
12 Institute of Technology (M.I.T.) not be used in advertising or publicity
13 pertaining to distribution of the software without specific, written
14 prior permission.
15
16 M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
18 M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 SOFTWARE.
23
24 */
25
26 #include <windows.h>
27 #include <stdio.h>
28 #define INITGUID
29 #include <guiddef.h>
30 #include <devguid.h>
31 #include <setupapi.h>
32 #include <tchar.h>
33 #include "loopbackutils.h"
34
35 #undef REDIRECT_STDOUT
36
37 static void
38 ShowUsage(void)
39 {
40     printf("instloop [-i [name [ip mask]] | -u]\n\n");
41     printf("    -i  install the %s\n", DRIVER_DESC);
42     _tprintf(_T("        (if unspecified, uses name %s,\n"), DEFAULT_NAME);
43     _tprintf(_T("         ip %s, and mask %s)\n"), DEFAULT_IP, DEFAULT_MASK);    
44     printf("    -u  uninstall the %s\n", DRIVER_DESC);
45 }
46
47 static void
48 DisplayStartup(BOOL bInstall)
49 {
50     printf("%snstalling the %s\n"
51            " (Note: This may take up to a minute or two...)\n",
52            bInstall ? "I" : "Un",
53            DRIVER_DESC);
54     
55 }
56
57 static void
58 DisplayResult(BOOL bInstall, DWORD rc)
59 {
60     if (rc)
61     {
62         printf("Could not %sinstall the %s\n", bInstall ? "" : "un",
63                DRIVER_DESC);
64         SLEEP;
65         PAUSE;
66     }
67 }
68
69
70 int _tmain(int argc, TCHAR *argv[])
71 {
72     DWORD rc = 0;
73 #ifdef REDIRECT_STDOUT
74     FILE *fh = NULL;
75 #endif
76
77     PAUSE;
78
79 #ifdef REDIRECT_STDOUT
80     fh = freopen("instlog.txt","a+", stdout);
81 #endif
82
83     if (argc > 1)
84     {
85         if (_tcsicmp(argv[1], _T("-i")) == 0)
86         {
87             TCHAR* name = DEFAULT_NAME;
88             TCHAR* ip = DEFAULT_IP;
89             TCHAR* mask = DEFAULT_MASK;
90
91             if (argc > 2)
92             {
93                 name = argv[2];
94                 if (argc > 3)
95                 {
96                     if (argc < 5)
97                     {
98                         ShowUsage();
99 #ifdef REDIRECT_STDOUT
100                         fflush(fh); fclose(fh);
101 #endif
102                         return 1;
103                     }
104                     else
105                     {
106                         ip = argv[3];
107                         mask = argv[4];
108                     }
109                 }
110             }
111             DisplayStartup(TRUE);
112                         if(IsLoopbackInstalled()) {
113                                 printf("Loopback already installed\n");
114                                 rc = 0; /* don't signal an error. */
115                         } else {
116                     rc = InstallLoopBack(name, ip, mask);
117                         }
118             DisplayResult(TRUE, rc);
119 #ifdef REDIRECT_STDOUT
120             fflush(fh); fclose(fh);
121 #endif
122             return rc;
123         }
124         else if (_tcsicmp(argv[1], _T("-u")) == 0)
125         {
126             DisplayStartup(FALSE);
127             rc = UnInstallLoopBack();
128             DisplayResult(FALSE, rc);
129 #ifdef REDIRECT_STDOUT
130             fflush(fh); fclose(fh);
131 #endif
132             return rc;
133         }
134         ShowUsage();
135 #ifdef REDIRECT_STDOUT
136         fflush(fh); fclose(fh);
137 #endif
138         return 1;
139     }
140     ShowUsage();
141 #ifdef REDIRECT_STDOUT
142     fflush(fh); fclose(fh);
143 #endif
144     return 0;
145 }
146