File: m32install.sl

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (190 lines) | stat: -rw-r--r-- 4,740 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
private variable Script_Version_String = "0.1.1";

require ("cmdopt");
require ("glob");

private variable MANDIR = "$root/share/man/man1";
private variable BINDIR = "$root/bin";
private variable DOCDIR = "$root/share/doc/slrn";
private variable CONFDIR = "$root/etc";
%private variable LOCALEDIR = "$root/share/locale";
private variable SLRN_SLANG_DIR = "$root/share/slrn/slang";

private variable BIN_FILES = ["src/slrn.exe", "src/slrnpull.exe"];
private variable MAN_FILES = ["doc/slrn.1", "doc/slrnpull.1"];
private variable DOC_FILES
  = ["COPYRIGHT", "COPYING", "README", "changes.txt", "@doc/INSTFILES"];
private variable SLANG_FILES = ["@macros/INSTFILES"];
private variable CONF_FILES = NULL;

%---------------------------------------------------------------------------
private define convert_path (path)
{
   return strtrans (path, "/", "\\");
}

private define mkdir_p (dir);
private define mkdir_p (dir)
{
   dir = convert_path (dir);

   if ((-1 == mkdir (dir)) && (errno != EEXIST))
     {
	variable parent = path_dirname (dir);
	if ((-1 == mkdir_p (parent))
	    || (-1 == mkdir (dir)))
	  {
	     () = fprintf (stderr, "Failed to create %s: %s\n",
			   dir, errno_string ());
	     exit (1);
	  }
     }
   return 0;
}

private define run_cmd (cmd)
{
   () = system (cmd);
}

private define install_files_array (file, dir);   %  forward decl
private define install_file (file, dir)
{
   dir = convert_path (dir);
   file = convert_path (file);

   if (file[0] == '@')
     {
	file = file[[1:]];
	variable fp = fopen (file, "r");
	if (fp == NULL)
	  {
	     () = fprintf (stderr, "Unable to open %S\n", file);
	     exit (1);
	  }
	variable files = fgetslines (fp);
	() = fclose (fp);
	files = array_map (String_Type, &strtrim, files);
	files = files[where (files != "")];
	files = files[where (array_map (Int_Type, &strncmp, files, "#", 1))];
	files = files[where (array_map (Int_Type, &strncmp, files, "%", 1))];
	files = array_map (String_Type, &path_concat, path_dirname(file), files);
	install_files_array (files, dir);
	return;
     }

   () = fprintf (stdout, "Installing %s into %s\n", file, dir);
   run_cmd ("copy /y $file $dir"$);
}

private define install_files_array (files, dir)
{
   foreach (files)
     {
	variable file = ();
	variable f = file;
	if (f[0] == '@')
	  f = f[[1:]];
	if (NULL == stat_file (f))
	  {
	     () = fprintf (stderr, "*** WARNING: Unable to stat %s -- skipping it\n", f);
	     continue;
	  }
	install_file (file, dir);
     }
}

private define install_files (pat, dir)
{
   install_files_array (glob (pat), dir);
}

private define install_slrn (root, pullroot)
{
   variable dir;

   dir = _$(BINDIR); () = mkdir_p (dir);
   install_files_array (BIN_FILES, dir);

   dir = _$(SLRN_SLANG_DIR); () = mkdir_p (dir);
   install_files_array (SLANG_FILES, dir);

   dir = _$(MANDIR); () = mkdir_p (dir);
   install_files_array (MAN_FILES, dir);

   dir = _$(DOCDIR); () = mkdir_p (dir);
   install_files_array (DOC_FILES, dir);

   dir = _$(CONFDIR); () = mkdir_p (dir);
   install_files_array (CONF_FILES, dir);

   () = mkdir_p (pullroot);
}

private define strip_drive_prefix (path)
{
   if ((strlen(path) >= 3)
       && (path[1] == ':'))
     path = path[[2:]];

   return path;
}

private define exit_version ()
{
   () = fprintf (stdout, "Version: %S\n", Script_Version_String);
   exit (0);
}

private define exit_usage ()
{
   variable fp = stderr;
   () = fprintf (fp, "Usage: %s [options] install\n", __argv[0]);
   variable opts =
     [
      "Options:\n",
      " -v|--version               Print version\n",
      " -h|--help                  This message\n",
      " --prefix=/install/prefix   Default is /usr\n",
      " --distdir=/path            Default is blank\n",
      " --slrnpull=/path           Default is $prefix/var/spool/news/slrnpull\n",
     ];
   foreach (opts)
     {
	variable opt = ();
	() = fputs (opt, fp);
     }
   exit (1);
}

define slsh_main ()
{
   variable c = cmdopt_new ();
   variable destdir = "";
   variable prefix = "/usr";
   variable slrnpull_root = NULL;
   c.add("h|help", &exit_usage);
   c.add("v|version", &exit_version);
   c.add("destdir", &destdir; type="str");
   c.add("prefix", &prefix; type="str");
   c.add("slrnpull", &slrnpull_root; type="str");

   variable i = c.process (__argv, 1);

   if ((i + 1 != __argc) || (__argv[i] != "install"))
     exit_usage ();

   () = fprintf (stdout, "Using destdir=%s, prefix=%s\n", destdir, prefix);


   if (slrnpull_root == NULL)
     slrnpull_root = "$prefix/var/spool/news/slrnpull"$;

   if (destdir != "")
     {
	prefix = destdir + strip_drive_prefix (prefix);
	slrnpull_root = destdir + strip_drive_prefix (slrnpull_root);
     }

   install_slrn (prefix, slrnpull_root);
}