0xGA: Check-in [bd2cc40bce]

Yet another PHP framework, but made for org-mode and geeks.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Remove orgx script as I switch to a full emacs integration.\nRename 0xga install script to narv, in order to let it become a more generic tool for the frawework.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:bd2cc40bce90dca964ed53f84281061c1dcf17ba
User & Date: milouse 2014-02-24 21:20:14
Context
2014-02-24
23:20
Modification to the test infrastructure check-in: 2c413d3bc1 user: milouse tags: trunk
21:20
Remove orgx script as I switch to a full emacs integration.\nRename 0xga install script to narv, in order to let it become a more generic tool for the frawework. check-in: bd2cc40bce user: milouse tags: trunk
2014-02-11
23:32
Add some nice french touch to pdf export of orgx script check-in: caf946d20a user: milouse tags: trunk
Changes

Deleted etc/batch.el.

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
;; Langue par défaut et caractères accentués
(setq current-language-environment "UTF-8")

;;; Org mode
(add-to-list 'load-path "/usr/share/emacs/site-lisp/org_contrib/lisp")

(require 'org)
(require 'ox-odt)
(require 'ox-latex)

(setq org-todo-keywords
      '((sequence "TODO" "PROGRESS" "FEEDBACKS" "CANCELED" "DONE")
	(sequence "BUG" "KNOWNCAUSE" "FIXED")))
(setq org-todo-keyword-faces
      '(("PROGRESS" . "yellow")
	("CANCELED" . "light sky blue")))

(add-to-list 'org-latex-classes
	     '("rapport-no-part"
	       "\\documentclass{book}
               [NO-DEFAULT-PACKAGES]
               [NO-PACKAGES]"
	       ("\\chapter{%s}" . "\\chapter*{%s}")
	       ("\\section{%s}" . "\\section*{%s}")
	       ("\\subsection{%s}" . "\\subsection*{%s}")
	       ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
	       ("\\paragraph{%s}" . "\\paragraph*{%s}")
	       ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
























































Name change from 0xga to usr/bin/narv.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
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
#!/usr/bin/env bash

# To be called Narv and let you do more administrative stuff
VERSION=0.2

function usage_help {
    echo -e "\t\t\t\033[1m0xGA tools suite v$VERSION\033[0m"
    echo ""
    echo -e "\033[1mSYNOPSIS\033[0m
    0xGA [COMMAND [OPTIONS]]
    0xGA [FILE]

\033[1mCOMMANDS\033[0m
    \033[1m-h
    --help\033[0m
        The -h option displays this help page and exits.
    \033[1m-v
    --version\033[0m
................................................................................
\033[1mEXIT STATUS\033[0m
    The script will exit with an error if no argument is given.

\033[1mAUTHORS\033[0m
    Étienne Deparis <etienne@depar.is>"

}











DOCAUTHOR=$USER
COMMAND="install"
MUSTPULL="yes"
FOSSILFILE=$HOME/.0xGA.fossil
DESTDIR=`realpath public_html`
DOMAINNAME="localhost"

if [ $# -gt 0 ]; then
    while [ -n "$1" ]; do
	case $1 in
	    --fossil|-f)
		shift
		FOSSILFILE=`realpath $1`
		;;
	    --dest|-d)
		shift
		DESTDIR=`realpath $1`
		;;
	    --nopull|-n)
		MUSTPULL="no"
		;;
	    --host|-H)
		shift
		DOMAINNAME=$1
		;;
	    install|update|streamindex|-h|-v|--help|--version)
		COMMAND=$1
		;;
	    *)
		COMMAND=install
		;;
	esac
	shift
    done
fi

if [ "$DESTDIR" = "/" ]; then
    echo ""
    echo "DO NOT RUN THIS SCRIPT AT THE ROOT OF YOUR SYSTEM, YOU FOOL"
    echo ""
................................................................................
    echo "Please choose a more appropriate destination"
    echo "like /srv/http, /home/whatever/public_html or /var/www"
    exit 1
fi

case $COMMAND in
    -v|--version)
	echo "0xGA tools suite v$VERSION"
	echo "Written by Étienne Deparis"
        ;;
    -h|--help)
	usage_help | fmt | more
	;;
    install)
	if [ `dirname $FOSSILFILE` = "$DESTDIR" ]; then
	    echo "For security reasons, install directory and fossil directory MUST be different."
	    exit 1
	fi

	echo "0xGA Installation"
	echo ":: will save fossil repository file in $FOSSILFILE"
	echo ":: will install 0xGA in $DESTDIR"

	if [ -e $DESTDIR -o -e $FOSSILFILE ]; then
	    echo ""
	    echo "ERROR: $DESTDIR or $FOSSILFILE already exists on your system."
	    echo ""
	    echo "This is an installation script. Maybe you just want to update your 0xGA
installation?"
	    echo ""
	    echo "If you really wants to do a fresh install at this place, remove it first.
This program will now exit to let you do all the necessary things before."
	    exit 1
	fi

	echo ":: fetching sources"
	fossil clone http://projects.depar.is/0xGA $FOSSILFILE

	mkdir $DESTDIR
	cd $DESTDIR
	fossil open $FOSSILFILE
	fossil close

	echo ":: cleaning up installation files"
	rm -rf .fossil-settings
	rm -rf test
	rm etc/systemd/system/multi-user.target.wants/sample.service
	rm 0xga

	echo ":: creating necessary directories structure"
	if [ -e var ]; then
	    rm -rf var
	fi
	mkdir -p var/db/
	mkdir -p var/cache/
	mkdir -p var/log/
	mkdir -p var/tmp/
	chmod -R g+w var

	if [ -n "$DOMAINNAME" ]; then
	    NEWDOMAIN=$DOMAINNAME
	else
	    NEWDOMAIN="http://mydomain.eu/"
	fi
	echo ""
	echo "What will be the complete URL to access your Website?"
	echo "The leading / is required! Example: $NEWDOMAIN"
	echo "Just hitting return key will skip the home creation process."
	echo -n "URL: "
	read NEWDOMAIN

	SITEFOLDER=`echo $NEWDOMAIN | sed "s|https\?://||" | cut -d/ -f1`
	SITEFOLDER=home/$SITEFOLDER

	if [ -e home ]; then
	    if [ ! -d home]; then
		rm -rf home
		mkdir home
	    fi
	else
	    mkdir home
	fi

	if [ -n "$NEWDOMAIN" ]; then
	    echo ":: creating the home folder for your new Website in $SITEFOLDER"

	    mkdir -p $SITEFOLDER
	    mkdir -p $SITEFOLDER/Documents
	    chmod -R g+w $SITEFOLDER/Documents
	fi

	echo ":: configuring new Website"
	APACHESERVER="yes"
	echo ""
	echo -n "Do you use Apache Webserver ? (YES, no): "
	read APACHESERVER
	if [[ $APACHESERVER == "YES" || $APACHESERVER == "yes" || $APACHESERVER == "" ]]; then
	    mv sample.htaccess .htaccess
	    $EDITOR .htaccess
	else
	    rm sample.htaccess
	fi

	echo ":: setting up general configuration"

	echo "Who will be the main user of your new Website?"
	echo -n "login ($DOCAUTHOR): "
	read ROOTLOGIN
	echo -n "password: "
	read ROOTPASS

	sed -i "s|/any/where/you/want/|$DESTDIR/|" etc/rc.sample.conf
	sed -i "s|http://mydomain.eu/|$NEWDOMAIN|" etc/rc.sample.conf

	if [ -n "$ROOTLOGIN" ]; then
	    sed -i "s/= root$/= $ROOTLOGIN/" etc/rc.sample.conf
	else
	    sed -i "s/= root$/= $DOCAUTHOR/" etc/rc.sample.conf
	fi
	sed -i "s/= N€w7a5s$/= $ROOTPASS/" etc/rc.sample.conf
	mv etc/rc.sample.conf etc/rc.conf
	$EDITOR etc/rc.conf

	echo ":: setting up template configuration"
	sed -i "s/Your Name/$DOCAUTHOR/" etc/template.sample.conf
	mv etc/template.sample.conf etc/template.conf
	$EDITOR etc/template.conf

	mv etc/routes.sample.conf etc/routes.conf

	echo ":: setting up your new Website home page"
	echo "#+title: Home
#+date: <"`date +%Y-%m-%d`" "`date +%a`">" > $SITEFOLDER/home.org
	$EDITOR $SITEFOLDER/home.org

	echo "Installation finished."
	if [ -n "$BROWSER" ]; then
	    $BROWSER $NEWDOMAIN
	fi
	;;
    update)
	echo ":: Will use fossil repository file in $FOSSILFILE"
	echo ":: will update the installation in $DESTDIR"

	cd $DESTDIR

	echo ":: fetching sources"
	echo ":: pull is required: $MUSTPULL"
	fossil open $FOSSILFILE
	if [ "$MUSTPULL" = "yes" ]; then
	    fossil pull http://projects.depar.is/0xGA
	fi
	fossil update
	fossil close

	echo ":: cleaning up installation files"
	rm -rf .fossil-settings
	rm -rf test
	rm etc/systemd/system/multi-user.target.wants/sample.service
	rm etc/*.sample.conf
	rm sample.htaccess
	rm 0xga

	echo "Update process finished."
	;;
    streamindex)
	touch $DESTDIR/var/cache/streamindextoken
	wget -q -O /dev/null ${DOMAINNAME}stream/admin/index
	rm $DESTDIR/var/cache/streamindextoken
	;;
    *)
	echo "There's something strange
in your neighborhood.
Who ya gonna call?"
	;;
esac


<



|


|
|







 







>
>
>
>
>
>
>
>
>
>










|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







|
|


|
|

|
|
|
|

|
|
|

|
|
|
|
|

|
|

|
|

|
|

|
|
|
|

|
<
<
<
<

|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|

|
|

|
|
|
|

|
|
|
|
|
|
|
|
|
|
|

|

|
|
|
|
|

|
|

|
|
|
|
|
|
|
|

|
|
|
|

|

|
|

|

|
|
|
|
|

|
|

|

|
|
|
|
|
|
|
|

|
<
<
<
<
<
<
>
|
|

|
|
|
|

|


|

1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
..
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
#!/usr/bin/env bash


VERSION=0.2

function usage_help {
    echo -e "\t\t\t\033[1mNarv - 0xGA tools suite v$VERSION\033[0m"
    echo ""
    echo -e "\033[1mSYNOPSIS\033[0m
    narv [COMMAND [OPTIONS]]
    narv [FILE]

\033[1mCOMMANDS\033[0m
    \033[1m-h
    --help\033[0m
        The -h option displays this help page and exits.
    \033[1m-v
    --version\033[0m
................................................................................
\033[1mEXIT STATUS\033[0m
    The script will exit with an error if no argument is given.

\033[1mAUTHORS\033[0m
    Étienne Deparis <etienne@depar.is>"

}

function cleanup_installation {
    echo ":: cleaning up installation files"
    rm -rf .fossil-settings
    rm -rf test
    rm etc/systemd/system/multi-user.target.wants/sample.service
    rm etc/*.sample.conf
    rm sample.htaccess
    rm usr/bin/narv    
}

DOCAUTHOR=$USER
COMMAND="install"
MUSTPULL="yes"
FOSSILFILE=$HOME/.0xGA.fossil
DESTDIR=`realpath public_html`
DOMAINNAME="localhost"

if [ $# -gt 0 ]; then
    while [ -n "$1" ]; do
        case $1 in
            --fossil|-f)
                shift
                FOSSILFILE=`realpath $1`
                ;;
            --dest|-d)
                shift
                DESTDIR=`realpath $1`
                ;;
            --nopull|-n)
                MUSTPULL="no"
                ;;
            --host|-H)
                shift
                DOMAINNAME=$1
                ;;
            install|update|streamindex|-h|-v|--help|--version)
                COMMAND=$1
                ;;
            *)
                COMMAND=install
                ;;
        esac
        shift
    done
fi

if [ "$DESTDIR" = "/" ]; then
    echo ""
    echo "DO NOT RUN THIS SCRIPT AT THE ROOT OF YOUR SYSTEM, YOU FOOL"
    echo ""
................................................................................
    echo "Please choose a more appropriate destination"
    echo "like /srv/http, /home/whatever/public_html or /var/www"
    exit 1
fi

case $COMMAND in
    -v|--version)
        echo "Narv - 0xGA tools suite v$VERSION"
        echo "Written by Étienne Deparis"
        ;;
    -h|--help)
        usage_help | fmt | more
        ;;
    install)
        if [ `dirname $FOSSILFILE` = "$DESTDIR" ]; then
            echo "For security reasons, install directory and fossil directory MUST be different."
            exit 1
        fi

        echo "0xGA Installation"
        echo ":: will save fossil repository file in $FOSSILFILE"
        echo ":: will install 0xGA in $DESTDIR"

        if [ -e $DESTDIR -o -e $FOSSILFILE ]; then
            echo ""
            echo "ERROR: $DESTDIR or $FOSSILFILE already exists on your system."
            echo ""
            echo "This is an installation script. Maybe you just want to update your 0xGA
installation?"
            echo ""
            echo "If you really wants to do a fresh install at this place, remove it first.
This program will now exit to let you do all the necessary things before."
            exit 1
        fi

        echo ":: fetching sources"
        fossil clone http://projects.depar.is/0xGA $FOSSILFILE

        mkdir $DESTDIR
        cd $DESTDIR
        fossil open $FOSSILFILE
        fossil close

        cleanup_installation





        echo ":: creating necessary directories structure"
        if [ -e var ]; then
            rm -rf var
        fi
        mkdir -p var/db/
        mkdir -p var/cache/
        mkdir -p var/log/
        mkdir -p var/tmp/
        chmod -R g+w var

        if [ -n "$DOMAINNAME" ]; then
            NEWDOMAIN=$DOMAINNAME
        else
            NEWDOMAIN="http://mydomain.eu/"
        fi
        echo ""
        echo "What will be the complete URL to access your Website?"
        echo "The leading / is required! Example: $NEWDOMAIN"
        echo "Just hitting return key will skip the home creation process."
        echo -n "URL: "
        read NEWDOMAIN

        SITEFOLDER=`echo $NEWDOMAIN | sed "s|https\?://||" | cut -d/ -f1`
        SITEFOLDER=home/$SITEFOLDER

        if [ -e home ]; then
            if [ ! -d home]; then
                rm -rf home
                mkdir home
            fi
        else
            mkdir home
        fi

        if [ -n "$NEWDOMAIN" ]; then
            echo ":: creating the home folder for your new Website in $SITEFOLDER"

            mkdir -p $SITEFOLDER
            mkdir -p $SITEFOLDER/Documents
            chmod -R g+w $SITEFOLDER/Documents
        fi

        echo ":: configuring new Website"
        APACHESERVER="yes"
        echo ""
        echo -n "Do you use Apache Webserver ? (YES, no): "
        read APACHESERVER
        if [[ $APACHESERVER == "YES" || $APACHESERVER == "yes" || $APACHESERVER == "" ]]; then
            mv sample.htaccess .htaccess
            $EDITOR .htaccess
        else
            rm sample.htaccess
        fi

        echo ":: setting up general configuration"

        echo "Who will be the main user of your new Website?"
        echo -n "login ($DOCAUTHOR): "
        read ROOTLOGIN
        echo -n "password: "
        read ROOTPASS

        sed -i "s|/any/where/you/want/|$DESTDIR/|" etc/rc.sample.conf
        sed -i "s|http://mydomain.eu/|$NEWDOMAIN|" etc/rc.sample.conf

        if [ -n "$ROOTLOGIN" ]; then
            sed -i "s/= root$/= $ROOTLOGIN/" etc/rc.sample.conf
        else
            sed -i "s/= root$/= $DOCAUTHOR/" etc/rc.sample.conf
        fi
        sed -i "s/= N€w7a5s$/= $ROOTPASS/" etc/rc.sample.conf
        mv etc/rc.sample.conf etc/rc.conf
        $EDITOR etc/rc.conf

        echo ":: setting up template configuration"
        sed -i "s/Your Name/$DOCAUTHOR/" etc/template.sample.conf
        mv etc/template.sample.conf etc/template.conf
        $EDITOR etc/template.conf

        mv etc/routes.sample.conf etc/routes.conf

        echo ":: setting up your new Website home page"
        echo "#+title: Home
#+date: <"`date +%Y-%m-%d`" "`date +%a`">" > $SITEFOLDER/home.org
        $EDITOR $SITEFOLDER/home.org

        echo "Installation finished."
        if [ -n "$BROWSER" ]; then
            $BROWSER $NEWDOMAIN
        fi
        ;;
    update)
        echo ":: Will use fossil repository file in $FOSSILFILE"
        echo ":: will update the installation in $DESTDIR"

        cd $DESTDIR

        echo ":: fetching sources"
        echo ":: pull is required: $MUSTPULL"
        fossil open $FOSSILFILE
        if [ "$MUSTPULL" = "yes" ]; then
            fossil pull http://projects.depar.is/0xGA
        fi
        fossil update
        fossil close

        cleanup_installation







        echo "Update process finished."
        ;;
    streamindex)
        touch $DESTDIR/var/cache/streamindextoken
        wget -q -O /dev/null ${DOMAINNAME}stream/admin/index
        rm $DESTDIR/var/cache/streamindextoken
        ;;
    *)
        echo "There's something strange
in your neighborhood.
Who ya gonna call?"
        ;;
esac

Deleted usr/bin/orgx.

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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env bash

VERSION=0.1

function usage_help {
    echo "orgx editor v$VERSION"
    echo ""
    echo "SYNOPSIS
    $0 [-h|-v]
    $0 create [-d PATH] [-t] [-f] [TITLE]
    $0 edit [-t] FILE
    $0 export [-t] [-d PATH] [-o FORMAT] [-l EMACSPREFS] [-x] FILE

COMMANDS
    create
        create a new orgx file.
    edit
        edit an existing orgx file. If FILE is not given, $0 will
        prompt you, which file to edit.
    export
        export an existing orgx file to another pdf, odt or latex
        file. If FILE is not given, $0 will prompt you, which file to
        export. If no output format is specified, the default is pdf.

OPTIONS
    -h | --help
        display this help page and exits.
    -v | --version
        display the version of this script and exits.
    -t | --text
        deactivate X support. $0 will run in text mode only.
    -d | --dest
        output new or exported document to this PATH. PATH must be a
        valid directory path.
    -f | --flat
        do not compress the created document as an orgx file.
    -o | --output
        specify the desired output format for the export COMMAND.
    -l | --load
        specify the emacs preferences file to use for the export
        COMMAND.
    -x
        open exported file after conversion. Require xdg-open.

AUTHORS
    Étienne Deparis <etienne@depar.is>"
}


function extract_orgx {
    if [ ! -f $ORGXFILE ]; then
        if [ -d $ORGXFILE ]; then
            FLAT="yes"
        else
            ERRORMSG='The given path is not a file nor a directory. Aborting.'
            if [ "$GRAPHIC" = 'no' ]; then
                echo ""
                echo "ERROR: $ERRORMSG"
            else
                zenity --error --title="orgx editor v$VERSION" \
                    --text="$ERRORMSG"
            fi
            exit 1
        fi
    else
        # ensure FLAT is defined
        FLAT='no'
    fi

    DOCDATE=`date +%Y%m%d%H%M%S`

    if [ "$FLAT" = "no" ]; then
        if [ ! -d "/tmp/0xGA-tmp-$DOCAUTHOR" ]; then
            mkdir -p /tmp/0xGA-tmp-$DOCAUTHOR
        fi

        TMPNAME="/tmp/0xGA-tmp-$DOCAUTHOR/0xGA-$DOCDATE"
        unzip -q $ORGXFILE -d $TMPNAME

        EXPORTPATH=`dirname $ORGXFILE`
    else
        TMPNAME=$ORGXFILE
        EXPORTPATH=`realpath $ORGXFILE/../`
    fi

    if [ ! -f "$TMPNAME/meta.conf" ]; then
        ERRORMSG="This document is not a valid 0xGA document. Aborting."

        if [ "$GRAPHIC" = 'no' ]; then
            echo ""
            echo "ERROR: $ERRORMSG"
        else
            zenity --error --title="orgx editor v$VERSION" \
                --text="$ERRORMSG"
        fi
        exit 1
    fi
}

function prepare_tmp_dir {
    DOCDATE=`date +%Y%m%d%H%M%S`
    if [ ! -d "/tmp/0xGA-tmp-$DOCAUTHOR" ]; then
        mkdir -p /tmp/0xGA-tmp-$DOCAUTHOR
    fi
    TMPNAME="/tmp/0xGA-tmp-$DOCAUTHOR/0xGA-$DOCDATE"
    mkdir $TMPNAME
    mkdir $TMPNAME/history
    mkdir $TMPNAME/media
    mkdir $TMPNAME/templates
}

function is_it_flat_file {
    if zenity --question --title="orgx editor v$VERSION" \
        --text="Is it a flat document?"; then
        FLAT='yes'
    else
        FLAT='no'
    fi
}

function post_create_orgx_file {

    DOCTITLE=`sed -n "s/#+title: \?\(.*\)/\1/Ip" $TMPNAME/content.org`
    DOCDATE=`sed -n "/#+date:/I{s/[^0-9]//g;p}" $TMPNAME/content.org`

    if [ `echo $DOCDATE | wc -c` = 9 ]; then
        DOCDATE=$DOCDATE`date +%H%M%S`
    fi

    if [ ! -n "$DOCTITLE" ]; then
        QUESTIONMSG="Document has no title. Save anyway?"
        if [ "$GRAPHIC" = 'yes' ]; then
            if zenity --question --title="orgx editor v$VERSION" \
                --text="$QUESTIONMSG"; then
                FORCESAVE='y'
            fi
        else
            echo -n "$QUESTIONMSG (y|N): "
            read FORCESAVE
        fi

        if [[ "$FORCESAVE" == 'y' ]]; then
            DOCTITLE="document$DOCDATE"
        fi
    fi

    if [ -n "$DOCTITLE" ]; then

        echo "; Description of this document
title=\"$DOCTITLE\"
author=$DOCAUTHOR
timestamp=$DOCDATE" > $TMPNAME/meta.conf
        cp $TMPNAME/content.org $TMPNAME/history/$DOCDATE
        ZIPNAME=`echo $DOCTITLE | sed "s/ /_/g" | sed "s/'//g"`

        if [ ! -n "$DESTDIR" ]; then
            if [ "$GRAPHIC" = 'yes' ]; then
                zenity --info --text="In the next window, please select the directory in which you want to create a new document." \
                    --title="orgx editor v$VERSION"
                DESTDIR=$(zenity --file-selection --directory --title="orgx editor v$VERSION" || pwd)
            else
                DESTDIR=`pwd`
            fi
        fi

        ZIPNAME="$DESTDIR/$ZIPNAME"

        if [ ! -n "$FLAT" -a "$GRAPHIC" = 'yes' ]; then
            is_it_flat_file
        fi

        if [ "$FLAT" = "no" ]; then
            ZIPNAME=$ZIPNAME.orgx
            cd $TMPNAME
            zip -qr $ZIPNAME *
            cd
        else
            mv $TMPNAME $ZIPNAME
        fi
        echo ":: Document saved under $ZIPNAME"
    else
        echo ":: Document without title has not been saved"
    fi
    rm -rf $TMPNAME
}

function export_orgx {
    cd $TMPNAME

    if [ -f $EMACSLOADFILE ]; then
        FILENAME=(`echo $ORGXFILE | sed "s|/| |g"`)
        FILENAME=${FILENAME[${#FILENAME[*]} - 1]}
        FILENAME=`echo $FILENAME | sed "s/.orgx//"`

        case $OUTPUTFORMAT in
            odt)
                emacs --batch --load=$EMACSLOADFILE \
                    --visit=content.org \
                    --funcall org-odt-export-to-odt &> /dev/null
                mv content.odt $EXPORTPATH/$FILENAME.odt
                ;;
            tex)
                emacs --batch --load=$EMACSLOADFILE \
                    --visit=content.org \
                    --funcall org-latex-export-to-latex &> /dev/null

                mv content.tex $EXPORTPATH/$FILENAME.tex
                ;;
            *)
                emacs --batch --load=$EMACSLOADFILE \
                    --visit=content.org \
                    --funcall org-latex-export-to-latex &> /dev/null

                sed -i "s/« /<<\\\,/g" content.tex
                sed -i "s/ »/\\\,>>/g" content.tex
                sed -i "s/ / /g" content.tex

                BIBLIO=`sed -n "s/bibliography{\(.*\)}/\1/Ip" content.tex`
                pdflatex content.tex
                if [ -n "$BIBLIO" ]; then
                    bibtex content
                fi
                pdflatex content.tex
                pdflatex content.tex
                rm *.aux *.log *.out *.toc *.tex
                mv content.pdf $EXPORTPATH/$FILENAME.pdf
                ;;
        esac
    fi
}

DOCAUTHOR=$USER
COMMAND="create"
FLAT=
DESTDIR=
OUTPUTFORMAT=pdf
ORGXFILE=
ORGXFILETITLE=
#EMACSLOADFILE=`echo $0 | sed "s|usr/bin/orgx|etc/batch.el|"`
#EMACSLOADFILE=`realpath $EMACSLOADFILE`
EMACSLOADFILE="/tmp/0xGA-tmp-$USER/batch.el"
cat > $EMACSLOADFILE <<END-EMACS-BOOTSTRAP-CONF
;; Langue par défaut et caractères accentués
(setq current-language-environment "UTF-8")

;;; Org mode
(add-to-list 'load-path "/usr/share/emacs/site-lisp/org_contrib/lisp")

(require 'org)
(require 'ox-odt)
(require 'ox-latex)

(setq org-todo-keywords
    '((sequence "TODO" "PROGRESS" "FEEDBACKS" "CANCELED" "DONE")
      (sequence "BUG" "KNOWNCAUSE" "FIXED")))
(setq org-todo-keyword-faces
    '(("PROGRESS" . "yellow")
      ("CANCELED" . "light sky blue")))

(add-to-list 'org-latex-classes
    '("rapport-no-part"
      "\\documentclass{book}
      [NO-DEFAULT-PACKAGES]
      [NO-PACKAGES]"
      ("\\chapter{%s}" . "\\chapter*{%s}")
      ("\\section{%s}" . "\\section*{%s}")
      ("\\subsection{%s}" . "\\subsection*{%s}")
      ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
      ("\\paragraph{%s}" . "\\paragraph*{%s}")
      ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
END-EMACS-BOOTSTRAP-CONF
GRAPHIC='no'
if which zenity>/dev/null; then
    GRAPHIC='yes'
fi

WILLEXIT=0
echo 'One ore more dependancy is missing on your system:' > /tmp/orgx-diagnosis
if ! which zip>/dev/null; then
    WILLEXIT=1
    echo ' - zip not found' >> /tmp/orgx-diagnosis
fi
if ! which unzip>/dev/null; then
    WILLEXIT=1
    echo ' - unzip not found' >> /tmp/orgx-diagnosis
fi
if ! which emacs>/dev/null; then
    WILLEXIT=1
    echo ' - emacs not found' >> /tmp/orgx-diagnosis
fi
if ! which pdflatex>/dev/null; then
    WILLEXIT=1
    echo ' - pdflatex not found' >> /tmp/orgx-diagnosis
fi
if ! which xdg-open>/dev/null; then
    echo ' - xdg-open (optional)' >> /tmp/orgx-diagnosis
    echo 'Warning: xdg-open not found, opening of generated file after export will not work.'
fi

if [[ "$WILLEXIT" == 1 ]]; then
    if [ "$GRAPHIC" = 'yes' ]; then
        zenity --error \
            --title="orgx editor v$VERSION" \
            --text="`cat /tmp/orgx-diagnosis`" || cat /tmp/orgx-diagnosis
    else
        cat /tmp/orgx-diagnosis
    fi
    exit 1
fi

rm /tmp/orgx-diagnosis


if [ $# -gt 0 ]; then
    while [ -n "$1" ]; do
        case $1 in
            --dest|-d)
                shift
                DESTDIR=`realpath $1`
                ;;
            --output|-o)
                shift
                OUTPUTFORMAT=$1
                ;;
            --load|-l)
                shift
                EMACSLOADFILE=`realpath $1`
                ;;
            --flat|-f)
                FLAT="yes"
                ;;
            create|edit|convert|export|-h|-v|--help|--version)
                COMMAND=$1
                ;;
            --text|-t)
                GRAPHIC='no'
                ;;
            -x)
                SHALLOPENRESULT='yes'
                ;;
            *)
                ISORGXFILE=`realpath $1`

                if [ -f "$ISORGXFILE" -o -d "$ISORGXFILE" ]; then
                    ORGXFILE=$ISORGXFILE
                else
                    COMMAND=create
                    ORGXFILETITLE=$1
                fi
                ;;
        esac
        shift
    done
else
    if [ "$GRAPHIC" = 'yes' ]; then
        COMMAND=`zenity --list --title="orgx editor v$VERSION" \
        --text="What do you want to do?" \
        --height=240 --column="Action" \
        create edit convert export about help`

        [ ! -n "$COMMAND" ] && exit 1

        if [ "$COMMAND" = 'help' ]; then
            COMMAND='-h'
        elif [ "$COMMAND" = 'about' ]; then
            COMMAND='-v'
        fi
    else
        echo 'ERROR: You must provide at least one parameter'
        usage_help | fmt | more
        exit 1
    fi
fi

if [ "$COMMAND" = 'edit' -o "$COMMAND" = 'export' -o "$COMMAND" = 'convert' ]; then

    if [ ! -n "$ORGXFILE" ]; then
        FORMATSELECTMSG="In which format must $ORGXFILE be exported?"

        if [ "$GRAPHIC" = 'yes' ]; then

            if [ ! -n "$FLAT" ]; then
                is_it_flat_file
            fi

            FLATOPTION=''
            if [ "$FLAT" = 'yes' -a "$COMMAND" != 'convert' ]; then
                FLATOPTION='--directory'
            fi
            zenity --info --text="In the next window, please select the file you want to ${COMMAND}." \
                --title="orgx editor v$VERSION"

            ORGXFILE=`zenity --file-selection $FLATOPTION \
                --title="orgx editor v$VERSION"`

            [ "$?" = 1 ] && exit 1

            if [ "$COMMAND" = 'export' ]; then
                OUTPUTFORMAT=`zenity --list --title="orgx editor v$VERSION" \
                    --text="$FORMATSELECTMSG" \
                    --column="Export format" \
                    pdf "pdf and open it" odt "odt and open it" tex`
                case $OUTPUTFORMAT in
                    'pdf and open it')
                        OUTPUTFORMAT=pdf
                        SHALLOPENRESULT='yes'
                        ;;
                    'odt and open it')
                        OUTPUTFORMAT=odt
                        SHALLOPENRESULT='yes'
                        ;;
                esac
            fi
        else
            echo -en "Which file do you want to ${COMMAND}?\n> "
            read ORGXFILE
        fi
    fi
fi


case $COMMAND in
    -v|--version)
        if [ "$GRAPHIC" = 'yes' ]; then
            zenity --info --title="orgx editor v$VERSION" \
                --text="orgx editor v$VERSION\n\nWritten by Étienne Deparis"
        else
            echo "orgx editor v$VERSION"
            echo "Written by Étienne Deparis <etienne@depar.is>"
        fi
        ;;
    -h|--help)
        if [ "$GRAPHIC" = 'yes' ]; then
            usage_help | zenity --text-info --width=600 --height=600 \
                --title="orgx editor v$VERSION"
        else
            usage_help | fmt | more
        fi
        ;;
    create)
        prepare_tmp_dir
        echo "#+title: $ORGXFILETITLE
#+date: <"`date +"%Y-%m-%d %a %H:%M"`">" > $TMPNAME/content.org

        $EDITOR $TMPNAME/content.org

        post_create_orgx_file
        ;;
    edit)
        extract_orgx

        if [ "$FLAT" = "no" ]; then
            mv $ORGXFILE $ORGXFILE~
            echo ":: An archive has been kept under $ORGXFILE~"
        fi

        $EDITOR $TMPNAME/content.org

        cp $TMPNAME/content.org $TMPNAME/history/$DOCDATE
        sed -i "s/timestamp=[0-9]*/timestamp=$DOCDATE/gI" $TMPNAME/meta.conf

        if [ "$FLAT" = "no" ]; then
            cd $TMPNAME
            zip -qr $ORGXFILE *
            cd
            rm -rf $TMPNAME
        fi
        echo ":: Document saved under $ORGXFILE"
        ;;
    convert)
        if [ ! -f "$ORGXFILE" ]; then
            ERRORMSG="This document is not a file. Aborting."
            if [ "$GRAPHIC" = 'no' ]; then
                echo ""
                echo "ERROR: $ERRORMSG"
            else
                zenity --error --title="orgx editor v$VERSION" \
                    --text="$ERRORMSG"
            fi
            exit 1
        fi

        prepare_tmp_dir
        cp "$ORGXFILE" $TMPNAME/content.org
        $EDITOR $TMPNAME/content.org
        post_create_orgx_file

        ;;
    export)
        extract_orgx

        if [ "$GRAPHIC" = 'yes' ]; then
            export_orgx | zenity --progress --title="orgx editor v$VERSION" \
                --pulsate --auto-close --text="Processing $ORGXFILE"
        else
            echo ":: Processing $TMPNAME"
            export_orgx
            echo ":: Document saved under $EXPORTPATH/$FILENAME.$OUTPUTFORMAT"
        fi

        if [ "$FLAT" = "no" ]; then
            rm -r $TMPNAME
        fi

        if [ -n "$SHALLOPENRESULT" ]; then
            if [ ! -n "$FILENAME" ]; then
                echo ':: Trying to infer converted filename'
                OPENFILE=`echo $ORGXFILE | sed s/.orgx$/.$OUTPUTFORMAT/`
                if [ -f "$OPENFILE" ]; then
                    echo $OPENFILE
                elif [ -f "$ORGXFILE.$OUTPUTFORMAT" ]; then
                    # Maybe original file was a directory...
                    echo $OPENFILE
                fi
            else
                xdg-open "$EXPORTPATH/$FILENAME.$OUTPUTFORMAT" &
            fi
        fi
        ;;
    *)
        echo "There's something strange
in your neighborhood.
Who ya gonna call?"
        ;;
esac
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<