ported genrc
[lcr.git] / genrc.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** generate start/stop script                                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include "macro.h"
17
18 int type[256];
19 int coredebug=0, carddebug=0, dspdebug=0;
20 int lawopt=0;
21
22 struct cards {
23         char *name;
24         char *module;
25 } cards[] = {
26 //      { "AVM Fritz PCI (PNP)", "avmfritz"},
27         { "HFC PCI (Cologne Chip)", "hfcpci"},
28         { "HFC-4S / HFC-8S / HFC-E1 (Cologne Chip)", "hfcmulti"},
29         { "HFC-S USB (Cologne Chip)", "hfcsusb"},
30 //      { "HFC-S MINI (Cologne Chip)", "hfcsmini"},
31 //      { "XHFC (Cologne Chip)", "xhfc"},
32 //      { "Sedlbaur FAX", "sedlfax"},
33 //      { "Winbond 6692 PCI", "w6692pci"},
34         {NULL, NULL}
35 };
36
37 int main(void)
38 {
39         FILE *fp;
40         int i = 0, j, jj, n;
41         char input[256], file[256];
42
43         printf("\n\nThis program generates a script, which is used to start/stop/restart mISDN\n");
44         printf("driver. Please select card only once. Mode and options are given by LCR.\n");
45
46         while(1)
47         {
48                 printf("\nSelect %sdriver for cards:\n\n", i?"another ":"");
49                 jj = 0;
50                 while(cards[jj].name)
51                 {
52                         printf(" (%d) %s\n", jj+1, cards[jj].name);
53                         jj++;
54                 }
55                 do
56                 {
57                         printf("\nSelect driver number[1-n] (or enter 'done'): "); fflush(stdout);
58                         scanf("%s", input);
59                 } while (atoi(input) <= 0 && !!strcmp(input, "done"));
60                 type[i] = atoi(input);
61                 i++;
62                 if (!strcmp(input, "done"))
63                         break;
64         }
65
66         if (!i)
67         {
68                 printf("\nNo cards defined!\n");
69                 return(-1);
70         }
71
72         printf("\nEnter LAW audio mode. For a-LAW (default), just enter 0. For u-LAW enter 1.\n[0..n | 0xn]: "); fflush(stdout);
73         scanf("%s", input);
74         lawopt = strtoul(input, NULL, 0);
75         printf("\nEnter debugging flags of mISDN core. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
76         scanf("%s", input);
77         coredebug = strtoul(input, NULL, 0);
78         printf("\nEnter debugging flags of cards. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
79         scanf("%s", input);
80         carddebug = strtoul(input, NULL, 0);
81         printf("\nEnter dsp debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
82         scanf("%s", input);
83         dspdebug = strtoul(input, NULL, 0);
84
85         n = i;
86
87         printf("\nWhere do you like to load the modules from, enter 0 for default, 1 for\n'/usr/local/lcr/modules/' or the full path.\n[0 | 1 | <path>]: "); fflush(stdout);
88         scanf("%s", input);
89         if (!strcmp(input, "0"))
90                 SCPY(input, "");
91         if (!strcmp(input, "1"))
92                 SCPY(input, "/usr/local/lcr/modules");
93         if (input[0]) if (input[strlen(input)-1] != '/')
94                 SCAT(input, "/");
95
96         printf("\n\nFinally tell me where to write the mISDN rc file.\nEnter the name 'mISDN' for current directory.\nYou may want to say '/usr/local/lcr/mISDN' or '/etc/rc.d/mISDN'\n: "); fflush(stdout);
97         scanf("%s", file);
98         if (!(fp=fopen(file, "w")))
99         {
100                 fprintf(stderr, "\nError: Failed to open '%s', try again.\n", file);
101                 exit(EXIT_FAILURE);
102         }
103         fprintf(fp, "# rc script for mISDN driver\n\n");
104         fprintf(fp, "case \"$1\" in\n");
105         fprintf(fp, "\tstart|--start)\n");
106         fprintf(fp, "\t\t%s %smISDN_core%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", coredebug);
107         fprintf(fp, "\t\t%s %smISDN_dsp%s debug=0x%x options=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", dspdebug, lawopt);
108         j = 0;
109         while(cards[j].name)
110         {
111                 jj = 0;
112                 while (jj < n)
113                 {
114                         if (type[jj] == j+1)
115                                 fprintf(fp, "\t\t%s %s%s%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, cards[j].module, input[0]?".ko":"", carddebug);
116                         jj++;
117                 }
118                 j++;
119         }
120         fprintf(fp, "\t\tsleep 1\n");
121         fprintf(fp, "\t\t;;\n\n");
122         fprintf(fp, "\tstop|--stop)\n");
123         while(j)
124         {
125                 j--;
126                 jj = 0;
127                 while (jj < n)
128                 {
129                         if (type[jj] == j+1)
130                                 fprintf(fp, "\t\trmmod %s\n", cards[j].module);
131                         jj++;
132                 }
133         }
134         fprintf(fp, "\t\trmmod mISDN_dsp\n");
135         fprintf(fp, "\t\trmmod mISDN_core\n");
136         fprintf(fp, "\t\t;;\n\n");
137         fprintf(fp, "\trestart|--restart)\n");
138         fprintf(fp, "\t\tsh $0 stop\n");
139         fprintf(fp, "\t\tsleep 2 # some phones will release tei when layer 1 is down\n");
140         fprintf(fp, "\t\tsh $0 start\n");
141         fprintf(fp, "\t\t;;\n\n");
142         fprintf(fp, "\thelp|--help)\n");
143         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
144         fprintf(fp, "\t\texit 0\n");
145         fprintf(fp, "\t\t;;\n\n");
146         fprintf(fp, "\t*)\n");
147         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
148         fprintf(fp, "\t\texit 2\n");
149         fprintf(fp, "\t\t;;\n\n");
150         fprintf(fp, "esac\n");
151         fclose(fp);
152
153         printf("\nFile '%s' is written to the current directory.\n", file);
154 }
155
156