3e349f43946dd82003a9a118f5b28f111bb13e98
[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         const char *name;
24         const 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                 printf("\nSelect %sdriver for cards:\n\n", i?"another ":"");
48                 jj = 0;
49                 while(cards[jj].name) {
50                         printf(" (%d) %s\n", jj+1, cards[jj].name);
51                         jj++;
52                 }
53                 do {
54                         printf("\nSelect driver number[1-n] (or enter 'done'): "); fflush(stdout);
55                         scanf("%s", input);
56                 } while (atoi(input) <= 0 && !!strcmp(input, "done"));
57                 type[i] = atoi(input);
58                 i++;
59                 if (!strcmp(input, "done"))
60                         break;
61         }
62
63         if (!i) {
64                 printf("\nNo cards defined!\n");
65                 return(-1);
66         }
67
68         printf("\nEnter LAW audio mode. For a-LAW (default), just enter 0. For u-LAW enter 1.\n[0..n | 0xn]: "); fflush(stdout);
69         scanf("%s", input);
70         lawopt = strtoul(input, NULL, 0);
71         printf("\nEnter debugging flags of mISDN core. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
72         scanf("%s", input);
73         coredebug = strtoul(input, NULL, 0);
74         printf("\nEnter debugging flags of cards. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
75         scanf("%s", input);
76         carddebug = strtoul(input, NULL, 0);
77         printf("\nEnter dsp debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
78         scanf("%s", input);
79         dspdebug = strtoul(input, NULL, 0);
80
81         n = i;
82
83         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);
84         scanf("%s", input);
85         if (!strcmp(input, "0"))
86                 SCPY(input, "");
87         if (!strcmp(input, "1"))
88                 SCPY(input, "/usr/local/lcr/modules");
89         if (input[0]) if (input[strlen(input)-1] != '/')
90                 SCAT(input, "/");
91
92         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);
93         scanf("%s", file);
94         if (!(fp=fopen(file, "w"))) {
95                 fprintf(stderr, "\nError: Failed to open '%s', try again.\n", file);
96                 exit(EXIT_FAILURE);
97         }
98         fprintf(fp, "# rc script for mISDN driver\n\n");
99         fprintf(fp, "case \"$1\" in\n");
100         fprintf(fp, "\tstart|--start)\n");
101         fprintf(fp, "\t\t%s %smISDN_core%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", coredebug);
102         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);
103         j = 0;
104         while(cards[j].name) {
105                 jj = 0;
106                 while (jj < n) {
107                         if (type[jj] == j+1)
108                                 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);
109                         jj++;
110                 }
111                 j++;
112         }
113         fprintf(fp, "\t\tsleep 1\n");
114         fprintf(fp, "\t\t;;\n\n");
115         fprintf(fp, "\tstop|--stop)\n");
116         while(j) {
117                 j--;
118                 jj = 0;
119                 while (jj < n) {
120                         if (type[jj] == j+1)
121                                 fprintf(fp, "\t\trmmod %s\n", cards[j].module);
122                         jj++;
123                 }
124         }
125         fprintf(fp, "\t\trmmod mISDN_dsp\n");
126         fprintf(fp, "\t\trmmod mISDN_core\n");
127         fprintf(fp, "\t\t;;\n\n");
128         fprintf(fp, "\trestart|--restart)\n");
129         fprintf(fp, "\t\tsh $0 stop\n");
130         fprintf(fp, "\t\tsleep 2 # some phones will release tei when layer 1 is down\n");
131         fprintf(fp, "\t\tsh $0 start\n");
132         fprintf(fp, "\t\t;;\n\n");
133         fprintf(fp, "\thelp|--help)\n");
134         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
135         fprintf(fp, "\t\texit 0\n");
136         fprintf(fp, "\t\t;;\n\n");
137         fprintf(fp, "\t*)\n");
138         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
139         fprintf(fp, "\t\texit 2\n");
140         fprintf(fp, "\t\t;;\n\n");
141         fprintf(fp, "esac\n");
142         fclose(fp);
143
144         printf("\nFile '%s' is written to the current directory.\n", file);
145 }
146
147