]> code.delx.au - gnu-emacs-elpa/blob - packages/notes-mode/notesinit
Add notes-mode.
[gnu-emacs-elpa] / packages / notes-mode / notesinit
1 #!/usr/bin/perl -w
2
3 #
4 # notesinit
5 # Copyright (C) 1996-2002 by John Heidemann
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, "/home/johnh/NOTES/BIN"); };
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 5. add code to your .emacs file to load notes-mode
119
120 END
121 ($def_choices, $def) = ('(M/d/s)', 'm');
122 ($def_choices, $def) = ('(m/D/s)', 'd') if (-f "$home_dir/.notesrc");
123 my($go) = query($expl, "Make these changes, describe the changes, or stop $def_choices? ", '[mds]', $def);
124 exit 1 if ($go eq 's');
125
126 sub commands {
127 my($expl, $cmd) = @_;
128 print $expl if (!$use_defaults);
129 if ($go eq 'm') {
130 system($cmd);
131 } else {
132 print "$cmd\n";
133 };
134 }
135
136 #
137 # Ok, the code below is less-than-ideal.
138 # It's somewhat silly to have Perl output the shell commands
139 # rather than just "do it".
140 # The reason is that we to allow the user to look at what's being done.
141 #
142
143 $expl = "\n### changes begin here\n" .
144 "\n### 0. set up your .notesrc\n";
145 commands($expl, "cat >$home_dir/.notesrc <<END
146 dir: $::notes{dir}
147 END
148 ");
149
150 # yuck: the || on the commands is to handle old systems without mkdir -p
151 $expl = "\n### 1. creating $::notes{dir}\n";
152 commands($expl, "mkdir -p $::notes{dir_notilde} || mkdir $::notes{dir_notilde};\nchmod 0700 $::notes{dir_notilde}\n");
153
154 my($heading) = strftime_epoch("%d-%b-%y %A", $now);
155 my($underline) = "-" x length($heading);
156 $expl = "\n### 2. create a sample note for today ($today_pathname)\n";
157 commands($expl, "mkdir `dirname $today_pathname`;\n cat >$today_pathname <<END
158
159 $heading
160 $underline
161
162 * Today
163 -------
164
165 to do list goes here?
166
167
168 * Environment/notes
169 -------------------
170
171 Set up notes with notesinit.
172
173 (To read the manual, run info notes-mode .)
174
175 END
176 ");
177
178 my($mkall) = $PERL . " " . $::notes{bin_dir} . "/mkall";
179 my($crontab_entry) = "0 4 * * * $mkall";
180
181 $expl = "\n### 3. index the default note\n";
182 commands($expl, $mkall);
183
184 $expl = "\n### 4. set up a crontab entry to re-index notes at 4am every morning\n";
185 my($tmpfile) = "$home_dir/notesinit.$$~";
186 # this whole touch thing is to avoid leaving a globally writable crontab
187 commands($expl, "touch $tmpfile;
188 chmod 0600 $tmpfile;
189 echo 'If you do not have a crontab, errors about not being able to open a cron table can be ignored.';
190 crontab -l | sed 's/^\\(.*\\/mkall\\)\$/# \\1/' >>$tmpfile;
191 echo '$crontab_entry' >>$tmpfile;
192 " . $::notes{bin_dir} . "/setcrontab $tmpfile
193 rm -f $tmpfile");
194
195 $expl = "\n### 5. add code to your .emacs file to load notes-mode\n";
196 commands($expl, "grep notes-variables $home_dir/.emacs >/dev/null || echo \"(require 'notes-variables)\" >>$home_dir/.emacs");
197
198 print "\n### changes end here\n" if (!$use_defaults);
199
200 print "\nYou have elected to have the changes DESCRIBED but not made.\n" .
201 "To make the changes yourself, run the commands between\n" .
202 "\"changes begin here\" and \"changes end here\".\n"
203 if ($go eq 'd');
204
205 exit 0;