aboutsummaryrefslogtreecommitdiff
path: root/trunk/src/mkbuild
blob: 7fba3d8f3e0db067537973964c29cc4bca5feda9 (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
#!/bin/bash
#
# mkbuild: SlackBuild scripts maker
# feedback: rudsonaalves at yahoo.com.br | gpl
#
#  mkbuild is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  mkbuild is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#
# Based in model generic.SlackBuild of Luiz
# Version $Rev$ - $Author$

PROG_VERSION="1.2.8"
PROG_NAME="`basename $0`"

#--------------------------------------------------------------------
# Functions
#--------------------------------------------------------------------
function mkbuild_use()

{
  # mkbuild help function
  echo -e "${red}NAME${normal}
        mkbuild - create SlackBuild script from ${green}.mkbuild${normal} input file

${red}SYNOPSIS
        mkbuild${normal} [${green}OPTIONS${normal}] [${green}mkbuild_file${normal}]

${red}DESCRIPTION${normal}
        [${green}mkbuild_file${normal}] input file with build rules and variables

        Input ${green}OPTIONS${normal}:
            ${red}-a${normal}, ${red}--author${normal} ${green}<author_name>${normal}
                author name
            ${red}-ai${normal}, ${red}--author_initials${normal} ${green}<initials>${normal}
                author signature
            ${red}-bn${normal}, ${red}--build-number${normal}
                change build number
            ${red}-cs${normal}, ${red}--const_string${normal} ${green}<string>${normal}
                construction string to source name
            ${red}-j${normal}, ${red}--jobs${normal} ${green}<jobs_number>${normal}
                Number of jobs to run simultaneously
            ${red}-md${normal}, ${red}--model${normal} ${green}<SlackBuild_model>${normal}
                SlackBuild model file
            ${red}-npss${normal}, ${red}--nps-strip${normal}
                Number of prefix slashes to strip
            ${red}-pf${normal}, ${red}--patch-files${normal}
                List of patch files
            ${red}-pn${normal}, ${red}--pkg_name${normal} ${green}<package_name>${normal}
                package name
            ${red}--prefix${normal} <install_dir>${normal}
                Prefix install directory
            ${red}-pv${normal}, ${red}--pkg_version${normal} ${green}<version>${normal}
                package version
            ${red}-sn${normal}, ${red}--src_name${normal} ${green}<source_name>${normal}
                source name
            ${red}-u${normal}, ${red}--url${normal} ${green}<url_address>${normal}
                url address to source

        Program options:
        ${red}-d${normal}, ${red}--debug${normal}
            enable debug mode
        ${red}-ss${normal}, ${red}--submit-slackbuild${normal}
            submit SlackBuilds in local svn SlackBuild tree
        ${red}-sm${normal}, ${red}--submit-mkbuild${normal}
            submit .mkbuild in local svn mkbuild tree
        ${red}-sa${normal}, ${red}--submit-all${normal}
            submit SlackBuild and .mkbuild files in local svn tree
        ${red}-cs${normal}, ${red}--commit-slackbuild${normal}
            commit SlackBuilds in svn SlackBuild tree
        ${red}-cm${normal}, ${red}--commit-mkbuild${normal}
            commit .mkbuild in svn mkbuild tree
        ${red}-ca${normal}, ${red}--commit-all${normal}
            commit SlackBuild and .mkbuild files in svn tree
        ${red}-h${normal}, ${red}--help${normal}
            this help mesage
        ${red}-n${normal}, ${red}--new${normal} ${green}<mkbuild_name>${normal}
            start a new mkbuild configure file
        ${red}-s${normal}, ${red}--search${normal} ${green}<mkbuild_name>${normal}
            search for a ${green}<mkbuild_name>${normal} file
        ${red}--sync${normal}
            synchronize mkbuilds repository
        ${red}-v${normal}, ${red}--version${normal}
            program version
        ${red}-V${normal}, ${red}--verbose${normal}
            print debug information

${red}EXAMPLES${normal}
        ${red}mkbuild -sa pyrex.mkbuild${normal}
            build pyrex.SlackBuild and submit .mkbuild and .SlackBuild in
            Slack.Sarava local tree.
        ${red}mkbuild -a \"Jose Araujo\" -ai \"ja\" -n pyrex${normal}
            make a basic pyrex.mkbuild with author name \"Jose Araujo\" and
            author signature \"ja\".
        ${red}mkbuild --prefix /usr/local pyrex.mkbuild${normal}
            build pyrex.SlackBuild with prefix /usr/local and pyrex.mkbuild
            variables and options definitions.

${red}AUTHOR${normal}
        Written by ${blue}Rudson R. Alves${normal}

${red}AVAILABILITY${normal}
        by svn: ${yellow}svn checkout svn://slack.sarava.org/simplepkg${normal}
        this mkbuild is found in branches/0.6/

${red}REPORTING BUGS${normal}
        Report bugs to <${blue}rudsonaalves[at]rra.etc.br${normal}>

${red}COPYRIGHT${normal}
        Copyright � 2006 Free Software Foundation, Inc.
        This is free software. You may redistribute copies of it under the
        terms of the GNU General Public License
        <${yellow}http://www.gnu.org/licenses/gpl.html${normal}>. There is NO WARRANTY,
        to the extent permitted by law." | more
}

function set_parameters {

  # Get and set mkbuild variables with parameters input
  # Use: set_parameters $@
  #           where $@ are the parameters input
  #
  # Parameters analyze
  ACTION="build"

  while [ "$1" ]; do
    case $1 in
      '-bn'|'--build-number')
        # Build Number
        BUILD_NUMBER=$2
        is_number $BUILD_NUMBER || handle_error "$ERROR_NOT_NUMBER" "--build-number"
        shift
      ;;
      '-ss'|'--submit-slackbuild')
        # Submit SlackBuild file
        SUBMIT_SLACKBUILD=$on
      ;;
      '-sm'|'--submit-mkbuild')
        # Submit mkbuild file
        SUBMIT_MKBUILD=$on
      ;;
      '-sa'|'--submit-all')
        # Submit SlackBuild and mkbuild file
        SUBMIT_SLACKBUILD=$on
        SUBMIT_MKBUILD=$on
      ;;
      '-cs'|'--commit-slackbuild')
        # Commit SlackBuild file
        shift
        commit_changes $SLACKBUILDS_DIR $*
        exit 0
      ;;
      '-cm'|'--commit-mkbuild')
        # commit mkbuild file
        shift
        commit_changes $MKBUILDS_DIR $*
        exit 0
      ;;
      '-ca'|'--commit-all')
        # Commit SlackBuild and mkbuild file
        shift
        commit_changes $SLACKBUILDS_DIR $*
        commit_changes $MKBUILDS_DIR $*
        exit 0
      ;;
      '-n'|'--new')
        # New mkbuild configure file
        MKBUILD_NAME="${2//.mkbuild}.mkbuild"
        [ ${MKBUILD_NAME:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR "--new <mkbuild_name>"
        ACTION="new"
        shift
      ;;
      '-s'|'--search')
        # Search for a mkbuild file
        MKBUILD_NAME="${2//.mkbuild}.mkbuild"
        [ ${MKBUILD_NAME:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR "--search <mkbuild_name>"
        ACTION='search'
      ;;
      '-d'|'--debug')
        # Debug mode
        set -x
      ;;
      '-h'|'--help' )
        # Show help mesage
        mkbuild_use && exit 0
      ;;
      '--sync' )
        # Synchronize mkbuilds repository
        sync_repo $MKBUILDS_DIR $MKBUILDS_SVN
        cd $WORK
      ;;
      '-v'|'--version')
        # Show program version
        eecho $normal "\n$PROG_NAME version $PROG_VERSION\n"
      ;;
      '-V' | '--verbose')
        # Enable verbose mode
        VERBOSE=1
      ;;
      '-a'|'--author')
        # Enter with author name
        AUTHOR=$2
        [ ${AUTHOR:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR AUTHOR
        shift
      ;;
      '-ai'|'--author_initials')
       # Enter with author name
       AUTHOR_INITIALS=$2
       [ ${AUTHOR_INITIALS:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR AUTHOR_INITIALS
       shift
      ;;
      '-cs'|'--const_string')
        # Enter with construction source name string
        CONST_STRING=$2
        [ ${CONST_STRING:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR CONST_STRING
        shift
      ;;
      '-md'|'--model')
        # Enter with SlackBuild model
        MODEL=$2
        [ ${MODEL:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR MODEL
        shift
      ;;
      '-j'|'--jobs')
        # Enter with SlackBuild model
        NUMJOBS=$2
        [ ${NUMJOBS:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR NUMJOBS
        ! is_number $NUMJOBS &&  handle_error $ERROR_NOT_NUMBER NUMJOBS
        NUMJOBS="-j$NUMJOBS"
        shift
      ;;
      '--prefix')
        # Enter with SlackBuild model
        PREFIX=$2
        [ ${PREFIX:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR PREFIX
        shift
      ;;
      '-pn'|'--pkg_name')
        # Enter with package name
        PKG_NAME=$2
        [ ${PKG_NAME:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR PKG_NAME
        shift
      ;;
      '-pv'|'pkg_version')
        # Enter with package version
        VERSION=$2
        [ ${VERSION:0:1} = "-" ] && handle_error $ERROR_MKBUILD_INPUT_PAR VERSION
        shift
      ;;
      '-sn'|'--src_name')
        # Enter with source name
        SRC_NAME=$2
        [ ${SRC_NAME:0:1} = '-' ] && handle_error $ERROR_MKBUILD_INPUT_PAR SRC_NAME
        shift
      ;;
      '-u'|'--url')
        # Enter with url address
        URL=$2
        [ ${URL:0:1} = '-' ] && handle_error $ERROR_MKBUILD_INPUT_PAR URL
        shift
      ;;
      '-pf'|'--patch-files')
        # Path files list
        PATCH_FILES=$2
        [ ${PATCH_FILES:0:1} = '-' ] && handle_error $ERROR_MKBUILD_INPUT_PAR PATCH_FILES
        shift
      ;;
      '-npss'|'--nps-strip')
        # Number of prefix slashes to strip
        NPS_STRIP=$2
        [ ${NPS_STRIP:0:1} = '-' ] && handle_error $ERROR_MKBUILD_INPUT_PAR NPS_STRIP
        shift
      ;;
      *)
        # mkbuild input file
        MKBUILD_NAME="${1//.mkbuild}.mkbuild"
      ;;
    esac
    shift
  done

  [ ! -e "$MKBUILD_NAME" -a "$ACTION" == "build" ] && ACTION='new'

}

function get_variable {

  # Get variable value from mkbuild file (MKBUILD_NAME)
  [ $# -ne 1 ] && handle_error $ERROR_PAR_NUMBER
  [ -z $MKBUILD_NAME ] && echo "Warning: no [mkbuild_file]." && return 0

  sed -n '1,/^#>>/ p' $MKBUILD_NAME | grep "^\[\[${1}\]\]" | cut -f2- -d= | sed -e 's/^"//' -e 's/"$//'

}

function edit_file {

  # Edit file  $3, by change string [[$1]] to $2
  [ $# -ne 3 ] && handle_error $ERROR_PAR_NUMBER

  eval "sed -i 's�\[\[$1\]\]�$2�g' $3"

}

function edit_file_full {

  # Edit file  $3, by change string $1 to $2
  [ $# -ne 3 ] && handle_error $ERROR_PAR_NUMBER

  eval "sed -i 's�$1$2�' $3"

}

function start_build  {

  # Build initial sections
  [ $# -ne 1 ] && handle_error $ERROR_PAR_NUMBER

  edit_file "SLACKBUILD AUTHOR" "$AUTHOR" $1
  edit_file "SLACKBUILD AUTHOR INITIALS" "$AUTHOR_INITIALS" $1
  edit_file "SOURCE NAME" "$SRC_NAME" $1
  edit_file "PROGRAM NAME" "$PKG_NAME" $1
  edit_file "PACKAGE NAME" "$PKG_NAME" $1
  edit_file "DECOMPRESSOR" "$DECOMPRESSOR" $1
  edit_file "DECOMPRESSOR TEST FLAG" "$DECOMPRESSOR_TEST_FLAG" $1
  edit_file "PROGRAM URL" "$URL" $1
  if [ "$ARCH" == "noarch" ]; then
    sed -i 's/^ARCH=.*$/ARCH="noarch"/' $1
  else
    edit_file "ARCH" "$ARCH" $1
  fi
  edit_file "NUMBER OF JOBS" "$NUMJOBS" $1
  edit_file "VERSION" "$VERSION" $1
  edit_file "SOURCE NAME CONSTRUCTION STRING" "$CONST_STRING" $1
  edit_file "EXTENSION" "$EXTENSION" $1
  edit_file "DOWNLOAD FOLDER URL" "$URL_BASE" $1
  edit_file "OTHER CONFIGURE ARGS" "$OPTIONS" $1
  edit_file "DOCUMENTATION FILES" "$DOCFILES" $1
  edit_file "PREFIX" "$PREFIX" $1
  edit_file "UNPACKER" "$UNPACKER" $1
  edit_file "UNPACKER FLAGS" "$UNPACKER_FLAGS" $1
  edit_file "BUILD NUMBER" "$BUILD_NUMBER" $1
  edit_file "PATCH FILES" "$PATCH_FILES" $1
  edit_file "NUMBER OF PREFIX SLASHES TO STRIP" "$NPS_STRIP" $1

  edit_file_full "\$EXTENSION" "$EXTENSION" $1

}

function clear_files {

  # Remove temporary files
  [ ! -z $AUX_TMP ] && rm $AUX_TMP 2>/dev/null
  [ ! -z $SLACKBUILD_TEMP ] && rm $SLACKBUILD_TEMP 2>/dev/null
  [ ! -z $DIFF_FILE ] && rm $DIFF_FILE 2>/dev/null
  chmod 755 *.SlackBuild 2>/dev/null

}

function set_status {

  # Set status section
  # $1 - Section
  # $2 - Status
  # $3 - file
  [ $# -ne 3 ] && handle_error $ERROR_PAR_NUMBER
  if [ "`get_status $1 $3`" != "all" ]; then
    [ $VERBOSE -eq $on ] && echo "Section $1 $2"
    eval "sed -i 's/^<$1>.*$/<$1> $2/' $3"
  else
    echo "Warning: Section $1 have status all. Can't change!"
  fi

}

function get_status {

  # Get status from section
  # $1 - Section
  # $2 - file
  [ $# -ne 2 ] && handle_error $ERROR_PAR_NUMBER
  eval "sed '/^<$1>.*$/! d' $2"

}

function get_mkbuild_status {

    # Get status from mkbuild file
    # $1 section
    eval "sed '/^#>>/,/<</ ! d; /^#/ d; /: *$1$/! d; s/^ *\(.*\):.*$/\1/' $MKBUILD_NAME"
}

function activate_sections {

  # Enable and desable sections
  ACTIONS_LIST=`sed '/^#>>/,/<</ ! d; /^#/ d ' $MKBUILD_NAME | tr -d ' '`
  for i in $ACTIONS_LIST; do
    STATUS=`echo $i | cut -f1 -d:`
    SECTION=`echo $i | cut -f2 -d:`
    set_status $SECTION $STATUS $SLACKBUILD_TEMP
  done

  #if [ $SVN_MOD -eq $on -o $URL == "" ]; then
  #  LIST_OFF="download_source md5sum_download_and_check_0 md5sum_download_and_check_1 gpg_signature_check untar_source"
  #  [ $VERBOSE -eq $on ] && echo -e "\nSubversion or empty URL changes:"
  #  for i in $LIST_OFF; do
  #    set_status $i "off" $SLACKBUILD_TEMP
  #  done
  #  [ $SVN_MOD -eq $on ] && set_status svn_source "on" $SLACKBUILD_TEMP
  #fi

}

function build_slackbuild {

  # Clean SlackBuild
  # Make SlackBuild backup
  [ -e $SLACKBUILD ] && mv $SLACKBUILD $SLACKBUILD.old
  # Remove off sections
  sed -i '/^<[a-z].*> off/, /^<\/[a-z].*>$/ d' $SLACKBUILD_TEMP
  # Remove sections names
  sed -i '/^<.*$/ d' $SLACKBUILD_TEMP
  # Remove clear lines
  # isto pode ser feito com "cat -s SLACKBUILD_TEMP ..."
  sed -i ':i ; $! N; s/\n/�/ ; t i ; s/�\{3,\}/��/g ; s/�/\n/g' $SLACKBUILD_TEMP
  # Remove from frist line do #!/... line
  sed '1,/^#\!/ {/^#\!/ b; d }' $SLACKBUILD_TEMP > $SLACKBUILD

}

function section_edit {

  # Edits a section substituting its content
  [ -z $MKBUILD_NAME ] && return 0

  SECTION_LIST=`grep '^#>[a-z]' $MKBUILD_NAME | cut -c3-`

  # Check for sections change
  [ -z "$SECTION_LIST" ] && return 0

  # Change sections
  for i in $SECTION_LIST; do
    [ $VERBOSE -eq $on ] && echo "Change section $i"
    if [ "$i" = "slackdesc" ]; then
      # Special slackdesc section
      slackdesc_edit > $AUX_TMP
      mv $AUX_TMP $SLACKBUILD_TEMP
    else
      # Others sections
      section_change $i
    fi
  done

}

function slackdesc_edit {

  # Edit slackdesc section
  sed -n '1,/|-----/ { /<slackdesc>/ b; /|-----/ b; p; }' $SLACKBUILD_TEMP
  echo -n $PKG_NAME | tr [a-z+\-] " "
  echo -n "|-----handy-ruler"
  let N=18+${#PKG_NAME}
  for i in `seq $N $SLACKDESC_LEN`; do
    echo -n "-"
  done
  echo -en "|\n"

  sed -n '/#>slackdesc/,/#<slackdesc/ { /^#/ b; p }' $MKBUILD_NAME
  sed '1, /\[\[SLACK-DESC\]\]/ d' $SLACKBUILD_TEMP

}

function section_change {

  # Change section lines
  [ $# -ne 1 ] && handle_error $ERROR_PAR_NUMBER

  # Copy first half
  eval "sed '1,/^<$1>/! d' $SLACKBUILD_TEMP > $AUX_TMP"
  # Paste new section
  eval "sed -n '/#>$1/,/#<$1/ { /^#>/ b; /^#</ b; p }' $MKBUILD_NAME >> $AUX_TMP"
  # Copy second halt
  eval "sed '/^<\/$1>/,$ ! d' $SLACKBUILD_TEMP >> $AUX_TMP"

  mv $AUX_TMP $SLACKBUILD_TEMP

}

function make_slack_required {

  # Build slack-required file
  [ -e slack-required ] && mv slack-required slack-required.old
  [ -z "$SLACK_REQUIRED" ] && return 0

  echo -e "# Dependency list to $SRC_NAME\n#\n# dependency [condition] [version]]" > slack-required

  echo $SLACK_REQUIRED | sed 's/:/\n/g' | while read i; do
    REQ=`echo $i | awk '{ print $1 }'`
    CON=`echo $i | awk '{ print $2 }'`
    VER=`echo $i | awk '{ print $3 }'`
    echo -e "$REQ\t\t$CON\t\t$VER" >> slack-required
  done

}

function change_others_parameters {

  # Change others parameters started by '[[' in .mkbuild file
  sed '1,/#>>/ ! d' $MKBUILD_NAME | grep -v '^#' | grep '^\[\[[A-Za-z]' | \
  while read i; do
    CHANGE="`echo $i | sed 's/\[\[\(.*\)\]\]=\"\(.*\)\"/\1/'`"
    VALUE="`echo $i | sed 's/\[\[\(.*\)\]\]=\"\(.*\)\"/\2/'`"
    edit_file "$CHANGE" "$VALUE" $SLACKBUILD
  done

}

function get_slackbuild_path {

  # Search for SlackBuild Path in order:
  #   - command line parameter;
  #   - mkbuild parameters file;
  #   - SlackBuild tree;
  #   - Gentool-portage internet tree;
  #   - default path (others/unclassified/$PKG_NAME).

  # Start AUX_PATH with command line parameter
  AUX_PATH="$SLACKBUILD_PATH"

  # Get in mkbuild
  [ -z $AUX_PATH ] && AUX_PATH=`validate_parameter "$SLACKBUILD_PATH" "SLACKBUILD PATH" ""`

  # SlackBuild path in SlackBuild tree
  [ -z $AUX_PATH ] && AUX_PATH=`cd $SLACKBUILDS_DIR && find . -name $SLACKBUILD | sed -e 's/^\.\///' | xargs dirname` 2>/dev/null

  # SlackBuild path default
  [ -z $AUX_PATH ] && AUX_PATH="others/unclassified/$PKG_NAME"

  # Down case SlackBuild path
  echo $AUX_PATH | tr [A-Z] [a-z]
}

function apply_mkpatch {

  # Apply mkpatch if exist
  sed -n '/#p>/,/#p</ { /^#/ b; p }' $MKBUILD_NAME > $DIFF_FILE
  if [ -s $DIFF_FILE ]; then
    mkpatch $DIFF_FILE $SLACKBUILD_TEMP > $AUX_TMP || handle_error $?
    [ ! -s $AUX_TMP ] && handle_error 1
    cp $AUX_TMP $SLACKBUILD_TEMP
    [ $VERBOSE -eq $on ] && ( echo -e "\nApply mkpath ..."; cat $DIFF_FILE )
  fi

}

# ----------------------------------------------------------------
# ------------------- svn functions ------------------------------
function submit_slackbuild {

  # Submit SlackBuild in local Slack.Sarava tree
  echo -e "\nSubmiting $SLACKBUILD"

  local candidate oldplace

  # check SlackBuilds directory
  [ ! -e $SLACKBUILDS_DIR ] && createpkg --sync

  # change to SlackBuilds directory
  cd $SLACKBUILDS_DIR/

  # Add SlackBuild scripts
  # check path
  [ ! -e $SLACKBUILD_PATH ] && svn_mkdir $SLACKBUILD_PATH

  # add SlackBuild
  svn_copy $WORK/$SLACKBUILD $SLACKBUILD_PATH

  # check slack-required
  # add slack-required
  [ -e $WORK/slack-required ] && svn_copy $WORK/slack-required $SLACKBUILD_PATH

  for i in `ls $WORK | egrep -v '(SlackBuild|old|slack-required|.mkbuild$|.tmp$)\*{0,1}$'`; do
    svn_copy $WORK/$i $SLACKBUILD_PATH
  done

  # remove stuff in old places
  for candidate in `find . -name $SLACKBUILD`; do
    oldplace="`dirname $candidate | sed -e 's/^\.\///'`"
    if [ "$oldplace" != "$SLACKBUILD_PATH" ]; then
      echo "Removing SlackBuild found at old path $oldplace"
      svn_del $oldplace
    fi
  done

  cd $WORK

}

function submit_mkbuild {

  # Submit mkbuild in local mkbuild Slack.Sarava tree
  echo -e "\nSubmiting $MKBUILD_NAME"

  local candidate oldplace

  # check mkbuild directory
  [ ! -d $MKBUILDS_DIR ] && build_repo $MKBUILDS_DIR $MKBUILDS_SVN

  # Get mkbuild path in parameter file
  MKBUILD_PATH=$SLACKBUILD_PATH

  # change to mkbuilds directory
  cd $MKBUILDS_DIR/

  # Add mkbuild scripts
  # check path
  [ ! -e $MKBUILD_PATH ] && svn_mkdir $MKBUILD_PATH

  # add mkbuild
  svn_copy $WORK/$MKBUILD_NAME $MKBUILD_PATH

  for i in `ls $WORK | egrep -v '(SlackBuild|old|slack-required|.mkbuild$|.tmp$)\*{0,1}$'`; do
    svn_copy $WORK/$i $MKBUILD_PATH
  done

  # remove stuff in old places
  for candidate in `find . -name $MKBUILD_NAME`; do
    oldplace="`dirname $candidate | sed -e 's/^\.\///'`"
    if [ "$oldplace" != "$MKBUILD_PATH" ]; then
      echo "Removing mkbuild found at old path $oldplace"
      svn_del $oldplace
    fi
  done  

  cd $WORK

}

# ----------------------------------------------------------------
# ------------------- general functions --------------------------

function validate_parameter {

  # Validate parameter in .mkbuild file
  [ $# -ne 3 ] && handle_error $ERROR_PAR_NUMBER

  if [ ! -z "$1" ]; then
    echo "$1"
  else
    local STRING="`get_variable "$2"`"
    if [ ! -z "$STRING" ]; then
      echo "$STRING"
    else
      echo "$3"
    fi
  fi

}

function decompress_find {

  # Find decompressor program and test flag
  case $EXTENSION in
    'gz'|'GZ')
      DECOMPRESSOR="gunzip"
      DECOMPRESSOR_TEST_FLAG="-t"
    ;;
    'bz2'|'BZ2')
      DECOMPRESSOR="bunzip2"
      DECOMPRESSOR_TEST_FLAG="-t"
    ;;
    'zip'|'ZIP')
      DECOMPRESSOR="unzip"
      DECOMPRESSOR_TEST_FLAG="-t"
    ;;
    *)
    handle_error $ERROR_MKBUILD_CONSTRUCTION "DECOMPRESSOR"
    ;;
  esac

}

function load_parameters {

  # Load Createpkg parameters
  SLACKBUILDS_DIR="`eval_parameter SLACKBUILDS_DIR /var/simplepkg/slackbuilds`"
  [ ! -d $SLACKBUILDS_DIR ] && mkdir -p $SLACKBUILDS_DIR

  MKBUILDS_DIR="`eval_parameter MKBUILDS_DIR /var/simplepkg/mkbuilds`"
  [ ! -d $SLACKBUILDS_DIR ] && mkdir -p $SLACKBUILDS_DIR

  SLACKBUILDS_SVN="`eval_parameter SLACKBUILDS_DIR http://slack.sarava.org/slackbuilds`"
  MKBUILDS_SVN="`eval_parameter MKBUILDS_SVN http://slack.sarava.org/mkbuilds`"
  SVN_USER="`eval_parameter MKBUILDS_SVN_USER`"
  SVN_GROUP="`eval_parameter MKBUILDS_SVN_GROUP`"

  COLOR_MODE="`eval_parameter COLOR_MODE none`"
  TMP="`eval_parameter TMP /tmp`"

}
# ----------------------------------------------------------------

#=============================
#        Main Program
#=============================
#-----------------------------
# Common functions
COMMON_SH="/usr/libexec/simplepkg/common.sh"
SIMPLEPKG_CONF="/etc/simplepkg/simplepkg.conf"
BASENAME="`basename $0`"
WORK=`pwd`
LANG=en_US
EXIT_CODE=0

if [ -f "$COMMON_SH" ]; then
  source $COMMON_SH
else
  echo $error "$BASENAME: file $COMMON_SH not found. Check your $BASENAME installation"
fi

# Start constants
set_constants

# Set submit off
SUBMIT_SLACKBUILD=$off
SUBMIT_MKBUILD=$off
# Set verbose off
VERBOSE=$off
SVN_MOD=$off

# Load /etc/simplepkg/simplepkg.conf parameters
load_parameters

# Select color mode: gray, color or none (*)
color_select $COLOR_MODE

# Auxiliar file
AUX_TMP=/tmp/mkbuild_tmp.$RANDOM
DIFF_FILE=/tmp/mkbuild.diff.$RANDOM
# Derectory to SlackBuild models
MODEL_DIR=${MODEL_DIR:="/etc/simplepkg/defaults/mkbuild"}
# SlackDesk line length
SLACKDESC_LEN=78

# Load error codes
error_codes

[ $# -eq 0 ] && mkbuild_use && exit 1

# Configure input parameters
set_parameters "$@"

[ $VERBOSE -eq $on ] && echo -e "$PROG_NAME version $PROG_VERSION\n"
if [ ! -z $MKBUILD_NAME ]; then
  case $ACTION in
    'new')
      # Create a new .mkbuild parameters-file
      cp $MODEL_DIR/model.mkbuild $MKBUILD_NAME
      # Package Author
      if [ ! -z "$AUTHOR" ]; then
        edit_file "YOUR NAME" "${AUTHOR}" $MKBUILD_NAME
        # Package Author Signature
        if [ -z "$AUTHOR_INITIALS" ]; then
          AUTHOR_INITIALS=`echo $AUTHOR | tr '[A-Z]' '[a-z]' | sed 's/ /\n/g' |  sed 's/^\([a-z]\).*/\1/' | sed ':i ; $! N ; s/\n// ; t i'`
        fi
        edit_file "YOUR SIGNATURE" "${AUTHOR_INITIALS}" $MKBUILD_NAME
      fi
      # Change Default SourceForge URL
      [ -z "$URL" ] && URL="http://downloads.sourceforge.net/[[PKG NAME]]/"
      edit_file "DEFAULT URL" "${URL}" $MKBUILD_NAME
      # Change Package Name
      edit_file "PKG NAME" "${MKBUILD_NAME//.mkbuild}" $MKBUILD_NAME

      # Print .mkbuild name
      echo "$MKBUILD_NAME"
    ;;
    'build')
      # Get values
      # Author name
      AUTHOR=${AUTHOR:="`get_variable "SLACKBUILD AUTHOR"`"}
      [ -z "$AUTHOR" ] && handle_error $ERROR_MKBUILD_CONSTRUCTION "SLACKBUILD AUTHOR"
      [ $VERBOSE -eq $on ] && echo "[[SLACKBUILD AUTHOR]]=\"$AUTHOR\""

      # Author initials
      STR_MOUNT=`echo $AUTHOR | sed 's/ /\n/g' | sed 's/^\([A-Z]\).*/\1/' | sed ':i; $!N;  s/\n//; ti' | tr [A-Z] [a-z]`
      AUTHOR_INITIALS="`validate_parameter "$AUTHOR_INITIALS" "SLACKBUILD AUTHOR INITIALS" "$STR_MOUNT"`" || handle_error $ERROR_MKBUILD_CONSTRUCTION "SLACKBUILD AUTHOR INITIALS"
      [ $VERBOSE -eq $on ] && echo "[[SLACKBUILD AUTHOR INITIALS]]=\"$AUTHOR_INITIALS\""

      # URL program
      URL=`validate_parameter "$URL" "DOWNLOAD FOLDER URL" ""` || handle_error $ERROR_MKBUILD_CONSTRUCTION "URL"
      [ $VERBOSE -eq $on ] && echo "[[URL]]=\"$URL\""

      AUX=`get_mkbuild_status "svn_source"`
      SVN_MOD=`convert_boolean "$AUX"`

      AUX=`get_mkbuild_status "git_source"`
      GIT_MOD=`convert_boolean "$AUX"`

      # Check sections
      if [ $SVN_MOD -eq $on -o $GIT_MOD -eq $on -o $URL == "" ]; then
        LIST_OFF="download_source md5sum_download_and_check_0 md5sum_download_and_check_1 gpg_signature_check untar_source"
        [ $VERBOSE -eq $on ] && echo -e "\nCheck subversion or empty URL"
        for i in $LIST_OFF; do
          if [ `get_mkbuild_status "$i"` != "off" ]; then
            handle_error $ERROR_MKBUILD_VCS
          else
            [ $VERBOSE -eq $on ] && echo -e "off: $i is ok."
          fi
        done
      fi

      STR_MOUNT="`echo $URL | sed 's/.*\.\([a-z0-9]\+\)$/\1/'`"
      if [ $STR_MOUNT = "gz" -o $STR_MOUNT = "tgz" -o $STR_MOUNT = "bz2" -o $STR_MOUNT = "zip" ]; then
        SOURCE_NAME=`basename $URL`
        URL_BASE=`dirname $URL`
      else
        URL_BASE=$URL
      fi
      [ $VERBOSE -eq $on ] && echo "[[DOWNLOAD FOLDER URL]]=\"$URL_BASE\""

      if [ $SVN_MOD -eq $off ] && [ $GIT_MOD -eq $off ]; then
        # Extension
        EXTENSION=`validate_parameter "$EXTENSION" "EXTENSION" "$STR_MOUNT"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "EXTENSION"
         [ $VERBOSE -eq $on ] && echo "[[EXTENSION]]=\"$EXTENSION\""

          # Unpacker
        UNPACKER=`validate_parameter "$UNPACKER" "UNPACKER" "tar"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "UNPACKER"
          [ $VERBOSE -eq $on ] && echo "[[UNPACKER]]=\"$UNPACKER\""

          # Unpacker flags
        [ "$UNPACKER" == "tar" ] && STR_MOUNT="--no-same-owner --no-same-permissions -xvf" || STR_MOUNT=""
        UNPACKER_FLAGS=`validate_parameter "$UNPACKER_FLAGS" "UNPACKER FLAGS" "$STR_MOUNT"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "UNPACKER FLAGS"
        [ $VERBOSE -eq $on ] && echo "[[UNPACKER_FLAGS]]=\"$UNPACKER_FLAGS\""

        # Decompressor program and test flag
        DECOMPRESSOR=`validate_parameter "$DECOMPRESSOR" "DECOMPRESSOR" ""`
        [ -z $DECOMPRESSOR ] && decompress_find
        [ $VERBOSE -eq $on ] && echo "[[DECOMPRESSOR]]=\"$DECOMPRESSOR\""

        DECOMPRESSOR_TEST_FLAG=`validate_parameter "$DECOMPRESSOR_TEST_FLAG" "DECOMPRESSOR TEST FLAG" ""` || handle_error $ERROR_MKBUILD_CONSTRUCTION "DECOMPRESSOR TEST FLAG"
        [ $VERBOSE -eq $on ] && echo "[[DECOMPRESSOR TEST FLAG]]=\"$DECOMPRESSOR_TEST_FLAG\""
      fi

      BUILD_NUMBER=`validate_parameter "$BUILD_NUMBER" "BUILD NUMBER" "1"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "BUILD NUMBER"
      # Build archteture

      ARCH=`validate_parameter "$ARCH" "ARCH" "i486"`
      [ $VERBOSE -eq $on ] && echo "[[ARCH]]=\"$ARCH\""

      # Source name
      STR_MOUNT=`echo $SOURCE_NAME | sed -r 's/(.*)-(.*)\.(.*\..*)$/\1/'`
      SRC_NAME=`validate_parameter "$SRC_NAME" "SOURCE NAME" "$STR_MOUNT"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "SOURCE NAME"
      [ $VERBOSE -eq $on ] && echo "[[SOURCE NAME]]=\"$SRC_NAME\""

      # Package name
      STR_MOUNT=$SRC_NAME
      PKG_NAME=`validate_parameter "$PKG_NAME" "PACKAGE NAME" "$STR_MOUNT"`
      [ $VERBOSE -eq $on ] && echo "[[PACKAGE NAME]]=\"$PKG_NAME\""

      # Version
      STR_MOUNT=`echo $SOURCE_NAME | sed -r 's/(.*)-(.*)\.(.*\..*)$/\2/'`
      VERSION=`validate_parameter "$VERSION" "VERSION" "$STR_MOUNT"` || handle_error $ERROR_MKBUILD_CONSTRUCTION "VERSION"
      [ $VERBOSE -eq $on ] && echo "[[VERSION]]=\"$VERSION\""

      # Construction source name string
      CONST_STRING="`validate_parameter "$CONST_STRING" "SOURCE NAME CONSTRUCTION STRING" "\\\$SRC_NAME-\\\$VERSION.tar.$EXTENSION"`"
      [ $VERBOSE -eq $on ] && echo "[[SOURCE NAME CONSTRUCTION STRING]]=\"$CONST_STRING\""

      # Build Source Name
      [ -z $SOURCE_NAME ] && SOURCE_NAME=`eval "echo $CONST_STRING"`
      [ $VERBOSE -eq $on ] && echo "SOURCE_NAME=\"$SOURCE_NAME\""

      # Documentations list
      DOCFILES=`validate_parameter "$DOCFILES" "DOCUMENTATION FILES" "NEWS TODO README AUTHORS INSTALL ChangeLog MAINTAINERS COPYING LICENSE SIGNATURE readme.*"`
      [ $VERBOSE -eq $on ] && echo "[[DOCUMENTATION FILES]]=\"$DOCFILES\""

      # ./configure option
      OPTIONS=`validate_parameter "$OPTIONS" "OTHER CONFIGURE ARGS" ""`
      [ $VERBOSE -eq $on ] && echo "[[OTHER CONFIGURE ARGS]]=\"$OPTIONS\""

      # PREFIX
      PREFIX=`validate_parameter "$PREFIX" "PREFIX" "/usr"`
      [ $VERBOSE -eq $on ] && echo "[[PREFIX]]=\"$PREFIX\""

      # Number of jobs
      NUMJOBS=`validate_parameter "$NUMJOBS" "NUMBER OF JOBS" ""`
      is_number $NUMJOBS && NUMJOBS="-j${NUMJOBS}"
      [ $VERBOSE -eq $on ] && echo "[[NUMBER OF JOBS]]=\"$NUMJOBS\""

      # Make slack-required file
      SLACK_REQUIRED=`validate_parameter "$SLACK_REQUIRED" "SLACK REQUIRED" ""`
      [ $VERBOSE -eq $on ] && echo "[[SLACK REQUIRED]]=\"$SLACK_REQUIRED\""

      # SlackBuild model
      MODEL=`validate_parameter "$MODEL" "SLACKBUILD MODEL" "generic.mkSlackBuild"`
      [ $VERBOSE -eq $on ] && echo "[[SLACKBUILD MODEL]]=\"$MODEL\""

      # PATCH FILES
      PATCH_FILES=`validate_parameter "$PATCH_FILES" "PATCH FILES" ""`
      [ $VERBOSE -eq $on ] && echo "[[PATCH_FILES]]=\"$PATCH_FILES\""

      # Strip  the smallest prefix containing num leading slashes from each file name found in the patch file.
      NPS_STRIP=`validate_parameter "$NPS_STRIP" "NUMBER OF PREFIX SLASHES TO STRIP" "1"`
      [ $VERBOSE -eq $on ] && echo "[[NUMBER OF PREFIX SLASHES TO STRIP]]=\"$NPS_STRIP\""

      # SlackBuild path
      # SlackBuild path in mkbuild parameters file
      SLACKBUILD=${PKG_NAME}.SlackBuild
      SLACKBUILD_PATH=`get_slackbuild_path`
      [ $VERBOSE -eq $on ] && echo "[[SLACKBUILD PATH]]=\"$SLACKBUILD_PATH\""

      #--------------------------------------------------------------
      #-                   Start build SlackBuild                   -
      #--------------------------------------------------------------
      [ $VERBOSE -eq $on ] && echo -e "\nStart SlackBuild make"
      SLACKBUILD_TEMP=$SLACKBUILD.tmp
      cp $MODEL_DIR/$MODEL $SLACKBUILD_TEMP

      # Apply mkpatch
      [ $VERBOSE -eq $on ] && echo -e "\nMkpatch section ..."
      apply_mkpatch

      # On/Off sections
      [ $VERBOSE -eq $on ] && echo -e "\nEnable/desable sections ..."
      activate_sections

      # Change sections
      [ $VERBOSE -eq $on ] && echo -e "\nEdit sections ..."
      section_edit

      # Change strings from model
      [ $VERBOSE -eq $on ] && echo -e "\nChange strings in $PACKAGE.SlackBuild model..."
      start_build $SLACKBUILD_TEMP

      # Remove off sections
      [ $VERBOSE -eq $on ] && echo -e "\nRemove off sections ..."
      build_slackbuild

      # Make slack-required file
      [ $VERBOSE -eq $on ] && echo -e "\nMake slack-required file ..."
      make_slack_required

      if [ -e slack-required ]; then
        DEPENDENCY_LIST="`cat slack-required | awk '{print $1}' | grep '^[a-z]' | tr '\012' ' '`"
        edit_file "REQUIRES" "$DEPENDENCY_LIST" $SLACKBUILD
      else
        edit_file "REQUIRES" " " $SLACKBUILD
      fi

      # Others changes
      [ $VERBOSE -eq $on ] && echo -e "\nEdit others [[]] parameters ..."
      change_others_parameters

      # Commit SlackBuild
      [ $SUBMIT_SLACKBUILD -eq $on ] && submit_slackbuild

      # Commit mkbuild
      [ $SUBMIT_MKBUILD -eq $on ] && submit_mkbuild
    ;;
    'search')
      SEARCH=`find $MKBUILDS_DIR -iname $MKBUILD_NAME`
      if [ "$SEARCH" == "" ]; then
        EXIT_CODE=1
      else
        EXIT_CODE=0
        echo $SEARCH
      fi
    ;;
  esac
fi

# Clear temporary files
[ $VERBOSE -eq $on ] && echo -e "\nRemove temporary files ..."
clear_files
exit $EXIT_CODE