aboutsummaryrefslogtreecommitdiffstats
path: root/kerl
blob: f2549eeddb237155cc20f1545b21c05fa7ec29bd (plain) (blame)
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
#! /bin/sh

# Copyright (c) 2011 Spawngrid, Inc
# Copyright (c) 2011 Evax Software <contact(at)evax(dot)org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

ERLANG_DOWNLOAD_URL=http://www.erlang.org/download

KERL_BASE_DIR="$HOME/.kerl"
KERL_CONFIG="$HOME/.kerlrc"
KERL_DOWNLOAD_DIR="$KERL_BASE_DIR/archives"
KERL_BUILD_DIR="$KERL_BASE_DIR/builds"
if [ -n "$KERL_CONFIGURE_OPTIONS" ]; then
    _KCO="$KERL_CONFIGURE_OPTIONS"
fi
if [ -n "$KERL_SASL_STARTUP" ]; then
    _KSS="$KERL_SASL_STARTUP"
fi
if [ -n "$KERL_AGNER_AUTOINSTALL" ]; then
    _KAA="$KERL_AGNER_AUTOINSTALL"
fi
if [ -n "$KERL_DEPLOY_SSH_OPTIONS" ]; then
    _KDSSH="$KERL_DEPLOY_SSH_OPTIONS"
fi
if [ -n "$KERL_DEPLOY_RSYNC_OPTIONS" ]; then
    _KDRSYNC="$KERL_DEPLOY_RSYNC_OPTIONS"
fi
KERL_CONFIGURE_OPTIONS=
KERL_DISABLE_AGNER=
KERL_SASL_STARTUP=
KERL_INSTALL_MANPAGES=

# ensure the base dir exsists
mkdir -p "$KERL_BASE_DIR"

# source the config file if available
if [ -f "$KERL_CONFIG" ]; then . "$KERL_CONFIG"; fi

if [ -n "$_KCO" ]; then
    KERL_CONFIGURE_OPTIONS="$_KCO"
fi
if [ -n "$_KSS" ]; then
    KERL_SASL_STARTUP="$_KSS"
fi
if [ -n "$_KAA" ]; then
    KERL_AGNER_AUTOINSTALL="$_KAA"
fi
if [ -n "$_KDSSH" ]; then
    KERL_DEPLOY_SSH_OPTIONS="$_KDSSH"
fi
if [ -n "$_KDRSYNC" ]; then
    KERL_DEPLOY_RSYNC_OPTIONS="$_KDRSYNC"
fi

if [ -z "$KERL_SASL_STARTUP" ]; then
    INSTALL_OPT=-minimal
else
    INSTALL_OPT=-sasl
fi

KERL_SYSTEM=`uname -s`
case "$KERL_SYSTEM" in
    Darwin|OpenBSD)
        MD5SUM="openssl md5"
        MD5SUM_FIELD=2
        SED_OPT=-E
        ;;
    *)
        MD5SUM=md5sum
        MD5SUM_FIELD=1
        SED_OPT=-r
        ;;
esac

usage()
{
    echo "kerl: build and install Erlang/OTP"
    echo "usage: $0 <command> [options ...]"
    echo "\n  <command>       Command to be executed\n"
    echo "Valid commands are:"
    echo "  build    Build specified release or git repository"
    echo "  install  Install the specified release at the given location"
    echo "  deploy   Deploy the specified installation to the given host and location"
    echo "  update   Update agner or the list of available releases from erlang.org"
    echo "  list     List releases, builds and installations"
    echo "  delete   Delete builds and installations"
    echo "  active   Print the path of the active installation"
    echo "  status   Print available builds and installations"
    echo "  prompt   Print a string suitable for insertion in prompt"
    exit 1
}

if [ $# -eq 0 ]; then usage; fi

get_releases()
{
    curl -s $ERLANG_DOWNLOAD_URL/ | \
        sed $SED_OPT -e 's/^.*>otp_src_(R1[-1234567890ABCD]+)\.tar\.gz<.*$/\1/' \
                     -e '/^R/!d'
}

update_checksum_file()
{
    echo "Getting the checksum file from erlang.org..."
    curl $ERLANG_DOWNLOAD_URL/MD5 > "$KERL_DOWNLOAD_DIR/MD5" || exit 1
}

ensure_checksum_file()
{
    if [ ! -f "$KERL_DOWNLOAD_DIR/MD5" ]; then
        update_checksum_file
    fi
}

check_releases()
{
    if [ ! -f "$KERL_BASE_DIR/otp_releases" ]; then
        echo "Getting the available releases from erlang.org..."
        get_releases > "$KERL_BASE_DIR/otp_releases"
    fi
}

# c.f. agner issue #98
# https://github.com/agner/agner/issues/#issue/98
KERL_NO_AGNER_SUPPORT="R10B-0 R10B-2 R10B-3 R10B-4 R10B-5 R10B-6 R10B-7
R10B-8 R10B-9 R11B-0 R11B-1 R11B-2 R11B-3 R11B-4 R11B-5 R12B-0 R12B-1
R12B-2 R12B-3 R12B-4 R12B-5 R13A R13B R13B01 R13B02 R13B03 R13B04"

KERL_NO_LION_SUPPORT="R10B-0 R10B-2 R10B-3 R10B-4 R10B-5 R10B-6 R10B-7
R10B-8 R10B-9 R11B-0 R11B-1 R11B-2 R11B-3 R11B-4 R11B-5 R12B-0 R12B-1
R12B-2 R12B-3 R12B-4 R12B-5 R13A R13B R13B01 R13B02 R13B03 R13B04 R14A R14B R14B01 R14B02 R14B03"

agner_support()
{
    if [ -n "$KERL_DISABLE_AGNER" ]; then
        return 1;
    fi

    for v in $KERL_NO_AGNER_SUPPORT; do
        if [ "$v" = "$1" ]; then
            return 1
        fi
    done

    return 0
}

lion_support() {
    for v in $KERL_NO_LION_SUPPORT; do
        if [ "$v" = "$1" ]; then
           return 1
        fi
    done
    return 0
}

is_valid_release()
{
    check_releases
    for rel in `cat $KERL_BASE_DIR/otp_releases`; do
        if [ "$1" = "$rel" ]; then
            return 0
        fi
    done
    return 1
}

assert_valid_release()
{
    if ! is_valid_release $1; then
        echo "$1 is not a valid Erlang/OTP release"
        exit 1
    fi
    return 0
}

get_release_from_name()
{
    if [ -f "$KERL_BASE_DIR/otp_builds" ]; then
        for l in `cat "$KERL_BASE_DIR/otp_builds"`; do
            rel=`echo $l | cut -d "," -f 1`
            name=`echo $l | cut -d "," -f 2`
            if [ "$name" = "$1" ]; then
                echo "$rel"
                return 0
            fi
        done
    fi
    return 1
}

is_valid_installation()
{
    if [ -f "$1/activate" ]; then
        return 0
    fi
    return 1
}

assert_valid_installation()
{
    if ! is_valid_installation $1; then
        echo "$1 is not a kerl-managed Erlang/OTP installation"
        exit 1
    fi
    return 0
}

do_update_agner()
{
    rel=`get_release_from_name $1`
    if [ "$?" -eq 1 ]; then
        echo "Unknown build name $1"
        exit 1
    fi
    TARGET="$KERL_BUILD_DIR/$1/release_$rel"
    cd "$KERL_BUILD_DIR/$1/agner_$rel" && \
        git pull &&
        PATH="$KERL_BUILD_DIR/$1/otp_src_$rel/bin:$PATH" \
            make > /dev/null 2>&1
    if [ "$?" -eq 1 ]; then
        return 1
    fi
    return 0
}

assert_build_name_unused()
{
    if [ -f "$KERL_BASE_DIR/otp_builds" ]; then
        for l in `cat "$KERL_BASE_DIR/otp_builds"`; do
            name=`echo $l | cut -d "," -f 2`
            if [ "$name" = "$1" ]; then
                echo "There's already a build named $1"
                exit 1
            fi
        done
    fi
}

do_git_build()
{
    assert_build_name_unused $3
    mkdir -p "$KERL_BUILD_DIR/$3"
    cd "$KERL_BUILD_DIR/$3"
    echo "Checking Erlang/OTP git repository from $1..."
    git clone $1 otp_src_git > /dev/null 2>&1
    if [ "$?" -eq 1 ]; then
        echo "Error retriving git repository"
        exit 1
    fi
    if [ ! -x otp_src_git/otp_build ]; then
        echo "Not a valid Erlang/OTP repository"
        rm -Rf "$KERL_BUILD_DIR/$3"
        exit 1
    fi
    cd otp_src_git
    git branch -a | grep "$2" > /dev/null 2>&1
    if [ "$?" -eq 1 ]; then
        git checkout $2 > /dev/null 2>&1
    else
        git checkout -b $2 origin/$2 > /dev/null 2>&1
    fi
    if [ "$?" -eq 1 ]; then
        echo "Couldn't checkout specified version"
        rm -Rf "$KERL_BUILD_DIR/$3"
        exit 1
    fi
    LOGFILE="$KERL_BUILD_DIR/$3/opt_build.log"
    echo "Building Erlang/OTP $3 from git, please wait..."
    ./otp_build setup -a $KERL_CONFIGURE_OPTIONS > "$LOGFILE" 2>&1
    if [ "$?" -eq 1 ]; then
        echo "Build error, see $LOGFILE"
        exit 1
    fi
    rm -f "$LOGFILE"
    ./otp_build release -a "$KERL_BUILD_DIR/$3/release_git" > /dev/null 2>&1
    cd "$KERL_BUILD_DIR/$3/release_git"
    ./Install $INSTALL_OPT "$KERL_BUILD_DIR/$3/release_git" > /dev/null 2>&1
    echo "Erlang/OTP $3 from git has been successfully built"
    list_add builds "git,$3"
    if [ -z "$KERL_DISABLE_AGNER" ]; then
        echo "Fetching and building agner..."
        cd "$KERL_BUILD_DIR/$3" && \
        git clone https://github.com/agner/agner.git agner_git > /dev/null 2>&1 && \
            cd agner_git && \
            PATH="$KERL_BUILD_DIR/$3/otp_src_git/bin:$PATH" make > /dev/null 2>&1 && \
        if [ "$?" -eq 1 ]; then
            echo "Agner install failed"; exit 1
        fi
        echo "Agner has been successfully built"
    fi
}

do_build()
{
    case "$KERL_SYSTEM" in
        Darwin)
            if [ `gcc --version | grep llvm | wc -l` = "1" ]; then
                if lion_support $1; then
                    KERL_CONFIGURE_OPTIONS="CFLAGS=-O0 $KERL_CONFIGURE_OPTIONS"
                else
                    if [ -x "`which gcc-4.2`" ]; then
                        KERL_CONFIGURE_OPTIONS="CC=gcc-4.2 $KERL_CONFIGURE_OPTIONS"
                    else
                        KERL_CONFIGURE_OPTIONS="CC=llvm-gcc-4.2 CFLAGS=-O0 $KERL_CONFIGURE_OPTIONS"
                    fi
                fi
            fi
        ;;
        *)
        ;;
    esac

    assert_valid_release $1
    assert_build_name_unused $2

    FILENAME=otp_src_$1.tar.gz
    download "$FILENAME"
    mkdir -p "$KERL_BUILD_DIR/$2"
    if [ ! -d "$KERL_BUILD_DIR/$2/otp_src_$1" ]; then
        echo "Extracting source code"
        cd "$KERL_BUILD_DIR/$2" && tar xfz "$KERL_DOWNLOAD_DIR/$FILENAME"
    fi
    echo "Building Erlang/OTP $1 ($2), please wait..."
    ERL_TOP="$KERL_BUILD_DIR/$2/otp_src_$1"
    cd "$ERL_TOP"
    LOGFILE="$KERL_BUILD_DIR/$2/otp_build_$1.log"
    if [ -n "$KERL_USE_AUTOCONF" ]; then
        ./otp_build setup -a $KERL_CONFIGURE_OPTIONS > "$LOGFILE" 2>&1
    else
        ./otp_build configure $KERL_CONFIGURE_OPTIONS > "$LOGFILE" 2>&1 && \
            ./otp_build boot -a > "$LOGFILE" 2>&1
    fi
    if [ "$?" -eq 1 ]; then
        echo "Build failed, see $LOGFILE"
        list_remove builds "$1 $2"
        exit 1
    fi
    rm -f "$LOGFILE"
    ERL_TOP="$ERL_TOP" ./otp_build release -a "$KERL_BUILD_DIR/$2/release_$1" > /dev/null 2>&1
    cd "$KERL_BUILD_DIR/$2/release_$1"
    ./Install $INSTALL_OPT "$KERL_BUILD_DIR/$2/release_$1" > /dev/null 2>&1
    echo "Erlang/OTP $1 ($2) has been successfully built"
    list_add builds "$1,$2"
    if agner_support $1; then
        echo "Fetching and building agner..."
        cd "$KERL_BUILD_DIR/$2" && \
        git clone https://github.com/agner/agner.git agner_$1 > /dev/null 2>&1 && \
            cd agner_$1 && \
            PATH="$KERL_BUILD_DIR/$2/otp_src_$1/bin:$PATH" make > /dev/null 2>&1 && \
        if [ "$?" -eq 1 ]; then
            echo "Agner install failed"; exit 1
        fi
        echo "Agner has been successfully built"
    fi
}

do_install()
{
    rel=`get_release_from_name $1`
    if [ "$?" -eq 1 ]; then
        echo "No build named $1"
        exit 1
    fi
    mkdir -p "$2"
    if [ ! -d "$2" ]; then
        echo "Destination is not a directory"
        exit 1
    fi
    absdir=`cd "$2" && pwd`
    echo "Installing Erlang/OTP $rel ($1) in $absdir..."
    ERL_TOP="$KERL_BUILD_DIR/$1/otp_src_$rel"
    cd "$ERL_TOP"
    ERL_TOP="$ERL_TOP" ./otp_build release -a "$absdir" > /dev/null 2>&1 &&
        cd "$absdir" && ./Install $INSTALL_OPT "$absdir" > /dev/null 2>&1
    if [ $? -eq 1 ]; then
        echo "Couldn't install Erlang/OTP $rel ($1) in $absdir"
        exit 1
    fi
    list_add installations "$1 $absdir";
    cat <<ACTIVATE > "$absdir/activate"
# credits to virtualenv
kerl_deactivate()
{
    if [ -n "\$_KERL_SAVED_PATH" ]; then
        PATH="\$_KERL_SAVED_PATH"
        export PATH
        unset _KERL_SAVED_PATH
    fi
    if [ -n "\$_KERL_SAVED_AGNER_BIN" ]; then
        AGNER_BIN="\$_KERL_SAVED_AGNER_BIN"
        export AGNER_BIN
        unset _KERL_SAVED_AGNER_BIN
    fi
    if [ -n "\$_KERL_SAVED_AGNER_EXACT_PREFIX" ]; then
        AGNER_EXACT_PREFIX="\$_KERL_SAVED_AGNER_EXACT_PREFIX"
        export AGNER_EXACT_PREFIX
        unset _KERL_SAVED_AGNER_EXACT_PREFIX
    fi
    if [ -n "\$_KERL_SAVED_REBAR_PLT_DIR" ]; then
        REBAR_PLT_DIR="\$_KERL_SAVED_REBAR_PLT_DIR"
        export REBAR_PLT_DIR
        unset _KERL_SAVED_REBAR_PLT_DIR
    fi
    if [ -n "\$BASH" -o -n "\$ZSH_VERSION" ]; then
        hash -r
    fi
    if [ ! "\$1" = "nondestructive" ]; then
        unset -f kerl_deactivate
    fi
}
kerl_deactivate nondestructive
_KERL_SAVED_PATH="\$PATH"
export _KERL_SAVED_PATH
_KERL_SAVED_AGNER_BIN="\$AGNER_BIN"
export _KERL_SAVED_AGNER_BIN
_KERL_SAVED_AGNER_EXACT_PREFIX="\$AGNER_EXACT_PREFIX"
export _KERL_SAVED_AGNER_EXACT_PREFIX
_KERL_SAVED_REBAR_PLT_DIR="\$REBAR_PLT_DIR"
export _KERL_SAVED_REBAR_PLT_DIR
PATH="$absdir/bin:\$PATH"
export PATH
AGNER_BIN="$absdir/bin"
export AGNER_BIN
AGNER_EXACT_PREFIX="$absdir/lib"
export AGNER_EXACT_PREFIX
REBAR_PLT_DIR="$absdir"
export REBAR_PLT_DIR
if [ -n "\$BASH" -o -n "\$ZSH_VERSION" ]; then
    hash -r
fi
ACTIVATE
    if agner_support $1; then
        echo "Installing agner in $absdir..."
        cp "$KERL_BUILD_DIR/$1/agner_$rel/agner" "$absdir/bin/"
        if [ -n "$KERL_INSTALL_AGNERIZED_REBAR" ]; then
            echo "Installing agnerized rebar in $absdir..."
            cp "$KERL_BUILD_DIR/$1/agner_$rel/agner" "$absdir/bin/rebar"
        fi
        if [ -n "$KERL_AGNER_AUTOINSTALL" ]; then
            for i in $KERL_AGNER_AUTOINSTALL; do
                echo "Autoinstalling $i"
                agner install $i > /dev/null 2>&1
            done
        fi
    fi
    if [ "$rel" != "git" -a -n "$KERL_INSTALL_MANPAGES" ]; then
        echo "Fetching and installing manpages..."
        FILENAME=otp_doc_man_$rel.tar.gz
        download "$FILENAME"
        echo "Extracting manpages"
        cd "$absdir" && tar xfz "$KERL_DOWNLOAD_DIR/$FILENAME"
    fi

    if [ "$rel" != "git" -a -n "$KERL_INSTALL_HTMLDOCS" ]; then
        echo "Fetching and installing HTML docs..."
        FILENAME="otp_doc_html_$rel.tar.gz"
        download "$FILENAME"
        echo "Extracting HTML docs"
        (cd "$absdir" && mkdir -p html && \
         tar -C "$absdir/html" -xzf "$KERL_DOWNLOAD_DIR/$FILENAME")
    fi

    echo "You can activate this installation running the following command:"
    echo ". $absdir/activate"
    echo "Later on, you can leave the installation typing:"
    echo "kerl_deactivate"
}

do_deploy()
{
    if [ -z "$1" ]; then
        echo "No host given"
        exit 1
    fi
    host=$1

    assert_valid_installation "$2"
    rel=`get_name_from_install_path "$2"`
    path=$2
    remotepath=$path

    if [ ! -z "$3" ]; then
        remotepath=$3
    fi

    ssh $KERL_DEPLOY_SSH_OPTIONS $host true > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Couldn't ssh to $host"
        exit 1
    fi

    echo "Cloning Erlang/OTP $rel ($path) to $host ($remotepath) ..."

    rsync -aqz -e "ssh $KERL_DEPLOY_SSH_OPTIONS" $KERL_DEPLOY_RSYNC_OPTIONS "$path/" "$host:$remotepath/"
    if [ $? -ne 0 ]; then
        echo "Couldn't rsync Erlang/OTP $rel ($path) to $host ($remotepath)"
        exit 1
    fi

    ssh $KERL_DEPLOY_SSH_OPTIONS $host "cd \"$remotepath\" && env ERL_TOP=\`pwd\` ./Install $INSTALL_OPT \`pwd\` > /dev/null 2>&1"
    if [ $? -ne 0 ]; then
        echo "Couldn't install Erlang/OTP $rel to $host ($remotepath)"
        exit 1
    fi

    ssh $KERL_DEPLOY_SSH_OPTIONS $host "cd \"$remotepath\" && sed -i -e \"s#$path#\`pwd\`#g\" activate"
    if [ $? -ne 0 ]; then
        echo "Couldn't completely install Erlang/OTP $rel to $host ($remotepath)"
        exit 1
    fi

    echo "On $host, you can activate this installation running the following command:"
    echo ". $remotepath/activate"
    echo "Later on, you can leave the installation typing:"
    echo "kerl_deactivate"
}

list_print()
{
    if [ -f $KERL_BASE_DIR/otp_$1 ]; then
        if [ "`cat "$KERL_BASE_DIR/otp_$1" | wc -l`" != "0" ]; then
            if [ -z "$2" ]; then
                cat "$KERL_BASE_DIR/otp_$1"
            else
                echo `cat "$KERL_BASE_DIR/otp_$1"`
            fi
            return 0
        fi
    fi
    echo "There are no $1 available"
}

list_add()
{
    if [ -f "$KERL_BASE_DIR/otp_$1" ]; then
        for l in `cat "$KERL_BASE_DIR/otp_$1"`; do
            if [ "$l" = "$2" ]; then
                return 1
            fi
        done
        echo "$2" >> "$KERL_BASE_DIR/otp_$1"
    else
        echo "$2" > "$KERL_BASE_DIR/otp_$1"
    fi
}

list_remove()
{
    if [ -f "$KERL_BASE_DIR/otp_$1" ]; then
        sed $SED_OPT -i -e "/^.*$2$/d" "$KERL_BASE_DIR/otp_$1"
    fi
}

list_has()
{
    if [ -f "$KERL_BASE_DIR/otp_$1" ]; then
        grep $2 "$KERL_BASE_DIR/otp_$1" > /dev/null 2>&1 && return 0
    fi
    return 1
}

list_usage()
{
    echo "usage: $0 list <releases|builds|installations>"
}

delete_usage()
{
    echo "usage: $0 delete <build|installation> <build_name or path>"
}

update_usage()
{
    echo "usage: $0 $1 <releases|agner>"
}

get_active_path()
{
    if [ -n "$_KERL_SAVED_PATH" ]; then
        echo $PATH | cut -d ":" -f 1 | sed 's/\(.*\)..../\1/'
    fi
    return 0
}

get_name_from_install_path()
{
    if [ -f "$KERL_BASE_DIR/otp_installations" ]; then
        grep -F "$1" "$KERL_BASE_DIR/otp_installations" | cut -d ' ' -f 1
    fi
    return 0
}

do_active()
{
    ACTIVE_PATH=`get_active_path`
    if [ -n "$ACTIVE_PATH" ]; then
        echo "The current active installation is:"
        echo $ACTIVE_PATH
        return 0
    else
        echo "No Erlang/OTP kerl installation is currently active"
        return 1
    fi
}

download()
{
    if [ ! -f "$KERL_DOWNLOAD_DIR/$1" ]; then
        echo "Downloading $1 to $KERL_DOWNLOAD_DIR"
        mkdir -p "$KERL_DOWNLOAD_DIR"
        curl "$ERLANG_DOWNLOAD_URL/$1" > "$KERL_DOWNLOAD_DIR/$1"
        update_checksum_file
    fi
    ensure_checksum_file
    echo "Verifying archive checksum..."
    SUM=`$MD5SUM "$KERL_DOWNLOAD_DIR/$1" | cut -d " " -f $MD5SUM_FIELD`
    ORIG_SUM=`grep -F "$1" "$KERL_DOWNLOAD_DIR/MD5" | cut -d " " -f 2`
    if [ "$SUM" != "$ORIG_SUM" ]; then
        echo "Checksum error, check the files in $KERL_DOWNLOAD_DIR"
        exit 1
    fi
    echo "Checksum verified ($SUM)"
}

case "$1" in
    build)
        if [ "$2" = "git" ]; then
            if [ $# -ne 5 ]; then
                echo "usage: $0 $1 $2 <git_url> <git_version> <build_name>"
                exit 1
            fi
            do_git_build $3 $4 $5
        else
            if [ $# -lt 3 ]; then
                echo "usage: $0 $1 <release> <build_name>"
                exit 1
            fi
            do_build $2 $3
        fi
        ;;
    install)
        if [ $# -lt 2 ]; then
            echo "usage: $0 $1 <build_name> [directory]"
            exit 1
        fi
        if [ $# -eq 3 ]; then
            do_install $2 "$3"
        else
            if [ -z "$KERL_DEFAULT_INSTALL_DIR" ]; then
              do_install $2 .
            else
              do_install $2 "$KERL_DEFAULT_INSTALL_DIR/$2"
            fi
        fi
        ;;
    deploy)
        if [ $# -lt 2 ]; then
            echo "usage: $0 $1 <[user@]host> [directory] [remote_directory]"
            exit 1
        fi
        if [ $# -eq 4 ]; then
            do_deploy $2 "$3" "$4"
        else
            if [ $# -eq 3 ]; then
                do_deploy $2 "$3"
            else
                do_deploy $2 .
            fi
        fi
        ;;
    update)
        if [ $# -lt 2 ]; then
            update_usage
            exit 1
        fi
        case "$2" in
            releases)
                rm -f "$KERL_BASE_DIR/otp_releases"
                check_releases
                echo "The available releases are:"
                list_print releases spaces
                ;;
            agner)
                if [ $# -ne 3 ]; then
                    echo "usage: $0 $1 $2 <build_name>"
                    exit 1
                fi
                if agner_support $3; then
                    echo "Updating agner for build $3..."
                    if do_update_agner $3; then
                        echo "agner has been updated successfully"
                    else
                        echo "failed to update agner"
                        exit 1
                    fi
                fi
                ;;
            *)
                update_usage
                exit 1
                ;;
        esac
        ;;
    list)
        if [ $# -ne 2 ]; then
            list_usage
            exit 1
        fi
        case "$2" in
            releases)
                check_releases
                list_print $2 space
                echo "Run \"$0 update releases\" to update this list from erlang.org"
                ;;
            builds)
                list_print $2
                ;;
            installations)
                list_print $2
                ;;
            *)
                echo "Cannot list $2"
                list_usage
                exit 1
                ;;
        esac
        ;;
    delete)
        if [ $# -ne 3 ]; then
            delete_usage
            exit 1
        fi
        case "$2" in
            build)
                rel=`get_release_from_name $3`
                if [ -d "$KERL_BUILD_DIR/$3" ]; then
                    rm -Rf "$KERL_BUILD_DIR/$3"
                    list_remove $2s "$rel,$3"
                    echo "The $3 build has been deleted"
                else
                    echo "No build named $3"
                    exit 1
                fi
                ;;
            installation)
                assert_valid_installation "$3"
                rm -Rf "$3"
                escaped=`echo "$3" | sed $SED_OPT -e 's#/$##' -e 's#\/#\\\/#g'`
                list_remove $2s "$escaped"
                echo "The installation in $3 has been deleted"
                ;;
            *)
                echo "Cannot delete $2"
                delete_usage
                exit 1
                ;;
        esac
        ;;
    active)
        if ! do_active; then
            exit 1;
        fi
        ;;
    status)
        echo "Available builds:"
        list_print builds
        echo "----------"
        echo "Available installations:"
        list_print installations
        echo "----------"
        do_active
        exit 0
        ;;
    prompt)
        FMT=" (%s)"
        if [ -n "$2" ]; then
            FMT="$2"
        fi
        ACTIVE_PATH=`get_active_path`
        if [ -n "$ACTIVE_PATH" ]; then
            ACTIVE_NAME=`get_name_from_install_path "$ACTIVE_PATH"`
            if [ -z "$ACTIVE_NAME" ]; then
                VALUE="`basename "$ACTIVE_PATH"`*"
            else
                VALUE="$ACTIVE_NAME"
            fi
            printf "$FMT" "$VALUE"
        fi
        exit 0
        ;;
    *)
        echo "unkwnown command: $1"; usage; exit 1
        ;;
esac