aboutsummaryrefslogtreecommitdiff
path: root/branches/0.6/src/mkbuild
blob: cae72240b4aeb93855ab6f1edd4868ad552b9e22 (plain)
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
#!/bin/bash -x
#
# Script para gerar SlackBuild
# Por Rudson R. Alves
#
# Este script auxilia a cria��o de SlackBuilds, com o modelo
# do Luiz do Sarava Linux
#
# SlackBuilds s�o scripts utilizados no Slackware para gerar
# pacotes tgz.
#
# Turn off debug
set +x
#
# Version 0.9.18
PROG_VERSION=0.9.18

#--------------------------------------------------------------------
# Functions
#--------------------------------------------------------------------
# Show ./configure --help
function config_help()
{
    view_file "Check ./configure --help" $CONFIG_HELP
}

# Show file
# INPUT: $1 string textbox
#        $2 file
function view_file()
{
    [ $# -ne 2 ] && return 1
    dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title " $1 " --textbox $2 20 75
}

# Exit function
function mk_exit()
{
    clean_all
    clear
    exit $1
}

# Clear all temp files
function clean_all()
{
    [ -s $DEPENDENCY_LIST ] && cp $DEPENDENCY_LIST $BASEDIR/slack-required

    rm $AUX_TMP 2>/dev/null
    rm $SLACKDESC_TMP 2>/dev/null
    rm $SLACKBUILD.test 2>/dev/null
    rm $DEPENDENCY_LIST 2>/dev/null
    if [ ! -z $MK_PKGNAME ]; then
        rm $BASEDIR/*_$MK_PKGNAME.mkbuild 2>/dev/null
        rm -rf /tmp/$MK_PKGNAME 2>/dev/null
        rm $CONFIG_HELP 2>/dev/null
    fi
}

# Print from file $2, the lines betwen <$1> and </$1> lines
function print_lines()
{
    [ $# -ne 2 ] && mk_exit 1
    eval "sed -n '/<$1>/,/<\/$1>/ { /<$1>/ b; /<\/$1>/ b; p; }' $2"
}

# Print from file $2, the lines betwen <$1> and $2 lines
function print_lines_to()
{
    [ $# -ne 3 ] && mk_exit 1
    eval "sed -n '/<$1>/,/$2/ { /<$1>/ b; /$2/ b; p; }' $3"
}

# Make slack-desc file
function mk_slackdesc()
{
    print_lines_to "slackdesc" "|-----" $SB_MODEL
    echo -n $MK_PKGNAME | tr [a-z+\-] " "
    echo -n "|-----handy-ruler"
    let N=18+${#MK_PKGNAME}
    for i in `seq $N $SLACKDESC_LEN`; do
        echo -n "-"
    done
    echo -en "|\n"

    for i in `seq 1 11`; do
        echo -en "$MK_PKGNAME: \n"
    done
    print_lines "slackdesc" $SB_MODEL | sed '1,/\]\]/ d'
}

# Get label status from SlackBuild model
function get_status()
{
    [ $# -ne 2 ] && mk_exit 1
    eval "sed -n '/<$1>/ p' $2 | sed 's/.*> \([a-z]\+\)/\1/'"
}

# Get status from edited SlackBuild
# input: $1 -> section name
# output: section status and section number
function mk_status()
{
    for i in `seq 0 $MAXSECTION`;do
        if [ "${SECTION_NAME[i]}" = "$1" ]; then
            echo "${SECTION_FLAG[i]} $i"
            return 0
        fi
    done
    return 1
}

# Edit file $3, by change string [[$1]] to $2
function edit_file()
{
    local STR_OLD
    local STR_NEW

    [ $# -ne 3 ] && mk_exit 1
    STR_OLD=$( echo $1 | sed 's/\//\\\//g' )
    STR_NEW=$( echo $2 | sed 's/\//\\\//g' )
    eval "sed 's/\[\[$STR_OLD\]\]/$STR_NEW/' $3 > $AUX_TMP"
    mv $AUX_TMP $3
}

# Unpackage source
function unpkg_source()
{
    # FIXME: if MK_DECOMPRESSOR -ne bunzip2 or gunzip...
    if [ $MK_DECOMPRESSOR = "bunzip2" -o $MK_DECOMPRESSOR = "gunzip" ]; then
        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --cancel-label "Main Menu" --title "$MK_TITLE" --infobox "Uncompress source in /tmp/$MK_PKGNAME. Wait" 3 45
        [ $? -ne 0 ] && return 100
        mkdir /tmp/$MK_PKGNAME/ 2>/dev/null
        tar xvf $MK_SOURCE -C /tmp/$MK_PKGNAME/ 1>/dev/null
        [ $? -ne 0 ] && mk_exit 1
        MK_TMPSRC="/tmp/$MK_PKGNAME/`ls /tmp/$MK_PKGNAME/`"

        DOC_FILES=`find $MK_TMPSRC -type f -maxdepth 1 -name "[A-Z]*" | sed 's/.*\/\(.*\)$/\1/'`
    else
        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --title "$MK_TITLE" --msgbox "$MK_DECOMPRESSOR not configurated..." 5 45
    fi
}

# Download source
function download_url()
{
    clear
    mkdir -p $MK_SOURCEDIR/$MK_PKGNAME 2>/dev/null
    wget --continue --limit-rate=$MK_LIMITRATE $MK_URL -P $MK_SOURCEDIR/$MK_PKGNAME/
    if [ $? -ne 0 ]; then
        echo "URL error"
        return 0
    fi
    MK_SOURCE=$MK_SOURCEDIR/$MK_PKGNAME/$MK_SRCNAME
}

# Select source file
function select_source()
{
    MK_SOURCE=$BASEDIR
    while [ ! -f $MK_SOURCE ]; do
        MK_SOURCE=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --cancel-label "Main Menu" --title " Select source file name (use spaces to select): " --fselect "$MK_SOURCE" 10 70`
        [ $? -ne 0 ] && return 100
        if [ ! -f $MK_SOURCE ]; then
            MK_SOURCE="$MK_SOURCE/"
        fi
    done

    MK_SRCNAME=`basename $MK_SOURCE`
    MK_SOURCEDIR=`dirname $MK_SOURCE`
}

#--------------------------------------------------------------------
# Edit functions
#--------------------------------------------------------------------
# Edit authot, Initials and editor
function edit_author()
{
    MK_AUTHOR=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with author complite name:" 8 60 "$MK_AUTHOR"`
    edit_file "AUTHOR NAME" "$MK_AUTHOR" ~/.mkslackbuild

    MK_AUTHOR_INITIALS=`echo $MK_AUTHOR | sed 's/ /\n/g' | sed 's/^\([A-Z]\).*/\1/' | sed ':i; $!N;  s/\n//; ti' | tr [A-Z] [a-z]`
    MK_AUTHOR_INITIALS=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with author assignature:" 8 60 "$MK_AUTHOR_INITIALS"`
    edit_file "AUTHOR INITIALS" "$MK_AUTHOR_INITIALS" ~/.mkslackbuild

    MK_EDITOR=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with editor command:" 8 60 "pico"`
    edit_file "EDITOR" "$MK_EDITOR" ~/.mkslackbuild

    MK_SOURCEDIR=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with sources directory:" 8 60 "\`pwd\`"`
    edit_file "SOURCE DIR" "$MK_SOURCEDIR" ~/.mkslackbuild

    if [ "$USER" != "root" ]; then
        SLACKBUILDRC=`[ -e ~/.slackbuildrc ] && echo ~/.slackbuildrc || echo /etc/slackbuildrc`
        MK_REPOS=`grep 'REPOS=' $SLACKBUILDRC | sed 's/REPOS=\(.*\)/\1/'`
    else
        SLACKBUILDRC=`su -c "[ -e ~/.slackbuildrc ] && echo ~/.slackbuildrc || echo /etc/slackbuildrc" 2>/dev/null`
        MK_REPOS=`su -c "grep 'REPOS=' $SLACKBUILDRC | sed 's/REPOS=\(.*\)/\1/'" 2>/dev/null`
    fi

    MK_REPOS=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with binary repository directory:" 8 60 "$MK_REPOS"`
    edit_file "REPOS DIR" "$MK_REPOS" ~/.mkslackbuild

    MK_SLACKBUILD_DIR=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with binary repository directory:" 8 60 "$MK_SLACKBUILD_DIR"`
    edit_file "SLACKBUILD DIR" "$MK_SLACKBUILD_DIR" ~/.mkslackbuild

    MK_ARCH=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter default architecture to SlackBuild:" 8 60 "i468"`
    edit_file "DEFAULT ARCH" "$MK_ARCH" ~/.mkslackbuild

    MK_LIMITRATE=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter wget limit rate:" 8 60 "120k"`
    edit_file "LIMIT RATE" "$MK_LIMITRATE" ~/.mkslackbuild
}

# Edit source URL
function edit_url()
{
    MK_URL=${MK_URL:="http://downloads.sourceforge.net/"}
    MK_URL=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --cancel-label "Main Menu" --title "$MK_TITLE" --inputbox "Enter with complite URL from source:" 8 60 "$MK_URL"`
    [ $? -ne 0 ] && return 100
}

# Edit source name
function edit_source_name()
{
    # Carrega nome do arquivo
    MK_SRCNAME=`basename $MK_URL`
    MK_SRCNAME=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --cancel-label "Main Menu" --title "$MK_TITLE" --inputbox "Enter with source file name:" 8 60 "$MK_SRCNAME"`
    [ $? -ne 0 ] &&  return 100
}

# Edit MK_PKGNAME, MK_VERSION and MK_EXTENSION
function edit_pkgname()
{
    # Remove nome, vers�o e extens�o do nome da fonte
    MK_SRC_NAME=`echo $MK_SRCNAME | sed -r 's/(.*)-(.*)\.(.*\..*)$/\1/'`
    MK_PKGNAME=`echo $MK_SRC_NAME | tr [A-Z_] [a-z\-]`
    MK_VERSION=`echo $MK_SRCNAME | sed -r 's/(.*)-(.*)\.(.*\..*)$/\2/'`
    MK_EXTENSION=`echo $MK_SRCNAME | sed -r 's/(.*)-(.*)\.(.*\..*)$/\3/'`

    # Configura nome, vers�o e extens�o do pacote
    ANS=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --cancel-label "Main Menu" --title "$MK_TITLE" \
        --form "Check information below:" 14 52 7 \
        "Source name:"  1 1 "$MK_SRC_NAME" 1 15 30 60 \
        "Package name:" 3 1 "$MK_PKGNAME"  3 15 30 60 \
        "Version:"      5 1 "$MK_VERSION"   5 15 30 60 \
        "Extension:"    7 1 "$MK_EXTENSION" 7 15 30 60`
    [ $? -ne 0 ] && return 100

    MK_SRC_NAME=`echo $ANS | awk '{print $1}'`
    MK_PKGNAME=`echo $ANS | awk '{print $2}'`
    MK_VERSION=`echo $ANS | awk '{print $3}'`
    MK_EXTENSION=`echo $ANS | awk '{print $4}'`
}

# Edit MK_DECOMPRESSOR and MK_DECOMPRESSOR_TEST_FLAG
function edit_decompress_arg()
{
    # Remove nome, vers�o e extens�o do nome da fonte
    MK_DECOMPRESSOR=""
    MK_DECOMPRESSOR_TEST_FLAG=""

    # Configura nome, vers�o e extens�o do pacote
    ANS3=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --cancel-label "Main Menu" --title "$MK_TITLE" \
        --form "Check information below:" 10 55 3 \
        "Uncompress program:" 1 1 "$MK_DECOMPRESSOR"  1 23 25 60 \
        "Uncompress test flag:" 3 1 "$MK_DECOMPRESSOR_TEST_FLAG"   3 23 25 100`
    [ $? -ne 0 ] && return 100

    MK_DECOMPRESSOR=`echo $ANS3 | awk '{print $1}'`
    MK_DECOMPRESSOR_TEST_FLAG=`echo $ANS3 | awk '{print $2}'`
}

# Edit configure options
function edit_configure()
{
    CONFIG_HELP=`mktemp -p /tmp/ config_$MK_PKGNAME.XXXXXX`

    $MK_TMPSRC/configure --help > $CONFIG_HELP
    config_help
    #[ "$( cat $CONFIG_HELP  | egrep '/usr/local')" != "" ] && MK_PREFIX=/usr || MK_PREFIX=/opt/kde
    MK_PREFIX="/usr"
    MK_OPTIONS=""
    local ANS0=1

    while [ $ANS0 -eq 1 ]; do
        local ANS1="7"
        while [ $ANS1 -ne 0 ]; do
            ANS1=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
                --cancel-label "Continue" --title "$MK_TITLE" \
                --menu "Select one option" 0 0 0 \
                1 "View ./configure --help" \
                2 "View documentations" \
                3 "PREFIX=$MK_PREFIX" \
                4 "OPTIONS=$MK_OPTIONS" \
                0 "Continue"`

            case $ANS1 in
                '1')
                    config_help
                ;;
                '2')
                    view_docs
                ;;
                '3')
                    MK_PREFIX=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with prefix for install $MK_PKGNAME" 8 60 "$MK_PREFIX"`
                ;;
                '4')
                    MK_OPTIONS=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with configure options for compile $MK_PKGNAME" 8 60 "$MK_OPTIONS"`
                ;;
                *)
                    ANS1=0
                ;;
            esac
        done

        # Configure
        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title " Execute ./configure below? " --yesno "./configure --prefix=$MK_PREFIX $MK_OPTIONS" 6 70
        if [ $? -eq 0 ]; then
            (
            cd $MK_TMPSRC
            ./configure --prefix=$MK_PREFIX $MK_OPTIONS
            )

            echo -en "\nPress ENTER to continue"
            read ANS0
        fi

        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title " Configure line is ok? " --yesno "./configure --prefix=$MK_PREFIX $MK_OPTIONS" 6 70

        ANS0=$?
    done
}

# Show Documentations files
function view_docs()
{
    local MENU=""
    local SELECT=""
    local ANS2=0

    for i in $DOC_FILES; do
        MENU="$MENU \"$i\" \"\" "
    done

    while [ $ANS2 -eq 0 ]; do
        SELECT=`eval "dialog --stdout --backtitle \"Make SlackBuild $PROG_VERSION [$MK_PKGNAME]\" --cancel-label \"Continue\" --title \"$MK_TITLE\" --menu \"Select doc-file below:\" 20 45 13 $MENU"`
        ANS2=$?
        [ $ANS2 -eq 0 ] && view_file "File: $SELECT" $MK_TMPSRC/$SELECT
    done
}

# Edit documentations files
function edit_docfiles()
{
    [ -z $MK_TMPSRC ] && return 0

    if [ -e $MK_TMPSRC ]; then
        LIST=`( cd $MK_TMPSRC; find . -type f -maxdepth 1 | cut -c3- )`
        DOCMENU=""
        for i in $LIST; do
            if [ "`echo NEWS TODO README AUTHORS INSTALL ChangeLog MAINTAINERS COPYING | grep $i`" != "" ]; then
                DOCMENU="$DOCMENU $i \"\" on"
            else
                DOCMENU="$DOCMENU $i \"\" off"
            fi
        done
        SELECTDOCS=`eval "dialog --stdout --separate-output --backtitle \"Make SlackBuild $PROG_VERSION [$MK_PKGNAME]\" --title \"$MK_TITLE\" --cancel-label \"Continue\" --checklist \"Select documentation files below:\" 20 45 13 $DOCMENU"`
    fi

    MK_DOCFILES="`echo $SELECTDOCS | sed ':i; N; s/\n/ /; bi'`"
}

# Edit compiler architecture
function edit_arch()
{
    MK_ARCH=`dialog --stdout --backtitle "Make SlackBuild documentation files below:"  --cancel-label "Main Menu" --title "$MK_TITLE" --inputbox "Set an architecture to compiler:" 8 45 $MK_ARCH`
    [ $? -ne 0 ] && return 100
}

#--------------------------------------------------------------------
# Mount files
#--------------------------------------------------------------------
# Change string $1 to $2 in all files in the current directory
function change_strings()
{
    [ $# -ne 2 ] && mk_exit 1
    #[ -z "$2" ] && return 0
    eval "grep -l \"\[\[$1\]\]\" *" | while read FILE; do
        edit_file "$1" "$2" $FILE
    done
}

# Build initial sections
function start_build()
{
    change_strings "SLACKBUILD AUTHOR" "$MK_AUTHOR"
    change_strings "SLACKBUILD AUTHOR INITIALS" $MK_AUTHOR_INITIALS
    change_strings "SOURCE NAME" "$MK_SRC_NAME"
    change_strings "PROGRAM NAME" "$MK_PKGNAME"
    change_strings "PACKAGE NAME" "$MK_PKGNAME"
    change_strings "DECOMPRESSOR" "$MK_DECOMPRESSOR"
    change_strings "DECOMPRESSOR TEST FLAG" "$MK_DECOMPRESSOR_TEST_FLAG"
    change_strings "PROGRAM URL" "${MK_URL//$MK_SRCNAME}"
    change_strings "ARCH" "$MK_ARCH"
    change_strings "VERSION" $MK_VERSION
    change_strings "SOURCE EXTENSION" ${MK_EXTENSION#tar.}
    change_strings "DOWNLOAD FOLDER URL" "`dirname $MK_URL`"
    if [ -z $MK_OPTIONS ]; then
        change_strings "OTHER CONFIGURE ARGS" "\"\""
    else
        change_strings "OTHER CONFIGURE ARGS" "$MK_OPTIONS"
    fi
    change_strings "DOCUMENTATION FILES" "$MK_DOCFILES"
    change_strings "PREFIX" "$MK_PREFIX"
}

# Mount final SlackBuild
function mount_slackbuild()
{
    dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --infobox "SlackBuild save in $SLACKBUILD" 5 45

    rm $SLACKBUILD 2>/dev/null
#    [ ! -e `dirname $SLACKBUILD` ] && mkdir `dirname $SLACKBUILD`
    for i in `seq 0 $MAXSECTION`; do
        if [ "${SECTION_FLAG[i]}" = "all" -o "${SECTION_FLAG[i]}" = "on" ]; then
            cat ${SECTION_NAME[i]}_$MK_PKGNAME.mkbuild >> $SLACKBUILD
            echo "" >> $SLACKBUILD
        fi
    done
    chmod +x $SLACKBUILD
}

# Mount SlackBuild to section $1
function mount_slackbuild_to()
{
    if [ $# -eq 1 ]; then
        END_SECTION=$1
        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --infobox "SlackBuild save in $SLACKBUILD.test" 5 45

        rm $SLACKBUILD.test 2>/dev/null
        let i=0
        while [ $i -le $END_SECTION ]; do
            if [ "${SECTION_FLAG[i]}" = "all" -o "${SECTION_FLAG[i]}" = "on" ]; then
                cat ${SECTION_NAME[i]}_$MK_PKGNAME.mkbuild >> $SLACKBUILD.test
                echo "" >> $SLACKBUILD
            fi
            let i++
        done
        echo "" >> $SLACKBUILD
    fi
}

# Make files section
function make_file_sections()
{
    THIS_MODEL=$1

    # Create SlackBuilds-sections files
    rm *_$MK_PKGNAME.mkbuild 2>/dev/null

    # Make files section
    INDEX=0
    while read ITEM; do
        SECTION_NAME[INDEX]="$ITEM"

        AUX=${SB_SECTION[$INDEX,0]}_${MK_PKGNAME}
        SECTION_FILE=${SECTION_NAME[INDEX]}_${MK_PKGNAME}.mkbuild
        rm $SECTION_FILE 2>/dev/null

        STRING=$ITEM
        # Save section status flag
        SECTION_FLAG[INDEX]=`get_status "$STRING" $THIS_MODEL`

        # Create sections files
        print_lines "$STRING" $THIS_MODEL  > $SECTION_FILE
        let INDEX++
    done < <( cat $THIS_MODEL | grep '^<[a-z_0-9]\+>' | sed 's/<\(.*\)>.*$/\1/' )

    let INDEX--
    MAXSECTION=$INDEX
    md5sum *.mkbuild > md5sum
}

function get_source_dialog()
{
    if [ ! -e $MK_SOURCE ]; then
        dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --title "$MK_TITLE" --yesno " Download $MK_SRCNAME? " 5 50
        if [ $? -eq 0 ]; then
            download_url
        else
            select_source
            [ $? -eq 100 ] && return 100
        fi
    fi
}


function test_source()
{
    local TEST=1
    local ANS=0

    while [ $TEST -ne 0 ]; do
        dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --infobox "Test source with $MK_DECOMPRESSOR $MK_DECOMPRESSOR_TEST_FLAG $MK_SOURCE" 5 50
        $MK_DECOMPRESSOR $MK_DECOMPRESSOR_TEST_FLAG $MK_SOURCE
        TEST=$?
        if [ $TEST -ne 0 ]; then
            dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --yesno "Source uncompress error. Download source again?" 6 50
            if [ $? -eq 0 ]; then
                rm $MK_SOURCE
                get_source_dialog
                [ $? -eq 100 ] return 100
            else
                TEST=0
            fi
        fi
    done
}

# Configure test source variables
function set_source_test()
{
    MK_SOURCE=$MK_SOURCEDIR/$MK_PKGNAME/$MK_SRCNAME
    if [ "$MK_EXTENSION" = "tar.gz" -o "$MK_EXTENSION" = "gz" -o "$MK_EXTENSION" = "tgz" ]; then
        MK_DECOMPRESSOR=gunzip
        MK_DECOMPRESSOR_TEST_FLAG="-t"
    elif [ "$MK_EXTENSION" = "tar.bz2" -o "$MK_EXTENSION" = "bz2" ]; then
        MK_DECOMPRESSOR=bunzip2
        MK_DECOMPRESSOR_TEST_FLAG="-t"
    else
        edit_decompress_arg
        [ $? -eq 100 ] && return 100
    fi
}

# Enable MD5 or GPG signature check
function edit_signature()
{
    # GPG and MD5 test
    local ANS=0

    ANS=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --title "$MK_TITLE" --menu "Select code validate type:" 0 0 0 \
        "1" "gpg signature" \
        "2" "md5sum" \
        "0" "none"`

    case $ANS in
        1)
            edit_gpg
        ;;
        2)
            edit_md5sum
        ;;
    esac
}

# Enable GPG signature check
function edit_gpg()
{
    dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --title "$MK_TITLE" --msgbox "Enter with gpg minimized signing key." 5 50
    $EDITOR $MK_GPGFILEKEY
}

# Enable MD5SUM signature check
function edit_md5sum()
{
    echo "none"
}

#--------------------------------------------------------------------
# Main functions
#--------------------------------------------------------------------
# Create new SlackBuild
function create_slackbuild()
{
    # Config package URL
    edit_url
    [ $? -eq 100 ] && return 100

    # Source Name
    edit_source_name
    [ $? -eq 100 ] && return 100

    # Package name, version and extension
    edit_pkgname
    [ $? -eq 100 ] && return 100

    # Compile arch
    edit_arch
    [ $? -eq 100 ] && return 100

    # Set source test variables
    set_source_test
    [ $? -eq 100 ] && return 100

    # Get source file
    MK_SOURCE=$MK_SOURCEDIR/$MK_PKGNAME/$MK_SRCNAME
    get_source_dialog
    [ $? -eq 100 ] && return 100

    # Test source
    test_source
    [ $? -eq 100 ] && return 100

    # Uncompress source
    unpkg_source
    [ $? -eq 100 ] && return 100

    # GPG and MD5 test
#    edit_signature

    # Configure compiler options if exist
    if [ -e $MK_TMPSRC/configure ]; then
        edit_configure
    fi

    # Edit documentation list
    edit_docfiles

    # Create files sections
    make_file_sections $SB_MODEL

    # SlackBuild script name
    SLACKBUILD=$BASEDIR/$MK_PKGNAME.SlackBuild

    # mkSlackBuild script name
    # FIXME: make an MODELDIR???
    MKSLACKBUILD=$BASEDIR/$MK_PKGNAME.mkslackbuild

    mk_slackdesc > slackdesc_$MK_PKGNAME.mkbuild

    # Edit builds sections
    start_build

    # Main-Menu
    edit_menu
}

# Open mkslackbuild dialog input
function open_mkslackbuild_dialog()
{
    MKSLACKBUILD=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --title " Select source file name: " \
            --fselect "$BASEDIR/" 10 70`
    [ $? -ne 0 ] && return 100

    open_mkslackbuild
    [ $? -eq 100 ] || return 100
}

# Open mkslackbuild
function open_mkslackbuild()
{
    if [ -f $MKSLACKBUILD ]; then
        # Start defaults variables
        source $MKSLACKBUILD

        # SlackBuild scrit name
        SLACKBUILD=$BASEDIR/$MK_PKGNAME.SlackBuild

        # Create files sections
        make_file_sections $MKSLACKBUILD

        [ -e $BASEDIR/slack-required ] && ( cp $BASEDIR/slack-required $DEPENDENCY_LIST )

    else
        dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --title "$MK_TITLE" --msgbox "$MKSLACKBUILD most be a mkslackbuild file" 6 45
        return 100
    fi

    # CHECK:
    # Set source test vadiables
    set_source_test
    [ $? -eq 100 ] && return 100

    # Get source file
    get_source_dialog
    [ $? -eq 100 ] && return 100

    # Test source
    test_source

    # Uncompress source
    unpkg_source
}

# Save mkSlackBuild
function save_mkslackbuild()
{
    dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --infobox "mkSlackBuild save in $MKSLACKBUILD" 5 30

    rm $MKSLACKBUILD 2>/dev/null
    echo '#!/bin/bash' > $MKSLACKBUILD
    echo -e "# mkSlackBuild version $MK_VERSION\n" >> $MKSLACKBUILD
    echo "MK_SRC_NAME=\"$MK_SRC_NAME\"" >> $MKSLACKBUILD
    echo "MK_SRCNAME=\"$MK_SRCNAME\"" >> $MKSLACKBUILD
    echo "MK_PKGNAME=\"$MK_PKGNAME\"" >> $MKSLACKBUILD
    echo "MK_URL=\"$MK_URL\"" >> $MKSLACKBUILD
    echo "MK_AUTHOR=\"$MK_AUTHOR\"" >> $MKSLACKBUILD
    echo "MK_AUTHOR_INITIALS=\"$MK_AUTHOR_INITIALS\"" >> $MKSLACKBUILD
    echo "MK_VERSION=\"$MK_VERSION\"" >> $MKSLACKBUILD
    echo "MK_EXTENSION=\"$MK_EXTENSION\"" >> $MKSLACKBUILD
    echo "MK_DECOMPRESSOR=\"$MK_DECOMPRESSOR\"" >> $MKSLACKBUILD
    echo "MK_DECOMPRESSOR_TEST_FLAG=\"$MK_DECOMPRESSOR_TEST_FLAG\"" >> $MKSLACKBUILD
    echo "MK_DOCFILES=\"$MK_DOCFILES\"" >> $MKSLACKBUILD
    echo "MK_OPTIONS=\"$MK_OPTIONS\"" >> $MKSLACKBUILD
    echo "MK_PREFIX=\"$MK_PREFIX\"" >> $MKSLACKBUILD
    echo "MK_MD5SUM_EXT=\"$MK_MD5SUM_EXT\"" >> $MKSLACKBUILD
    echo -e "return 0\n\n" >> $MKSLACKBUILD

    for i in `seq 0 $MAXSECTION`; do
        echo "<${SECTION_NAME[i]}> ${SECTION_FLAG[i]}" >> $MKSLACKBUILD
        cat ${SECTION_NAME[i]}_$MK_PKGNAME.mkbuild >> $MKSLACKBUILD
        echo "</${SECTION_NAME[i]}>" >> $MKSLACKBUILD
        echo "" >> $MKSLACKBUILD
    done

    [ -s $DEPENDENCY_LIST ] && cp $DEPENDENCY_LIST $BASEDIR/slack-required
}

# Edit Menu
function edit_menu()
{
    ANSE=0
    while [ $ANSE -ne 1 ]; do
        # Create main-menu string
        EDITMENU=""
        for i in `seq 0 $MAXSECTION`; do
            #echo "${SECTION_NAME[i]} ${SECTION_FLAG[i]} ${SECTION_FILE[i]}"
            EDITMENU="$EDITMENU ${SECTION_NAME[i]} ${SECTION_FLAG[i]}"
        done

       SELECT=`eval "dialog --stdout --backtitle \"Make SlackBuild $PROG_VERSION [$MK_PKGNAME]\" --ok-label \"Edit\" --cancel-label \"Main Menu\" --title \"$MK_TITLE\" --menu \"Select one section to edit\" 20 40 13 $EDITMENU"`
        ANSE=$?
        if [ $ANSE -ne 1 ]; then
            STATUS=`mk_status $SELECT`
            if [ $? -ne 0 ]; then
                echo "Section name error..."
                mk_exit 1
            fi
            FLAG=`echo $STATUS | awk '{print $1}'`
            INDEX=`echo $STATUS | awk '{print $2}'`
            if [ "$FLAG" = "on" ]; then
                SECTION_FLAG[$INDEX]=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --radiolist "Change $SELECT status?" 0 0 0 "on" "enable section" "on" "off" "desable section" "off"`
            elif [ "$FLAG" = "off" ]; then
                SECTION_FLAG[$INDEX]=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --radiolist "Change $SELECT status?" 0 0 0 "on" "enable section" "off" "off" "desable section" "on"`
            fi
            if [ $? -eq 0 ]; then
                if [ "${SECTION_FLAG[$INDEX]}" = "on" -o "${SECTION_FLAG[$INDEX]}" = "all" ]; then
                    if [ "$SELECT" = "md5sum_download_and_check" ]; then
                        md5sum_edit ${SELECT}_${MK_PKGNAME}.mkbuild
#                    elif [ "$SELECT" = "gpg_signature_check" ]; then
#                        gpg_edit
                    else
                        $MK_EDITOR ${SELECT}_${MK_PKGNAME}.mkbuild
                    fi
                fi
            else
                SECTION_FLAG[$INDEX]=$FLAG
            fi
        fi
    done
}

# Edit md5sum
function md5sum_edit()
{
    local SELECT=0
    local FILE=$1

    if ( grep '\[\[MD5SUM EXTENSION\]\]' $FILE &>/dev/null ); then
        SELECT=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --radiolist "Select one option" 0 0 0 "1" "$MK_SRC_NAME-$MK_VERSION.[[MD5SUM EXT]]" on "2" "$MK_SRCNAME.[[MD5SUM EXT]]" off  "3" "MK5SUM CODE" off`

        case $SELECT in
            1)
                MK_MD5SUM_EXT="md5sum"
                MK_MD5SUM_EXT=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with md5sum extension:" 8 50 $MK_MD5SUM_EXT`
                edit_file "MD5SUM EXTENSION" "$MK_MD5SUM_EXT" $FILE
            ;;
            2)
                MK_MD5SUM_EXT="md5"
                MK_MD5SUM_EXT=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with md5sum extension:" 8 50 $MK_MD5SUM_EXT`
                eval "sed 's/^MD5SUM_FILE.*$/MD5SUM_FILE=\$SRC.$MK_MD5SUM_EXT/' $FILE > $AUX_TMP"
                cp $AUX_TMP $FILE
            ;;
            3)
                MK_MD5SUM_CODE=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter with md5sum extension:" 8 50 $MK_MD5SUM_CODE`
                sed '1! d' $FILE > $AUX_TMP
                sed -n '2, /^fi/ p' $FILE | while read LINE; do echo "#$LINE" >> $AUX_TMP; done
                sed -n '/^fi/, $ { /^fi/ b; p; }' $FILE >> $AUX_TMP
                eval "sed 's/^MD5SUM_SRC.*/MD5SUM_SRC=\"$MK_MD5SUM_CODE\"/' $AUX_TMP > $FILE"
            ;;
        esac
    fi

    $MK_EDITOR $FILE
}

# Test Menu
function test_menu()
{
    local ANST=0
    local SECTION=0
    local EDITMENU=""

    # Create main-menu string
    for i in `seq 0 $MAXSECTION`; do
        if [ "${SECTION_FLAG[i]}" = "all" -o "${SECTION_FLAG[i]}" = "on" ]; then
            EDITMENU="$EDITMENU $i ${SECTION_NAME[i]}"
        fi
    done

    EDITMENU="all  \"SlackBuild\" $EDITMENU"

    while [ "$ANST" != "1" ]; do
        SECTION=`eval "dialog --stdout --backtitle \"Make SlackBuild $PROG_VERSION [$MK_PKGNAME]\" --cancel-label \"Exit\" --title \"$MK_TITLE\" --menu \"Select section to test end\" 20 40 13 $EDITMENU"`
        ANST=$?

        if [ $ANST -ne 1 ]; then
            [ "$SECTION" = "all" ] && SECTION=$MAXSECTION
            mount_slackbuild_to $SECTION

            # FIXME:  -> sintaxe check
            clear
            echo "---------------------------------------------------------"
            echo "                 --= SINTAX CHECK =--"
            sh -n $SLACKBUILD.test
            if [ $? -eq 0 ]; then
                echo "                     --= 0K =--"
                DEBUG=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
                    --cancel-label "no debug" --title "$MK_TITLE" \
                    --menu "Select debug level" 10 35 3 \
                    "-x"    "Simple debug" \
                    "-xv"   "More information" \
                    "none"  "No debug"`

                [ $? -ne 0 -o "$DEBUG" = "none" ] && DEBUG=""

                clear
                echo "---------------------------------------------------------"
                echo "                 --= START BUILDING =--"
                if [ "$USER" != "root" ]; then
                    su -c "( sh $DEBUG $SLACKBUILD.test )"
                else
                    ( sh $DEBUG $SLACKBUILD.test )
                fi
                echo "---------------------------------------------------------"
                echo "                      --= END =--"

                echo -n "Enter 1 to end test: "
                read ANST
            else
                echo "Script error..."
                read ANST
            fi
        fi
    done
}

# Dependency Menu
function menu_dependency()
{
    local ANSD=11
    while [ $ANSD -ne 0 ]; do
        ANSD=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
            --cancel-label "Main Menu" --title "$MK_TITLE" --menu "Main Menu:" 0 0 0 \
            "1" "Edit dependency list" \
            "2" "Test dependency list"`

        [ $? -ne 0 ] && ANSD=0

        case $ANSD in
            1)
                edit_dependency
            ;;
            2)
                test_dependency
            ;;
        esac
    done
}

# Dependency list edit
function edit_dependency()
{
    if [ ! -s $DEPENDENCY_LIST ]; then
        echo -e "# Dependency list to $MK_PKGNAME\n# \n# dependency [condition] [version]]\n" > $DEPENDENCY_LIST
    fi

    $MK_EDITOR $DEPENDENCY_LIST
}

# Check dependency and comment not found dependencies
function test_dependency()
{
    local DEP_LIST=""

    if [ -s $DEPENDENCY_LIST ]; then
        for i in `grep '^[^#]' $DEPENDENCY_LIST  | awk '{print $1}' | sort | uniq`; do
            if [ `ls /var/log/packages/$i-[0-9]* 2>/dev/null` ]; then
                DEP_LIST="$DEP_LIST $i \"intalled\" off"
            else
                DEP_LIST="$DEP_LIST $i \"unintalled\" on"
            fi
        done

        DEP_SELECT=`eval "dialog --stdout --separate-output --backtitle \"Make SlackBuild $PROG_VERSION [$MK_PKGNAME]\" --title \"$MK_TITLE\" --checklist \"Select packages to remove from dependency list:\" 0 0 0 $DEP_LIST"`
        # 20 45 13

        for i in $DEP_SELECT; do
            eval "sed 's/$i$/#$i/' $DEPENDENCY_LIST > $AUX_TMP "
            eval "sed 's/$i /#$i /' $AUX_TMP > $DEPENDENCY_LIST"
            eval "sed 's/$i\t/#$i\t/' $DEPENDENCY_LIST > $AUX_TMP"
            cp $AUX_TMP $DEPENDENCY_LIST
        done
    fi
}

# Add/commit one SlackBuild
function commit_SlackBuild()
{
    # Mount SlackBuild
    mount_slackbuild
    MK_TITLE="Commit SlackBuild"

    # Find SlackBuild
    SB_PATH=`find $MK_SLACKBUILD_DIR -iname $MK_PKGNAME`

    # Change to SlackBuild directory
    cd $MK_SLACKBUILD_DIR

    # Check for new SlackBuild
    if [ -z $SB_PATH ]; then
        # New SlackBuild
        AUX=""
        while [ -z $AUX ]; do
            dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --infobox "lynx -dump http://gentoo-portage.com/Search?search=$MK_PKGNAME. Wait..." 0 0
            AUX=`lynx -dump http://gentoo-portage.com/Search?search=$MK_PKGNAME | sed -n '/Results:/,+1 { /Results:/ b ; p }' | head --lines=1 | tr -d " "`
            # Default directory
            if [ -z $AUX ]; then
                dialog --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --yesno "Net search error. Try again?" 0 0
                if [ $? -ne 0 ]; then
                    AUX="/others/unclass"
                    SB_PATH=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter dir-name to SlackBuild from $MK_PKGNAME:" 8 60 "$AUX"`
                fi
            else
                SB_PATH="`dirname $AUX | tr - /`/`basename $AUX`"
            fi
        done
        # Make directory and add SlackBuild to svn
        svn mkdir $SB_PATH
        cp $BASEDIR/$MK_PKGNAME.SlackBuild $SB_PATH/
        svn add $SB_PATH/$MK_PKGNAME.SlackBuild
        if [ -e $BASEDIR/slack-required ]; then
            cp $BASEDIR/slack-required $SB_PATH/
            svn add $SB_PATH/slack-required
        fi
    else
        echo "cp $BASEDIR/$MK_PKGNAME.SlackBuild $SB_PATH/"
        if [ -e $BASEDIR/slack-required ]; then
            cp $BASEDIR/slack-required $SB_PATH/
        fi
    fi

    # Commit SlackBuild
    MK_MSG=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" --title "$MK_TITLE" --inputbox "Enter message do commit command:" 8 60`
    svn commit -m "$MK_PKGNAME: $MK_MSG"

    cd $BASEDIR

    echo -e "\nPress ENTER to continue."
    read ANS1
}

# Compile SlackBuild and install
function compile_install()
{
    local ANS=""
    # Mount SlackBuild
    mount_slackbuild

    # Clean temporary file
    echo " " > $AUX_TMP

    if [ "$USER" = "root" ]; then
        # Root user
        # Compile pid
        PID=`exec-slackbuild $SLACKBUILD $AUX_TMP`

        # Tail messages
        tail -s .1 --pid=$PID -f $AUX_TMP

        # Package name
        PACKAGE=`tail -10 $AUX_TMP | grep "^Moving $MK_PKGNAME-[0-9].*\.\.\.$" | awk '{ print $2 }' | grep "$MK_PKGNAME-.*\.tgz$"`

        # Install package
        if [ ! -z $PACKAGE ]; then
            installpkg $MK_REPOS/$PACKAGE
        else
            echo -e "\nBuild error!"
        fi
    else
        # Commum users
        # Compile pid
        PID=`su -c "exec-slackbuild $SLACKBUILD $AUX_TMP"`

        # Tail messages
        tail -s .1 --pid=$PID -f $AUX_TMP

        # Package name
        PACKAGE=`tail -10 $AUX_TMP | grep "^Moving $MK_PKGNAME-[0-9].*\.\.\.$" | awk '{ print $2 }' | grep "$MK_PKGNAME-.*\.tgz$"`

        # Install package
        if [ ! -z $PACKAGE ]; then
            su -c "installpkg $MK_REPOS/$PACKAGE"
        else
            echo -e "\nBuild error!"
        fi
    fi

    echo -e "\nPress ENTER to continue."
    read ANS
}

#
#--------------------------------------------------------------------
#                        Start Program
#--------------------------------------------------------------------
#
# Start variables
LANG=en_US
BASEDIR=`pwd`
AUX_TMP=`mktemp -p  /tmp/ aux_tmp.XXXXXX`
SLACKDESC_TMP=`mktemp -p  /tmp/ slackdesc_tmp.XXXXXX`
SLACKDESC_LEN=77
DEPENDENCY_LIST=`mktemp -p  /tmp/ slack-required.XXXXXX`
if [ -z $SB_MODEL ]; then
    if [ -e ~/.generic.mkSlackBuild ]; then
        SB_MODEL=~/.generic.mkSlackBuild
    elif [ -e /etc/simplepkg/generic.mkSlackBuild ]; then
        SB_MODEL=/etc/simplepkg/generic.mkSlackBuild
    else
        echo "SlackBuild model (generic.mkSlackBuild) not found"
        mk_exit 0
    fi
fi
[ $# -ne 0 ] && MKSLACKBUILD=${1//.*}.mkslackbuild

# Clear dialog
dialog --clear

# Load/create personal definitions
if [ ! -e ~/.mkslackbuild ]; then
    ( cat << EOF > ~/.mkslackbuild
#!/bin/bash
# mkslackbuild definitions
#
MK_AUTHOR="[[AUTHOR NAME]]"
MK_AUTHOR_INITIALS="[[AUTHOR INITIALS]]"
MK_EDITOR=[[EDITOR]]
MK_REPOS=[[REPOS DIR]]
MK_LIMITRATE=[[LIMIT RATE]]
MK_SOURCEDIR=[[SOURCE DIR]]
MK_ARCH=[[DEFAULT ARCH]]
MK_SLACKBUILD_DIR=[[SLACKBUILD DIR]]
#SB_MODEL="my_model"
EOF
)
fi
source ~/.mkslackbuild
MK_SOURCEDIR=${MK_SOURCEDIR:=$BASEDIR}
MK_ARCH=${MK_ARCH:="i486"}

# Config Author
if [ "$MK_AUTHOR" = "[[AUTHOR NAME]]" ]; then
    MK_TITLE=" Personal Config "
    edit_author
    MK_TITLE=""
fi

if [ ! -z $MKSLACKBUILD ]; then
    MK_TITLE=" Open mkSlackBuild "
    open_mkslackbuild
fi

MK_TITLE=""

ANS0=11
while [ $ANS0 -ne 0 ]; do
    if [ -z $MK_PKGNAME ]; then
        ANS0=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --cancel-label "Exit" --menu "Main Menu:" 0 0 0 \
        "1" "Create New SlackBuild" \
        "2" "Open mkSlackBuild" \
        "0" "Exit"`

        if [ $? -ne 0 ]; then
            ANS0=0
        fi

        case $ANS0 in
            1)
                MK_TITLE=" Create SlackBuild "
                create_slackbuild
                [ $? -eq 100 ] && MK_PKGNAME=""
            ;;
            2)
                MK_TITLE=" Open mkSlackBuild "
                open_mkslackbuild_dialog
                [ $? -eq 100 ] && MK_PKGNAME=""
            ;;
            *)
                echo "Exit..."
                ANS0=0
            ;;
        esac
         MK_TITLE=""
    else
        ANS0=`dialog --stdout --backtitle "Make SlackBuild $PROG_VERSION [$MK_PKGNAME]" \
        --cancel-label "Exit" --menu "Main Menu:" 0 0 0 \
        "1" "Edit SlackBuild sections" \
        "2" "Edit dependency list" \
        "3" "Test SlackBuild script" \
        "4" "View Doc-Files" \
        "5" "Save SlackBuild and mkslackbuild" \
        "6" "Compile/install package" \
        "7" "Remove package" \
        "8" "Commit SlackBuild to Sarava"`

        if [ $? -ne 0 ]; then
            ANS0=0
        fi
        case $ANS0 in
            1) # Edit Sections
                MK_TITLE=" Edit Menu "
                edit_menu
            ;;
            2) # Dependency Menu
                MK_TITLE=" Dependency Menu "
                menu_dependency
            ;;
            3) # Test Script
                MK_TITLE=" Test Menu "
                test_menu
            ;;
            4) # View Doc-Files
                MK_TITLE=" View Docs "
                view_docs
            ;;
            5) # Save SlackBuild
                save_mkslackbuild
                mount_slackbuild
            ;;
            6) # Compile/install package
                compile_install
            ;;
            7) # Remove package
                if [ "$USER" = "root" ]; then
                    removepkg $MK_PKGNAME
                else
                    su -c "( removepkg $MK_PKGNAME )"
                fi
                echo -e "\nPress ENTER to continue."
                read ANS1
            ;;
            8) # Commit SlackBuild to Sarava
                commit_SlackBuild
            ;;
            0) # Exit
                ANS0=0
            ;;
        esac
        MK_TITLE=""
    fi
done

# Check changes
if [ ! -z $MK_PKGNAME ]; then
    md5sum -c md5sum
    if [ $? -ne 0 ]; then
        MKSLACKBUILD=$BASEDIR/$MK_PKGNAME.mkslackbuild
        save_mkslackbuild
        mount_slackbuild
    fi
fi

mk_exit 0
# _________________________________END_______________________________