|
| 1 | +#! /bin/sh |
| 2 | + |
| 3 | +# Copyright (c) 2011 Evax Software <contact(at)evax(dot)org> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +ERLANG_DOWNLOAD_URL=http://www.erlang.org/download |
| 24 | + |
| 25 | +KERL_BASE_DIR=$HOME/.kerl |
| 26 | +KERL_CONFIG=$HOME/.kerlrc |
| 27 | +KERL_DOWNLOAD_DIR=$KERL_BASE_DIR/archives |
| 28 | +KERL_BUILD_DIR=$KERL_BASE_DIR/builds |
| 29 | +KERL_CONFIGURE_OPTIONS= |
| 30 | +KERL_MAKE_OPTIONS= |
| 31 | + |
| 32 | +# source the config file if available |
| 33 | +if [ -f "$KERL_CONFIG" ]; then . "$KERL_CONFIG"; fi |
| 34 | + |
| 35 | +usage() { |
| 36 | + echo "kerl: build and install Erlang/OTP" |
| 37 | + echo "usage: $0 <command> [options ...]" |
| 38 | + echo "\n <command> Command to be executed\n" |
| 39 | + echo "Valid commands are:" |
| 40 | + echo " build Build specified release" |
| 41 | + echo " install Install the specified release at the given location" |
| 42 | + echo " update Update the list of available releases from erlang.org" |
| 43 | + echo " list List releases, builds and installations" |
| 44 | + exit 1 |
| 45 | +} |
| 46 | + |
| 47 | +if [ $# -eq 0 ]; then usage; fi |
| 48 | + |
| 49 | +get_releases() { |
| 50 | + curl -s $ERLANG_DOWNLOAD_URL/ | \ |
| 51 | + sed -e 's/^.*>otp_src_\(R1[-1234567890ABCD]\+\)\.tar\.gz<.*$/\1/' \ |
| 52 | + -e '/^R/!d' |
| 53 | +} |
| 54 | + |
| 55 | +update_checksum_file() { |
| 56 | + echo "Getting the checksum file from erlang.org..." |
| 57 | + curl $ERLANG_DOWNLOAD_URL/MD5 > "$KERL_DOWNLOAD_DIR/MD5" || exit 1 |
| 58 | +} |
| 59 | + |
| 60 | +ensure_checksum_file() { |
| 61 | + if [ ! -f "$KERL_DOWNLOAD_DIR/MD5" ]; then |
| 62 | + update_checksum_file |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +check_releases() { |
| 67 | + if [ ! -f "$KERL_BASE_DIR/otp_releases" ]; then |
| 68 | + echo "Getting the available releases from erlang.org..." |
| 69 | + get_releases > "$KERL_BASE_DIR/otp_releases" |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +assert_valid_release() { |
| 74 | + check_releases |
| 75 | + for rel in `cat $KERL_BASE_DIR/otp_releases`; do |
| 76 | + if [ "$1" = "$rel" ]; then |
| 77 | + return 0 |
| 78 | + fi |
| 79 | + done |
| 80 | + echo "$1 is not a valid Erlang/OTP release" |
| 81 | + exit 1 |
| 82 | +} |
| 83 | + |
| 84 | +do_build() { |
| 85 | + assert_valid_release $1 |
| 86 | + FILENAME=otp_src_$1.tar.gz |
| 87 | + if [ ! -f "$KERL_DOWNLOAD_DIR/$FILENAME" ]; then |
| 88 | + echo "Downloading $FILENAME to $KERL_DOWNLOAD_DIR" |
| 89 | + mkdir -p "$KERL_DOWNLOAD_DIR" |
| 90 | + curl $ERLANG_DOWNLOAD_URL/$FILENAME > "$KERL_DOWNLOAD_DIR/$FILENAME" |
| 91 | + update_checksum_file |
| 92 | + fi |
| 93 | + ensure_checksum_file |
| 94 | + echo "Verifying archive checksum..." |
| 95 | + SUM=`md5sum $KERL_DOWNLOAD_DIR/$FILENAME | cut -d " " -f 1` |
| 96 | + ORIG_SUM=`grep $FILENAME $KERL_DOWNLOAD_DIR/MD5 | cut -d " " -f 2` |
| 97 | + if [ "$SUM" != "$ORIG_SUM" ]; then |
| 98 | + echo "Checksum error, check the files in $KERL_DOWNLOAD_DIR" |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + echo "Checksum verified ($SUM)" |
| 102 | + mkdir -p $KERL_BUILD_DIR |
| 103 | + mkdir -p $KERL_BASE_DIR/logs |
| 104 | + if [ ! -d $KERL_BUILD_DIR/otp_src_$1 ]; then |
| 105 | + echo "Extracting source code" |
| 106 | + cd $KERL_BUILD_DIR && tar xfz $KERL_DOWNLOAD_DIR/$FILENAME |
| 107 | + fi |
| 108 | + echo "Building Erlang/OTP $1, please wait..." |
| 109 | + cd $KERL_BUILD_DIR/otp_src_$1 |
| 110 | + ./configure $KERL_CONFIGURE_OPTIONS > \ |
| 111 | + $KERL_BASE_DIR/logs/configure_$1.log 2>&1 |
| 112 | + if [ "$?" -eq 1 ]; then |
| 113 | + echo "./configure failed"; |
| 114 | + exit 1 |
| 115 | + fi |
| 116 | + rm -f "$KERL_BASE_DIR/logs/configure_$1.log" |
| 117 | + make $KERL_MAKE_OPTIONS > $KERL_BASE_DIR/logs/build_$1.log 2>&1 |
| 118 | + if [ "$?" -eq 1 ]; then |
| 119 | + echo "Build failed, see $KERL_BASE_DIR/logs/build_$1.log"; |
| 120 | + list_remove builds $2 |
| 121 | + fi |
| 122 | + rm -f "$KERL_BASE_DIR/logs/build_$1.log" |
| 123 | + echo "Erlang/OTP $1 has been successfully built"; |
| 124 | + list_add builds $1 |
| 125 | +} |
| 126 | + |
| 127 | +do_install() { |
| 128 | + assert_valid_release $1 |
| 129 | + if ! list_has builds $1; then |
| 130 | + echo "You must build the $1 realease before installing it" |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + mkdir -p "$2" |
| 134 | + if [ ! -d "$2" ]; then |
| 135 | + echo "Destination is not a directory" |
| 136 | + exit 1 |
| 137 | + fi |
| 138 | + absdir=`cd "$2" && pwd` |
| 139 | + echo "Installing Erlang/OTP $1 in $absdir" |
| 140 | + cd $KERL_BUILD_DIR/otp_src_$1 |
| 141 | + ./configure --prefix="$absdir" > /dev/null 2>&1 |
| 142 | + make install > /dev/null 2>&1 |
| 143 | + if [ $? -eq 1 ]; then |
| 144 | + echo "Couldn't install Erlang/OTP $1 in $absdir" |
| 145 | + exit 1 |
| 146 | + fi |
| 147 | + list_add installations "$1 $absdir"; |
| 148 | + echo "export PATH=$absdir/bin:\$PATH" > $absdir/activate |
| 149 | + echo "You can activate this installation running the following command:" |
| 150 | + echo ". $absdir/activate" |
| 151 | +} |
| 152 | + |
| 153 | +list_print() { |
| 154 | + if [ -f $KERL_BASE_DIR/otp_$1 ]; then |
| 155 | + if [ -z "$2" ]; then |
| 156 | + cat $KERL_BASE_DIR/otp_$1 |
| 157 | + else |
| 158 | + echo `cat $KERL_BASE_DIR/otp_$1` |
| 159 | + fi |
| 160 | + else |
| 161 | + echo "There are no $1 available" |
| 162 | + fi |
| 163 | +} |
| 164 | + |
| 165 | +list_add() { |
| 166 | + if [ -f $KERL_BASE_DIR/otp_$1 ]; then |
| 167 | + grep $2 $KERL_BASE_DIR/otp_$1 > /dev/null 2>&1 || \ |
| 168 | + echo $2 >> $KERL_BASE_DIR/otp_$1 |
| 169 | + else |
| 170 | + echo $2 > $KERL_BASE_DIR/otp_$1 |
| 171 | + fi |
| 172 | +} |
| 173 | + |
| 174 | +list_remove() { |
| 175 | + if [ -f $KERL_BASE_DIR/otp_$1 ]; then |
| 176 | + sed -i -e '/$2/d' $KERL_BASE_DIR/otp_$1 |
| 177 | + fi |
| 178 | +} |
| 179 | + |
| 180 | +list_has() { |
| 181 | + if [ -f $KERL_BASE_DIR/otp_$1 ]; then |
| 182 | + grep $2 $KERL_BASE_DIR/otp_$1 > /dev/null 2>&1 && return 0 |
| 183 | + fi |
| 184 | + return 1 |
| 185 | +} |
| 186 | + |
| 187 | +list_usage() { |
| 188 | + echo "usage: $0 list <releases|builds|installations>" |
| 189 | +} |
| 190 | + |
| 191 | +case "$1" in |
| 192 | + build) |
| 193 | + if [ $# -ne 2 ]; then |
| 194 | + echo "specify a release to install (e.g. R14B02)" |
| 195 | + exit 1 |
| 196 | + fi |
| 197 | + do_build $2 |
| 198 | + ;; |
| 199 | + install) |
| 200 | + if [ $# -lt 2 ]; then |
| 201 | + echo "usage: $0 $1 <release> [directory]" |
| 202 | + exit 1 |
| 203 | + fi |
| 204 | + if [ $# -eq 3 ]; then |
| 205 | + do_install $2 $3 |
| 206 | + else |
| 207 | + do_install $2 . |
| 208 | + fi |
| 209 | + ;; |
| 210 | + update) |
| 211 | + rm -f $KERL_BASE_DIR/otp_releases |
| 212 | + check_releases |
| 213 | + echo "The available releases are:" |
| 214 | + list_print releases spaces |
| 215 | + ;; |
| 216 | + list) |
| 217 | + if [ $# -ne 2 ]; then |
| 218 | + list_usage |
| 219 | + exit 1 |
| 220 | + fi |
| 221 | + case "$2" in |
| 222 | + releases) |
| 223 | + check_releases |
| 224 | + list_print $2 space |
| 225 | + echo "Run \"$0 update\" to update this list from erlang.org" |
| 226 | + ;; |
| 227 | + builds) |
| 228 | + list_print $2 |
| 229 | + ;; |
| 230 | + installations) |
| 231 | + list_print $2 |
| 232 | + ;; |
| 233 | + *) |
| 234 | + echo "Cannot list $2" |
| 235 | + list_usage |
| 236 | + exit 1 |
| 237 | + ;; |
| 238 | + esac |
| 239 | + ;; |
| 240 | + *) |
| 241 | + echo "unkwnown command: $1"; usage; exit 1 |
| 242 | + ;; |
| 243 | +esac |
| 244 | + |
0 commit comments