about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--dev/git/index.html33
-rw-r--r--index.html2
-rw-r--r--tools/nginx.html162
-rw-r--r--tools/scripts/pkgmk-test.conf4
-rw-r--r--tools/vim.html23
-rw-r--r--tools/x.html2
6 files changed, 170 insertions, 56 deletions
diff --git a/dev/git/index.html b/dev/git/index.html
index b41f7a1..4e61822 100644
--- a/dev/git/index.html
+++ b/dev/git/index.html
@@ -17,6 +17,24 @@
        $ git config --global user.email user@devbox
     </pre>
 
+    <p>If you want to disable pager / paging you can set globally;</p>
+
+    <pre>
+    $ git config --global core.pager cat
+    </pre>
+
+    <p>Paging based on output size with less;</p>
+
+    <pre>
+    git config --global --replace-all core.pager "less -F -X"
+    </pre>
+
+    <p>Example to disable for just one subcommand and local repository;</p>
+
+    <pre>
+    $ git config pager.diff cat
+    </pre>
+
     <p>Assumptions of this document;</p>
 
     <ul>
@@ -51,6 +69,12 @@
 
     <p>Main Branches;</p>
 
+    <pre>
+    ___A___________AB_____ Master
+        \___B_____/ \____  Develop
+
+    </pre>
+
     <dl>
         <dt>master</dt>
         <dd>Current official stable release history.</dd>
@@ -60,6 +84,15 @@
 
     <p>Add-on Branches;</p>
 
+    <pre>
+    _A_________________AFR___________________AFRHR Master
+     \            _AFR_/   \        _AFRHR_/\      Release
+      \          /          \_AFRH_/         \     Hotfix
+       \________/_____________________________\__  Develop
+         \_F_/     \_F1_/                          Feature
+
+    </pre>
+
     <dl>
         <dt>feature (f-)</dt>
         <dd>New features, improvement proposal, tests, etc...</dd>
diff --git a/index.html b/index.html
index 1368ebe..41d7690 100644
--- a/index.html
+++ b/index.html
@@ -38,7 +38,7 @@
         <p>Version;</p>
 
         <pre>
-        rev 0.4.0
+        rev 0.4.1
         </pre>
 
         <a href="links.html">Links</a> contains relevant
diff --git a/tools/nginx.html b/tools/nginx.html
index 7094ac7..d64356d 100644
--- a/tools/nginx.html
+++ b/tools/nginx.html
@@ -60,11 +60,11 @@
         <p>First run dryrun to test if everything is ok;</p>
 
         <pre>
-        # certbot certonly --dry-run --email user@mail.org --webroot -w /srv/www/c9-pmwiki/pub/cert/-d hive.gnu.systems
+        # certbot certonly --dry-run --email user@mail.org --webroot -w /srv/www/c9-pmwiki/pub/cert/-d example.sub.domain
         </pre>
 
         <pre>
-        # certbot certonly --email user@mail.org --webroot -w /srv/www/c9-pmwiki/pub/cert/-d hive.gnu.systems
+        # certbot certonly --email user@mail.org --webroot -w /srv/www/c9-pmwiki/pub/cert/-d example.sub.domain
         </pre>
 
         <h3 id="mancert">2.2. Self certificate</h2>
@@ -175,7 +175,7 @@
         1024
         </pre>
 
-       <p>Example of http block with ssl configured;</p>
+        <p>Edit ngnix configutarion;</p>
 
         <pre>
         #
@@ -183,7 +183,7 @@
         #
 
 
-        user nginx;
+        user www;
         worker_processes auto;
 
         error_log /var/log/nginx/error.log;
@@ -195,34 +195,31 @@
             worker_connections  1024;
         }
 
-
         http {
             include       mime.types;
             default_type  application/octet-stream;
 
-            ##
-            # SSL Settings
-            ##
-            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
-            ssl_prefer_server_ciphers on;
-
-            # ssl on;
-            ssl_certificate /etc/ssl/certs/nginx.crt;
-            ssl_certificate_key /etc/ssl/keys/nginx.key;
-
             #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
             #                  '$status $body_bytes_sent "$http_referer" '
             #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
-            access_log /var/log/nginx/access.log;
-            error_log  /var/log/nginx/error.log;
-
             sendfile        on;
             #tcp_nopush     on;
 
-            keepalive_timeout  65;
-            client_body_timeout 12;
-            client_header_timeout 12;
+            # Allow attach iso to wiki
+            #client_max_body_size 8M;
+            client_max_body_size 30M;
+            #keepalive_timeout  65;
+            keepalive_timeout  120;
+            #client_body_timeout 12;
+            client_body_timeout 24;
+            #client_header_timeout 12;
+            client_header_timeout 24;
+
+            #client_max_body_size 10000M;
+            #keepalive_timeout  10000;
+            #client_body_timeout 10000;
+            #client_header_timeout 10000;
             send_timeout 65;
 
 
@@ -235,15 +232,6 @@
             gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 
 
-            ##
-            # Virtual Host Configs
-            ##
-            server {
-                listen         80 default_server;
-                server_name _;
-                return         301 https://$host$request_uri;
-            }
-
             include /etc/nginx/conf.d/*.conf;
             include /etc/nginx/sites-enabled/*.conf;
 
@@ -294,48 +282,113 @@
 
         <p> This server is configured in a way that
         root serves pmwiki and /tasks serves flyspray. In order to
-        flyspray to link correctly change index is needed;</p>
+        flyspray to link correctly change index is needed. Create /etc/nginx/sites-enabled/example.sub.domain.conf;</p>
 
         <pre>
         server {
+
             listen 443 ssl;
-            # listen [::]:443 ssl;
+            listen 80;
+            server_name example.sub.domain;
+
+            #  listen [::]:443 ssl http2;
+            ssl_certificate /etc/letsencrypt/live/example.sub.domain/fullchain.pem;
+            ssl_certificate_key /etc/letsencrypt/live/example.sub.domain/privkey.pem;
+            ssl_trusted_certificate /etc/letsencrypt/live/example.sub.domain/chain.pem;
+
+            ssl_session_timeout 1d;
+            ssl_session_cache shared:SSL:50m;
+            ssl_session_tickets off;
+            ssl_protocols TLSv1.2;
+            ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
+            ssl_prefer_server_ciphers on;
+            add_header Strict-Transport-Security max-age=15768000;
+            ssl_stapling on;
+            ssl_stapling_verify on;
 
-            server_name c9.core;
+            access_log /var/log/nginx/example_access.log;
+            error_log  /var/log/nginx/example_error.log;
 
-            root /srv/www/default;
+            root /srv/www/;
 
-            location /distfiles {
-                alias /usr/ports/distfiles;
+            location /mirror {
+                #alias /usr/ports/releases;
+                proxy_pass http://10.0.0.3:80/;
             }
 
+            location /builder {
+                rewrite ^/blog(.*) /$1 break;
+                proxy_pass http://10.0.0.3:80;
+            }
 
-            location /tasks {
+            location /doc {
+                alias /srv/www/doc;
+                index index.html;
+            }
+
+            location /git/static {
+                # static files (png/css) served from /usr/share/gitweb/static
+                alias /srv/www/gitweb/static;
+            }
+
+            location /git {
+                alias /srv/www/gitweb;
+                index gitweb.cgi;
+                fastcgi_split_path_info      ^/git()(/?.+)$;
+                fastcgi_param GITWEB_CONFIG  /etc/gitweb.conf;
+                fastcgi_param DOCUMENT_ROOT  /srv/www/gitweb;
+                fastcgi_param SCRIPT_NAME    /gitweb.cgi$fastcgi_path_info;
+
+                include fastcgi_params;
+                fastcgi_pass unix:/var/run/fcgiwrap.sock;
+            }
+
+            location /chat {
                 index index.php;
-                alias /srv/www/default/flyspray;
+                alias /srv/www/chat;
                 try_files $uri $uri/ index.php$is_args$args;
             }
 
-            location ~  ^/tasks(.+\.php)$ {
-                alias /srv/www/default/flyspray;
+            location ~  ^/chat(.+\.php)$ { ### This location block was the solution
+                alias /srv/www/chat;
+                fastcgi_split_path_info ^(.+\.php)(/.+)$;
+                fastcgi_index index.php;
+                try_files $uri /index.php =404;	
+                include /etc/nginx/fastcgi_params;
+                fastcgi_param SCRIPT_FILENAME $document_root$1;
+                # fastcgi_pass unix:/var/run/php5-fpm.sock;
+                fastcgi_pass 127.0.0.1:9000;
+            }
+
+
+            location /task {
+                index index.php;
+                alias /srv/www/flyspray;
+                try_files $uri $uri/ index.php$is_args$args;
+            }
 
+            location ~  ^/task(.+\.php)$ { ### This location block was the solution
+                alias /srv/www/flyspray;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_index index.php;
-                try_files $uri /index.php =404;
+                try_files $uri /index.php =404;	
                 include /etc/nginx/fastcgi_params;
                 fastcgi_param SCRIPT_FILENAME $document_root$1;
                 # fastcgi_pass unix:/var/run/php5-fpm.sock;
                 fastcgi_pass 127.0.0.1:9000;
             }
 
-            location / {
-                alias /srv/www/default/pmwiki/;
-                index pmwiki.php
+            location /pub {
+                alias /srv/www/pmwiki/pub;
+            }
+            location /wiki {
+                alias /srv/www/pmwiki/;
+                index pmwiki.php;
                 try_files $uri $uri/ /pmwiki.php$is_args$args;
             }
-
-            location ~ \.php$ {
-                alias /srv/www/default/pmwiki;
+            location ~  ^/wiki(.+\.php)$ {
+                alias /srv/www/pmwiki;
+                index pmwiki.php;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_index pmwiki.php;
                 try_files $uri /pmwiki.php =404;
@@ -344,6 +397,21 @@
                 # fastcgi_pass unix:/var/run/php5-fpm.sock;
                 fastcgi_pass 127.0.0.1:9000;
             }
+
+            # ACME challenge
+            location ^~ /.well-known {
+                allow all;
+                alias /srv/www/pmwiki/pub/cert/.well-known/;
+                default_type "text/plain";
+                try_files $uri =404;
+            }
+
+            location / {
+                alias /srv/www/frontpage/;
+                index index.html;
+                try_files $uri $uri/ /index.html$is_args$args;
+            }
+
         }
         </pre>
 
diff --git a/tools/scripts/pkgmk-test.conf b/tools/scripts/pkgmk-test.conf
index 715ac01..c5f70e4 100644
--- a/tools/scripts/pkgmk-test.conf
+++ b/tools/scripts/pkgmk-test.conf
@@ -4,11 +4,11 @@
 
 source /etc/pkgmk.conf
 
-#PKGMK_SOURCE_MIRRORS=()
+PKGMK_SOURCE_MIRRORS=(http://c1.ank/distfiles/)
 PKGMK_SOURCE_DIR="$PWD"
 PKGMK_PACKAGE_DIR="$PWD"
 PKGMK_WORK_DIR="$PWD/work"
-# PKGMK_DOWNLOAD="no"
+PKGMK_DOWNLOAD="yes"
 # PKGMK_IGNORE_FOOTPRINT="no"
 # PKGMK_IGNORE_NEW="no"
 # PKGMK_NO_STRIP="no"
diff --git a/tools/vim.html b/tools/vim.html
index 6b9ec31..b3354d8 100644
--- a/tools/vim.html
+++ b/tools/vim.html
@@ -44,7 +44,7 @@
         <p>Default vimrc skeleton is configured to use wombat2mod,
         which is installed by adduser skeleton.</p>
 
-        <h2 id="spacetab">3. Split and tab</h2>
+        <h2 id="spacetab">3. Split and Tab</h2>
 
         <p>Horizontal split;</p>
         <pre>
@@ -72,7 +72,20 @@
         ctrl+w (k,j,l,h)
         </pre>
 
-        <h2 id="edit">4. Editing files</h2>
+        <h2 id="filebrowser">4. File browser</h2>
+
+        <p>Use built in netrw to see directories and files;</p>
+
+        <dl>
+            <dt>:Explore</dt>
+            <dd>Opens window with file browser.</dd>
+            <dt>:Sex</dt>
+            <dd>Open horizontal split with file browser.</dd>
+            <dt>:Vex</dt>
+            <dd>Open vertical split with file browser.</dd>
+        </dl>
+
+        <h2 id="edit">5. Editing files</h2>
 
         <p>Come from background;</p>
 
@@ -166,15 +179,15 @@
             comma (,), press esc to confirm.</dd>
         </dl>
 
-        <h2 id="spellcheck"> Spell check</h2>
+        <h2 id="spellcheck">6. Spell check</h2>
 
         <p>Press z= over the bad written word and select desired one.</p>
 
-        <h2 id="plugin">6. Plugins</h2>
+        <h2 id="plugin">7. Plugins</h2>
 
         <a href="index.html">Systools Index</a>
         <p>This is part of the Hive System Documentation.
-        Copyright (C) 2018
+        Copyright (C) 2019
         Hive Team.
         See the file <a href="../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
         for copying conditions.</p>
diff --git a/tools/x.html b/tools/x.html
index 79d1757..c693062 100644
--- a/tools/x.html
+++ b/tools/x.html
@@ -155,7 +155,7 @@
         fi
         </pre>
 
-        <h3>Global keyboard config </h3>
+        <h3 id="confkeyboard">Global keyboard config </h3>
 
 	<p>Xorg global default keyboard configuration; <a href="conf/etc/X11/xorg.conf.d/10-keyboard.conf">/etc/X11/xorg.conf.d/10-keyboard.conf</a></p>
 
k.com> 2015-08-14 21:13:49 -0700 2000 - stop constantly copying large arrays around' href='/akkartik/mu/commit/020run.cc?h=main&id=e74a2940522e37d63043f662f38fba3620c01780'>e74a2940 ^
0487a30e ^
dc269891 ^
fca0ebbe ^
dcfca05e ^
f1a6f323 ^
d241c9c4 ^
827898fc ^
d241c9c4 ^

f1a6f323 ^
d241c9c4 ^
dcfca05e ^
d241c9c4 ^
9cf71627 ^
cfb142b9 ^
acc4792d ^
cfb142b9 ^

56c0e796 ^

0487a30e ^
2dab0edb ^
0d302187 ^
1066660e ^
9fc64bbc ^
9d670bb5 ^
9fc64bbc ^

e74a2940 ^




3c435756 ^


2199940a ^
b24eb476 ^
8eff7919 ^
ec926027 ^

7c8493b3 ^
795f5244 ^
ec926027 ^

dcfca05e ^
795f5244 ^
dcfca05e ^

69e14325 ^
795f5244 ^
ec926027 ^

95b2a140 ^



e5e9f7db ^



0d3f30c2 ^
f28f2636 ^
785a60fa ^
2429c65c ^
8aa4b664 ^

e5e9f7db ^

b39ceb27 ^
2186422c ^
db50f43a ^
e7f76736 ^
cea49fde ^





2429c65c ^
cea49fde ^

e7f76736 ^
ae5f0b6f ^
b39ceb27 ^
2429c65c ^
6dd67576 ^

2429c65c ^
e7f76736 ^
e5e9f7db ^

b39ceb27 ^
ae5f0b6f ^
0d3f30c2 ^
b39ceb27 ^
f28f2636 ^
6c1376f8 ^
4f10b93a ^
7858a06a ^
cea49fde ^





ae5f0b6f ^
e7f76736 ^
e7f76736 ^

267ebb59 ^
e0551d78 ^
b24eb476 ^
e0551d78 ^

c062021a ^
b24eb476 ^
c062021a ^


b24eb476 ^
c062021a ^

e0551d78 ^
e5e9f7db ^

e0551d78 ^

f78f92c5 ^
f28f2636 ^

f78f92c5 ^







2429c65c ^
9f78ec2d ^
2429c65c ^
9f78ec2d ^

267ebb59 ^
f5f4b698 ^
9dcbec39 ^
f5f4b698 ^

b2ec0969 ^
8c9e97ae ^
267ebb59 ^


9f78ec2d ^





2429c65c ^
9f78ec2d ^



55fc43b7 ^
2429c65c ^
9f78ec2d ^





385ff136 ^

9f78ec2d ^
7284d503 ^

e5e9f7db ^
3473c63a ^
43b866d1 ^
fca0ebbe ^
ab6ed192 ^
a4ef18b1 ^
a26cc359 ^

43b866d1 ^
b24eb476 ^

6700f9f2 ^

b291f85b ^

cae5461b ^


56c0e796 ^
43b866d1 ^
0c0bc3ae ^
9dcbec39 ^
0c0bc3ae ^

6b6dfb0c ^
18429d40 ^
43b866d1 ^
6700f9f2 ^
70179d1f ^
9dcbec39 ^
35064671 ^
70179d1f ^
43b866d1 ^
b24eb476 ^
6700f9f2 ^


b291f85b ^
cae5461b ^

c1a50c82 ^
b24eb476 ^
3c163ef7 ^
43b866d1 ^
c6034af3 ^
3076bab4 ^
b24eb476 ^
3c163ef7 ^
c6034af3 ^
f48f6c14 ^
eaa75c87 ^
decaddb4 ^
70179d1f ^
3c163ef7 ^
c91caafd ^
6c1376f8 ^
70179d1f ^


78164bab ^
c6034af3 ^



a0fc38c9 ^

b24eb476 ^
78164bab ^





e5e9f7db ^




7ac8feef ^
390f4097 ^





e5e9f7db ^

decaddb4 ^
1ead3562 ^
decaddb4 ^
bc643692 ^
5497090a ^
decaddb4 ^
acc4792d ^

d7494165 ^
fc55fea0 ^

1ead3562 ^
bc643692 ^
fc55fea0 ^
acc4792d ^
18429d40 ^
bc643692 ^
5f98a10c ^
1ead3562 ^
a0fc38c9 ^
18429d40 ^
bc643692 ^
6d2b1168 ^
677fa104 ^



1ead3562 ^
578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^

1ead3562 ^
578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^

1ead3562 ^
578327f1 ^
677fa104 ^
578327f1 ^
677fa104 ^

1ead3562 ^
578327f1 ^
677fa104 ^
578327f1 ^
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
                                                           









                                                                              
                        
          
                     
 
                                           
                              
 
                
          
                     
                           
 
                                         
                      
                              
 
                         
          
                                   
 


                              
                     
                                       
                                                                                
                
                                
                         
                                                                        
                         
                                           

  
                       
                                


                                               
 
       
                            


                        

 
                          
                                                                       
                                                                              
   
                              
                                                                             
                                                                                                                       
                                        
                                                                                    
                        
     
                                                                                     
                                        
                                    
                                                                       
                                                                                    
     
                                                  
                                     
                                              
                                         
                  
                                                                                           

              
                                             
                
                                                                                  
       
     
                                                                
                                                                                                                                                                                       

          

                                                                              
     
                         
                        
                           
   
                                

 




                                              


                                                                              
 
                                  
                                             

 
                                            
                                                           

 
                                                 
                                                                                                    

 
                                        
                                                                       

 



                                                          



                                                               
                
                      
                                     
                                  

             

                                                          
                                   
                      
                                    
               





                                                              
                                  

           
   
                                                           
 
                

             
                 
 

                                                                         
                    
                                                                                                              
                 
          
                        
                                       
                                                       
                                          





                                       
                                                 
                

 
       
                     
                                                                                                           

                                                  
                                
                                                                                               


                                                  
                                                                                                                             

                                                  
 

                              

       
                     

                                   







                                                
                                              
                               
                       

           
                                 
             
                                                            

           
                                                     
            


              





                                                        
                           



                                                                
                                 
                                                





                        

                     
 

                                            
       
                                               
                                    
                        
                      
                              

                  
                                  

                                                 

                                                                                                 

                          


                
                                                                                                                                                                    
                                           
                
                                                                           

           
                          
                            
                                         
                           
                               
                                                                                                                                                                                                                                 
           
   
                                      
                                                       


                                                                                                                     
   

 
       
                               
                        
                                 
                         
 
                                    
                      
                            
           
 
 
                                                                  
                           
                               
                                                                                       


                                  
                                          



                                            

 
                                          





                                             




                                          
                                       





                                                                           

 
                     
          
      
                     
                           
 

                                           
          

                     
          
             
 
                              
 
                                 
                     
          
                     
 
                              
 



                                                                            
          
                                
 
                             

                               
          
                                
 
                             

                              
          
                                 
 
                             

                             
          
                                 
 
                             
//: Phase 3: Start running a loaded and transformed recipe.
//:
//: So far we've seen recipes as lists of instructions, and instructions point
//: at other recipes. To kick things off mu needs to know how to run certain
//: 'primitive' recipes. That will then give the ability to run recipes
//: containing these primitives.
//:
//: This layer defines a skeleton with just two primitive recipes: IDLE which
//: does nothing, and COPY, which can copy numbers from one memory location to
//: another. Later layers will add more primitives.

:(scenario copy_literal)
def main [
  1:number <- copy 23
]
+run: {1: "number"} <- copy {23: "literal"}
+mem: storing 23 in location 1

:(scenario copy)
def main [
  1:number <- copy 23
  2:number <- copy 1:number
]
+run: {2: "number"} <- copy {1: "number"}
+mem: location 1 is 23
+mem: storing 23 in location 2

:(scenario copy_multiple)
def main [
  1:number, 2:number <- copy 23, 24
]
+mem: storing 23 in location 1
+mem: storing 24 in location 2

:(before "End Types")
// Book-keeping while running a recipe.
//: Later layers will replace this to support running multiple routines at once.
struct routine {
  recipe_ordinal running_recipe;
  int running_step_index;
  routine(recipe_ordinal r) :running_recipe(r), running_step_index(0) {}
  bool completed() const;
  const vector<instruction>& steps() const;
};

:(before "End Globals")
routine* Current_routine = NULL;
map<string, int> Instructions_running;
map<string, int> Locations_read;
map<string, int> Locations_read_by_instruction;

:(code)
void run(recipe_ordinal r) {
  routine rr(r);
  Current_routine = &rr;
  run_current_routine();
}

void run_current_routine()
{  // curly on a separate line, because later layers will modify header
  while (!Current_routine->completed())  // later layers will modify condition
  {
    // Running One Instruction
    if (current_instruction().is_label) { ++current_step_index(); continue; }
    trace(Initial_callstack_depth + Trace_stream->callstack_depth, "run") << to_string(current_instruction()) << end();
    if (get_or_insert(Memory, 0) != 0) {
      raise << "something wrote to location 0; this should never happen\n" << end();
      put(Memory, 0, 0);
    }
    // read all ingredients from memory, each potentially spanning multiple locations
    vector<vector<double> > ingredients;
    if (should_copy_ingredients()) {
      for (int i = 0; i < SIZE(current_instruction().ingredients); ++i)
        ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
    }
    // instructions below will write to 'products'
    vector<vector<double> > products;
    switch (current_instruction().operation) {
      // Primitive Recipe Implementations
      case COPY: {
        copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
        break;
      }
      // End Primitive Recipe Implementations
      default: {
        cout << "not a primitive op: " << current_instruction().operation << '\n';
      }
    }
    if (SIZE(products) < SIZE(current_instruction().products)) {
      raise << SIZE(products) << " vs " << SIZE(current_instruction().products) << ": failed to write to all products! " << to_original_string(current_instruction()) << '\n' << end();
    }
    else {
      for (int i = 0; i < SIZE(current_instruction().products); ++i)
        write_memory(current_instruction().products.at(i), products.at(i), i);
    }
    // End of Instruction
    finish_instruction:;
    ++current_step_index();
  }
  stop_running_current_routine:;
}

bool should_copy_ingredients() {
  // End should_copy_ingredients Special-cases
  return true;
}

//: Some helpers.
//: We'll need to override these later as we change the definition of routine.
//: Important that they return referrences into the routine.

inline int& current_step_index() {
  return Current_routine->running_step_index;
}

inline const string& current_recipe_name() {
  return get(Recipe, Current_routine->running_recipe).name;
}

inline const instruction& current_instruction() {
  return get(Recipe, Current_routine->running_recipe).steps.at(Current_routine->running_step_index);
}

inline bool routine::completed() const {
  return running_step_index >= SIZE(get(Recipe, running_recipe).steps);
}

inline const vector<instruction>& routine::steps() const {
  return get(Recipe, running_recipe).steps;
}

//:: Startup flow

//: Step 1: load all .mu files with numeric prefixes (in order)
:(before "End Load Recipes")
// Load .mu Core
//? Save_trace = true;
//? START_TRACING_UNTIL_END_OF_SCOPE;
load_file_or_directory("core.mu");
//? DUMP("");
//? exit(0);

//: Step 2: load any .mu files provided at the commandline
:(before "End Commandline Parsing")
// Check For .mu Files
//? START_TRACING_UNTIL_END_OF_SCOPE
if (argc > 1) {
  // skip argv[0]
  argv++;
  argc--;
  // ignore argv past '--'; that's commandline args for 'main'
  while (argc > 0) {
    if (string(*argv) == "--") break;
    load_file_or_directory(*argv);
    argv++;
    argc--;
  }
  if (Run_tests) Recipe.erase(get(Recipe_ordinal, "main"));
}
transform_all();
//? DUMP("");
//? exit(0);
save_snapshots();

//: Step 3: if we aren't running tests, locate a recipe called 'main' and
//: start running it.
:(before "End Main")
if (!Run_tests && contains_key(Recipe_ordinal, "main") && contains_key(Recipe, get(Recipe_ordinal, "main"))) {
  // Running Main
  setup();
//?   Save_trace = true;
//?   START_TRACING_UNTIL_END_OF_SCOPE;
  trace(9990, "run") << "=== Starting to run" << end();
  assert(Num_calls_to_transform_all == 1);
  run_main(argc, argv);
  teardown();
}

:(code)
void run_main(int argc, char* argv[]) {
  recipe_ordinal r = get(Recipe_ordinal, "main");
  if (r) run(r);
}

:(code)
void dump_profile() {
  for (map<string, int>::iterator p = Instructions_running.begin(); p != Instructions_running.end(); ++p) {
    cerr << p->first << ": " << p->second << '\n';
  }
  cerr << "== locations read\n";
  for (map<string, int>::iterator p = Locations_read.begin(); p != Locations_read.end(); ++p) {
    cerr << p->first << ": " << p->second << '\n';
  }
  cerr << "== locations read by instruction\n";
  for (map<string, int>::iterator p = Locations_read_by_instruction.begin(); p != Locations_read_by_instruction.end(); ++p) {
    cerr << p->first << ": " << p->second << '\n';
  }
}
:(before "End One-time Setup")
//? atexit(dump_profile);

:(code)
void cleanup_main() {
  if (Save_trace && Trace_stream) {
    ofstream fout("interactive");
    fout << Trace_stream->readable_contents("");
    fout.close();
  }
}
:(before "End One-time Setup")
atexit(cleanup_main);

:(code)
void load_file_or_directory(string filename) {
  if (is_directory(filename)) {
    load_all(filename);
    return;
  }
  ifstream fin(filename.c_str());
  if (!fin) {
    raise << "no such file '" << filename << "'\n" << end();
    return;
  }
  trace(9990, "load") << "=== " << filename << end();
  load(fin);
  fin.close();
}

bool is_directory(string path) {
  struct stat info;
  if (stat(path.c_str(), &info)) return false;  // error
  return info.st_mode & S_IFDIR;
}

void load_all(string dir) {
  dirent** files;
  int num_files = scandir(dir.c_str(), &files, NULL, alphasort);
  for (int i = 0; i < num_files; ++i) {
    string curr_file = files[i]->d_name;
    if (isdigit(curr_file.at(0)))
      load_file_or_directory(dir+'/'+curr_file);
    free(files[i]);
    files[i] = NULL;
  }
  free(files);
}
:(before "End Includes")
#include <dirent.h>
#include <sys/stat.h>

//:: Reading from memory, writing to memory.

:(code)
vector<double> read_memory(reagent/*copy*/ x) {
  // Begin Preprocess read_memory(x)
  vector<double> result;
  if (is_literal(x)) {
    result.push_back(x.value);
    return result;
  }
  // End Preprocess read_memory(x)
  int size = size_of(x);
  for (int offset = 0; offset < size; ++offset) {
    double val = get_or_insert(Memory, x.value+offset);
    trace(9999, "mem") << "location " << x.value+offset << " is " << no_scientific(val) << end();
    result.push_back(val);
  }
  return result;
}

void write_memory(reagent/*copy*/ x, const vector<double>& data, const int /*only when called in the run loop above to save results; -1 otherwise*/ product_index) {
  // Begin Preprocess write_memory(x, data)
  if (!x.type) {
    raise << "can't write to '" << to_string(x) << "'; no type\n" << end();
    return;
  }
  if (is_dummy(x)) return;
  if (is_literal(x)) return;
  // End Preprocess write_memory(x, data)
  if (x.value == 0) return;
  if (size_mismatch(x, data)) {
    raise << maybe(current_recipe_name()) << "size mismatch in storing to '" << x.original_string << "' (" << size_of(x.type) << " vs " << SIZE(data) << ") at '" << to_original_string(current_instruction()) << "'\n" << end();
    return;
  }
  // End write_memory(x) Special-cases
  for (int offset = 0; offset < SIZE(data); ++offset) {
    assert(x.value+offset > 0);
    trace(9999, "mem") << "storing " << no_scientific(data.at(offset)) << " in location " << x.value+offset << end();
    put(Memory, x.value+offset, data.at(offset));
  }
}

:(code)
int size_of(const reagent& r) {
  if (!r.type) return 0;
  // End size_of(reagent r) Cases
  return size_of(r.type);
}
int size_of(const type_tree* type) {
  if (!type) return 0;
  // End size_of(type) Cases
  return 1;
}

bool size_mismatch(const reagent& x, const vector<double>& data) {
  if (!x.type) return true;
  // End size_mismatch(x) Cases
//?   if (size_of(x) != SIZE(data)) cerr << size_of(x) << " vs " << SIZE(data) << '\n';
  return size_of(x) != SIZE(data);
}

inline bool is_literal(const reagent& r) {
  if (!r.type) return false;
  if (r.type->value == 0)
    assert(!r.type->left && !r.type->right);
  return r.type->value == 0;
}

inline bool scalar(const vector<int>& x) {
  return SIZE(x) == 1;
}
inline bool scalar(const vector<double>& x) {
  return SIZE(x) == 1;
}

// helper for tests
void run(string form) {
  vector<recipe_ordinal> tmp = load(form);
  transform_all();
  if (tmp.empty()) return;
  if (trace_count("error") > 0) return;
  // if a test defines main, it probably wants to start there regardless of
  // definition order
  if (contains_key(Recipe, get(Recipe_ordinal, "main")))
    run(get(Recipe_ordinal, "main"));
  else
    run(tmp.front());
}

:(scenario run_label)
def main [
  +foo
  1:number <- copy 23
  2:number <- copy 1:number
]
+run: {1: "number"} <- copy {23: "literal"}
+run: {2: "number"} <- copy {1: "number"}
-run: +foo

:(scenario run_dummy)
def main [
  _ <- copy 0
]
+run: _ <- copy {0: "literal"}

:(scenario write_to_0_disallowed)
% Hide_errors = true;
def main [
  0:number <- copy 34
]
-mem: storing 34 in location 0

//: mu is robust to various combinations of commas and spaces. You just have
//: to put spaces around the '<-'.

:(scenario comma_without_space)
def main [
  1:number, 2:number <- copy 2,2
]
+mem: storing 2 in location 1

:(scenario space_without_comma)
def main [
  1:number, 2:number <- copy 2 2
]
+mem: storing 2 in location 1

:(scenario comma_before_space)
def main [
  1:number, 2:number <- copy 2, 2
]
+mem: storing 2 in location 1

:(scenario comma_after_space)
def main [
  1:number, 2:number <- copy 2 ,2
]
+mem: storing 2 in location 1