backup
[lcr.git] / genrc.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
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 "save.h"
17
18 char mode[256];
19 int type[256];
20 int ptp[256];
21 int port[256];
22 char name[256];
23 int coredebug=0, carddebug=0, l1debug=0, l2debug=0, l3debug=0, dspdebug=0;
24 int lawopt=0;
25
26 static struct cards {
27         char *card;
28         char *module;
29         int ntmode;
30         int isac;
31         int ports;
32 } cards[] = {
33         { "AVM Fritz PCI (PNP)", "avmfritz", 0, 1, 1},
34         { "HFC PCI (Cologne Chip)", "hfcpci", 1, 0, 1},
35         { "HFC-4S 4 S/T Ports (Cologne Chip)", "hfcmulti", 1, 0, 4},
36         { "HFC-8S 8 S/T Ports (Cologne Chip)", "hfcmulti", 1, 0, 8},
37         { "HFC-E1 1 E1 Port (Cologne Chip)", "hfcmulti", 1, 0, 1},
38         { "HFC-S USB (Cologne Chip)", "hfcsusb", 1, 0, 1},
39         { "HFC-S MINI (Cologne Chip)", "hfcsmini", 1, 0, 1},
40         { "XHFC (Cologne Chip)", "xhfc", 1, 0, 1},
41         { "Sedlbaur FAX", "sedlfax", 0, 1, 1},
42         { "Winbond 6692 PCI", "w6692pci", 0, 0, 1},
43         { NULL, NULL, 0, 0},
44 };
45
46 int main(void)
47 {
48         FILE *fp;
49         int i = 0, j, jj, n, anyte = 0, remove_isac;
50         char input[256];
51         char protocol[1024], layermask[1024], types[256];
52
53         printf("\n\nThis program generates a script, which is used to start/stop/restart mISDN\n");
54         printf("driver. All configuration of cards is done for using with the PBX.\n");
55
56         while(i < (int)sizeof(mode)) /* number of cards */
57         {
58                 moreports:
59                 do
60                 {
61                         printf("\nEnter mode of isdn port #%d. Should it run in NT-mode (for internal\nphones), or in TE-mode (for external lines)? If you do not like to add more\ncards, say 'done'.\n[nt | te | done]: ", i+1); fflush(stdout);
62                         scanf("%s", input);
63                 } while (input[0]!='t' && input[0]!='n' && input[0]!='d');
64                 mode[i] = input[0];
65                 if (mode[i] == 'd')
66                         break;
67                 ptp[i] = 0;
68                 do
69                 {
70                         printf("\nIs your port #%d connected to point-to-multipoint line/phone, which supports multiple\ntelephones (Mehrgeräteanschluss) OR is it a point-to-point link which is used\nfor PBX and supports extension dialing (Anlagenanschluss)?\n[ptp | ptm]: ", i+1); fflush(stdout);
71                         scanf("%s", input);
72                 } while (!!strcmp(input,"ptp") && !!strcmp(input,"ptm"));
73                 ptp[i] = (input[2]=='p')?1:0;
74                 anyte = 1;
75
76                 if (!i)
77                 {
78                         askcard:
79                         do
80                         {
81                                 printf("\nSelect driver of ISDN port #%d.\n\n", i+1);
82                                 jj = 0;
83                                 while(cards[jj].card)
84                                 {
85                                         if (cards[jj].ntmode || mode[i]!='n')
86                                                 printf(" (%d) %s\n", jj+1, cards[jj].card);
87                                         jj++;
88                                 }
89                                 printf("\n%sSelect card number[1-n]: ", (mode[i]=='n')?"Your card will run in NT-mode. The shown cards are capable of providing\nhardware layer for NT-mode.\n":""); fflush(stdout);
90                                 scanf("%s", input);
91                         } while (atoi(input) <= 0);
92                         type[i] = atoi(input);
93                         port[i] = 1;
94                         j = 0;
95                         while(j < jj)
96                         {
97                                 if (j+1==type[i] && (cards[j].ntmode || mode[i]!='n'))
98                                         break;
99                                 j++;
100                         }
101                         if (j == jj)
102                         {
103                                 printf("\n\nWrong selection, please try again.\n");
104                                 goto askcard;
105                         }
106                 } else
107                 if (cards[type[i-1]-1].ports == port[i-1])
108                         goto askcard;
109                 else {
110                         type[i] = type[i-1];
111                         port[i] = port[i-1]+1;
112                         printf("\nUsing port %d of card '%s'.", port[i], cards[type[i]-1].card);
113                 }
114
115                 printf("\n\n\nSummary: Port #%d of type %s will run in %s-mode and %s-mode.\n", i+1, cards[type[i]-1].card, (mode[i]=='n')?"NT":"TE", (ptp[i])?"point-to-point":"point-to-multipoint");
116
117                 i++;
118         }
119
120         if (!i)
121         {
122                 printf("\nNo ports/cards defined!\n");
123                 return(-1);
124         }
125         if (cards[type[i-1]-1].ports > port[i-1])
126         {
127                 printf("\nNot all ports for the last card are defined. Please do that even if they will be not\nused! Select 'NT-mode' for these unused ports.\n");
128                 goto moreports;
129         }
130
131         printf("\nEnter LAW audio mode. For a-LAW (default), just enter 0. For u-LAW enter 1.\n[0..n | 0xn]: "); fflush(stdout);
132         scanf("%s", input);
133         lawopt = strtoul(input, NULL, 0);
134         printf("\nEnter debugging flags of mISDN core. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
135         scanf("%s", input);
136         coredebug = strtoul(input, NULL, 0);
137         printf("\nEnter debugging flags of cards. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
138         scanf("%s", input);
139         carddebug = strtoul(input, NULL, 0);
140         printf("\nEnter l1 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
141         scanf("%s", input);
142         l1debug = strtoul(input, NULL, 0);
143         printf("\nEnter l2 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
144         scanf("%s", input);
145         l2debug = strtoul(input, NULL, 0);
146         printf("\nEnter l3 debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
147         scanf("%s", input);
148         l3debug = strtoul(input, NULL, 0);
149         printf("\nEnter dsp debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
150         scanf("%s", input);
151         dspdebug = strtoul(input, NULL, 0);
152
153         n = i;
154
155         printf("\nWhere do you like to load the modules from, enter 0 for default, 1 for\n'/usr/local/pbx/modules/' or the full path.\n[0 | 1 | <path>]: "); fflush(stdout);
156         scanf("%s", input);
157         if (!strcmp(input, "0"))
158                 SCPY(input, "");
159         if (!strcmp(input, "1"))
160                 SCPY(input, "/usr/local/pbx/modules");
161         if (input[0]) if (input[strlen(input)-1] != '/')
162                 SCAT(input, "/");
163
164         printf("\n\nFinally tell me where to write the mISDN rc file.\Enter the name 'mISDN' for current directory.\nYou may want to say '/usr/local/pbx/mISDN' or '/etc/rc.d/mISDN'\n: "); fflush(stdout);
165         scanf("%s", name);
166         if (!(fp=fopen(name, "w")))
167         {
168                 fprintf(stderr, "\nError: Failed to open '%s', try again.\n", name);
169                 exit(-1);
170         }
171         fprintf(fp, "# rc script for mISDN driver\n\n");
172         fprintf(fp, "case \"$1\" in\n");
173         fprintf(fp, "\tstart|--start)\n");
174         fprintf(fp, "\t\t%s %smISDN_core%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, input[0]?".ko":"", coredebug);
175         if (anyte)
176         {
177                 fprintf(fp, "\t\t%s %smISDN_l1%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, input[0]?".ko":"", l1debug);
178                 fprintf(fp, "\t\t%s %smISDN_l2%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, input[0]?".ko":"", l2debug);
179                 fprintf(fp, "\t\t%s %sl3udss1%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, input[0]?".ko":"", l3debug);
180         }
181         fprintf(fp, "\t\t%s %smISDN_dsp%s debug=0x%x options=0x%x\n", input[0]?"insmod -f":"modprobe", input, input[0]?".ko":"", dspdebug, lawopt);
182         j = 0;
183         while(cards[j].card)
184         {
185                 protocol[0] = layermask[0] = types[0] = '\0';
186                 i = 0;
187                 while(i < n)
188                 {
189                         if (j+1 == type[i])
190                         {
191                                 if (!strcmp(cards[j].module, "hfcmulti") && port[i]==1)
192                                         UPRINT(strchr(types,'\0'), "0x%x,", cards[j].ports+((lawopt)?0x100:0)+0x200);
193                                 UPRINT(strchr(protocol,'\0'), "0x%x,", ((mode[i]=='n')?0x12:0x2) + (ptp[i]?0x20:0x0));
194                                 UPRINT(strchr(layermask,'\0'), "0x%x,", (mode[i]=='n')?0x3:0x0f);
195                         }
196                         i++;
197                 }
198                 if (protocol[0])
199                 {
200                         protocol[strlen(protocol)-1] = '\0';
201                         layermask[strlen(layermask)-1] = '\0';
202                         if (types[0])
203                         {
204                                 types[strlen(types)-1] = '\0';
205                                 fprintf(fp, "\t\t%s %s%s%s type=%s protocol=%s layermask=%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, cards[j].module, input[0]?".ko":"", types, protocol, layermask, carddebug);
206                         } else
207                                 fprintf(fp, "\t\t%s %s%s%s protocol=%s layermask=%s debug=0x%x\n", input[0]?"insmod -f":"modprobe", input, cards[j].module, input[0]?".ko":"", protocol, layermask, carddebug);
208                 }
209                 j++;
210         }
211         fprintf(fp, "\t\tsleep 1\n");
212         fprintf(fp, "\t\t;;\n\n");
213         fprintf(fp, "\tstop|--stop)\n");
214         remove_isac = 0;
215         j = 0;
216         while(cards[j].card)
217         {
218                 protocol[0] = 0;
219                 i = 0;
220                 while(i < n)
221                 {
222                         if (j+1 == type[i])
223                         {
224                                 protocol[0] = 1;
225                         }
226                         i++;
227                 }
228                 if (protocol[0])
229                 {
230                         fprintf(fp, "\t\trmmod %s\n", cards[j].module);
231                         if (cards[j].isac)
232                                 remove_isac = 1;
233                 }
234                 j++;
235         }
236         if (remove_isac)
237         {
238                 fprintf(fp, "\t\trmmod mISDN_isac\n");
239         }
240         fprintf(fp, "\t\trmmod mISDN_dsp\n");
241         if (anyte)
242         {
243                 fprintf(fp, "\t\trmmod l3udss1\n");
244                 fprintf(fp, "\t\trmmod mISDN_l2\n");
245                 fprintf(fp, "\t\trmmod mISDN_l1\n");
246         }
247         fprintf(fp, "\t\trmmod mISDN_core\n");
248         fprintf(fp, "\t\t;;\n\n");
249         fprintf(fp, "\trestart|--restart)\n");
250         fprintf(fp, "\t\tsh $0 stop\n");
251         fprintf(fp, "\t\tsleep 2 # some phones will release tei when layer 1 is down\n");
252         fprintf(fp, "\t\tsh $0 start\n");
253         fprintf(fp, "\t\t;;\n\n");
254         fprintf(fp, "\thelp|--help)\n");
255         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
256         fprintf(fp, "\t\texit 0\n");
257         fprintf(fp, "\t\t;;\n\n");
258         fprintf(fp, "\t*)\n");
259         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
260         fprintf(fp, "\t\texit 2\n");
261         fprintf(fp, "\t\t;;\n\n");
262         fprintf(fp, "esac\n");
263         fclose(fp);
264
265         printf("\nFile '%s' is written to the current directory.\n", name);
266 }
267
268