]> code.delx.au - gnu-emacs-elpa/blob - packages/notes-mode/notesinit
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / notes-mode / notesinit
1 #!/usr/bin/perl -w
2
3 #
4 # notesinit
5 # Copyright (C) 1996-2002,2012 Free Software Foundation, Inc.
6 # $Id: notesinit,v 1.12 2006/01/14 22:52:56 johnh Exp $
7 #
8
9 sub usage {
10 print STDERR <<END;
11 usage: $0 [-D]
12
13 This program sets up notes mode for the first time.
14 It typically runs interactively.
15
16 Options:
17 -D use all defaults (also turns off prompting)
18 END
19 exit 1;
20 }
21
22 require 5.000;
23 use strict;
24 use Getopt::Long;
25
26 my(%opts);
27 &GetOptions(\%opts, qw(D));
28 &usage if ($#ARGV >= 0 && $ARGV[0] eq '-?');
29 my($use_defaults) = defined($opts{'D'});
30
31 my($PERL) = $^X; # location of perl executable
32
33
34 use POSIX;
35 BEGIN { unshift(@INC, $ENV{'NOTES_BIN_DIR'}); };
36 use NotesVars;
37 use strict;
38
39
40 sub query {
41 my($lc) = 1;
42 if ($_[0] eq '-nolc') {
43 $lc = undef;
44 shift @_;
45 };
46 my($expl, $query, $valid_regexp, $default) = @_;
47 return $default if ($use_defaults);
48 print $expl;
49 my($a);
50 for (;;) {
51 print $query;
52 $a = <>;
53 chomp $a;
54 $a = lc($a) if ($lc);
55 return $default if ($a eq '');
56 return $a if ($a =~ /$valid_regexp/);
57 print "I didn't understand your answer `$a'.\n";
58 };
59 }
60
61 my($expl);
62 $expl = <<END;
63 notesinit will set up notes-mode for the first time.
64
65 What is notes mode? Texinfo documentation should be installed on your
66 system. Documentation is also available on the web at
67 <http://www.isi.edu/~johnh/SOFTWARE/NOTES_MODE/>. Everything you will
68 be asked here is discussed in greater detail in the documentation.
69
70 END
71
72 my($home_dir) = ((getpwuid($<))[7]);
73 my($def_choices, $def) = ('(Y/n)', 'y');
74 if (-f "$home_dir/.notesrc") {
75 die "$0: will not override existing .notesrc with -D option.\n"
76 if ($use_defaults);
77 ($def_choices, $def) = ('(y/N)', 'n');
78 print "WARNING: you already appear to have notes configured.\n\n"
79 };
80
81 my($a) = query($expl, "Do you want to set up notes mode now $def_choices? ", '(y|n)', $def);
82 if ($a ne 'y') {
83 print "\nnotesinit exited.\n\n";
84 exit 1;
85 };
86
87 $expl = <<END;
88
89 Notes are stored in two-level hierarchy of directories.
90 For example:
91 ~/NOTES/199603/960329
92 ^^^^^^-- a notes file (the date, in YYMMDD)
93 ^^^^^^--------- one subdirectory per month (form: YYYYMM)
94 ^^^^^---------------- the parent directory of everything
95
96 END
97 $::notes{dir} = query("-nolc", $expl, "What should the parent directory be (default: ~/NOTES)? ", '.', '~/NOTES');
98 # fix ~ (in honor of Cliffette's "yes, I have no tilde")
99 $::notes{dir_notilde} = $::notes{dir};
100 $::notes{dir_notilde} =~ s@^~/@$home_dir/@;
101 $::notes{dir} =~ s@^$home_dir/@~/@;
102
103
104 my($now) = time;
105 my($today_pathname) = epoch_to_pathname($now);
106
107 $expl = <<END;
108
109 notesinit can now set-up your environment for notes.
110 It will take the following steps:
111
112 0. set up your .notesrc
113 1. create $::notes{dir}
114 2. create a sample note for today ($today_pathname)
115 3. index the default note
116 4. set up a crontab entry to re-index notes at 4am every morning
117 (by running mkall)
118
119 END
120 ($def_choices, $def) = ('(M/d/s)', 'm');
121 ($def_choices, $def) = ('(m/D/s)', 'd') if (-f "$home_dir/.notesrc");
122 my($go) = query($expl, "Make these changes, describe the changes, or stop $def_choices? ", '[mds]', $def);
123 exit 1 if ($go eq 's');
124
125 sub commands {
126 my($expl, $cmd) = @_;
127 print $expl if (!$use_defaults);
128 if ($go eq 'm') {
129 system($cmd);
130 } else {
131 print "$cmd\n";
132 };
133 }
134
135 #
136 # Ok, the code below is less-than-ideal.
137 # It's somewhat silly to have Perl output the shell commands
138 # rather than just "do it".
139 # The reason is that we to allow the user to look at what's being done.
140 #
141
142 $expl = "\n### changes begin here\n" .
143 "\n### 0. set up your .notesrc\n";
144 commands($expl, "cat >$home_dir/.notesrc <<END
145 dir: $::notes{dir}
146 END
147 ");
148
149 $expl = "\n### 1. creating $::notes{dir}\n";
150 commands($expl, "mkdir -p $::notes{dir_notilde};\nchmod 0700 $::notes{dir_notilde}\n");
151
152 my($heading) = strftime_epoch("%d-%b-%y %A", $now);
153 my($underline) = "-" x length($heading);
154 $expl = "\n### 2. create a sample note for today ($today_pathname)\n";
155 commands($expl, "mkdir -p `dirname $today_pathname`;\n cat >$today_pathname <<END
156
157 $heading
158 $underline
159
160 * Today
161 -------
162
163 to do list goes here?
164
165
166 * Environment/notes
167 -------------------
168
169 Set up notes with notesinit.
170
171 (To read the manual, run info notes-mode .)
172
173 END
174 ");
175
176 my($mkall) = $PERL . " " . $::notes{bin_dir} . "/mkall";
177 my($crontab_entry) = "0 4 * * * $mkall";
178
179 $expl = "\n### 3. index the default note\n";
180 commands($expl, $mkall);
181
182 $expl = "\n### 4. set up a crontab entry to re-index notes at 4am every morning\n";
183 my($tmpfile) = "$home_dir/notesinit.$$~";
184 # this whole touch thing is to avoid leaving a globally writable crontab
185 commands($expl, "touch $tmpfile;
186 chmod 0600 $tmpfile;
187 echo 'If you do not have a crontab, errors about not being able to open a cron table can be ignored.';
188 crontab -l | sed 's/^\\(.*\\/mkall\\)\$/# \\1/' >>$tmpfile;
189 echo '$crontab_entry' >>$tmpfile;
190 " . $::notes{bin_dir} . "/setcrontab $tmpfile
191 rm -f $tmpfile");
192
193 print "\n### changes end here\n" if (!$use_defaults);
194
195 print "\nYou have elected to have the changes DESCRIBED but not made.\n" .
196 "To make the changes yourself, run the commands between\n" .
197 "\"changes begin here\" and \"changes end here\".\n"
198 if ($go eq 'd');
199
200 exit 0;