#!/usr/bin/env perl use strict; use warnings; my $version = '2016-02-14-1138-07439d3'; my $base_url = 'http://get.pmb.io'; my $os = $^O; my $bits = `uname -m`; my $local = "$ENV{HOME}/.pmb"; my $os_bits_to_name = { darwin => { x86_64 => 'pmb_darwin_amd64', }, linux => { x86_64 => 'pmb_linux_amd64', armv6l => 'pmb_linux_arm' }, }; chomp($bits); DEBUG("OS: $os\n"); DEBUG("BITS: $bits\n"); DEBUG("VERSION: $version\n"); DEBUG("LOCAL: $local\n"); my $binary = $os_bits_to_name->{$os}{$bits} || die "Unknown os/bits combination $os/$bits\n"; my $sum = get_sum($binary); if ( !-e $local || !check_sum( $local, $sum ) ) { fetch( "${base_url}/${version}/$binary", $local ); chmod 0755, $local; } if ( !check_sum( $local, $sum ) ) { die "Downloaded file doesn't match checksum.\n"; } DEBUG("Running ${local}\n"); exec $local, @ARGV; sub fetch { my ( $remote_path, $local_path ) = @_; INFO("Fetching $remote_path\n"); if ( bin_exists('wget') ) { system("wget -q $remote_path -O $local_path"); } elsif ( bin_exists('curl') ) { system("curl -s $remote_path > $local_path"); } my $rc = $? >> 8; DEBUG("RC: $rc\n"); if ( $rc > 0 ) { unlink($local_path); die "Unable to fetch $remote_path\n"; } } sub bin_exists { my $prog = shift; return scalar grep {defined} map { -x $_ } map {"$_/$prog"} split( /:/, $ENV{PATH} ); } sub get_sum { my $binary_name = shift; foreach my $sum_info () { chomp($sum_info); my ( $sum, $name ) = split( /\s+/, $sum_info ); if ( $name eq $binary_name ) { return $sum; } } } sub check_sum { my ( $local_path, $sum ) = @_; my $file_sum; if ( bin_exists('openssl') ) { my $output = `openssl md5 $local_path`; $file_sum = ( split( /\s+/, $output ) )[1]; } elsif ( bin_exists('md5sum') ) { my $output = `md5sum $local_path`; ($file_sum) = split( /\s+/, $output ); } elsif ( bin_exists('gmd5sum') ) { my $output = `gmd5sum $local_path`; ($file_sum) = split( /\s+/, $output ); } else { die "Unable to check file sum."; } DEBUG("downloaded: $file_sum\n"); DEBUG("checked: $sum\n"); return $file_sum eq $sum; } sub DEBUG { if ( $ENV{PMB_BOOTSTRAP_DEBUG} ) { print @_; } } sub INFO { print @_; } __END__ 21238612e831a1a55f007347d947177a pmb_darwin_amd64 dd3c4209093c1689fe450797c02680df pmb_linux_amd64 34e4367c59e665b3426bc950c26b8c69 pmb_linux_arm