diff --git a/bin/ruby-package b/bin/ruby-package new file mode 100755 index 00000000..e96ad0ca --- /dev/null +++ b/bin/ruby-package @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -e + +definition="$1" +if [ -z "$definition" ]; then + echo "usage: ruby-package DEFINITION" +fi + +package="${definition##*/}" +prefix="/tmp/ruby-build/-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/$package" + + +# ruby-build "$definition" "$prefix" +cwd="$(pwd)" +cd "${prefix}/.." + +# FIXME don't assume share is in cwd +mkdir -p "${package}.rubypackage/bin" +cp "${cwd}/share/ruby-package/"* "${package}.rubypackage/bin" + +mkdir -p "${package}.rubypackage/metadata" +echo -n "$prefix" > "${package}.rubypackage/metadata/prefix" +echo -n "$package" > "${package}.rubypackage/metadata/package" + +cd "$package" + +while read line; do + binary="${line#Binary file }" + if [ "$line" = "$binary" ]; then + # plain text match + text="${line%%:*}" + echo "$text" >> "../${package}.rubypackage/metadata/text-files" + else + # binary match + binary="${binary% matches}" + echo "$binary" >> "../${package}.rubypackage/metadata/binary-files" + fi +done < <( grep -m 1 -R "$prefix" * ) + +tar cf "../${package}.rubypackage/package.tar" * + +cd .. + +tar czf "${cwd}/${package}.rubypackage.tar.gz" "${package}.rubypackage" +rm -fr "${package}.rubypackage" diff --git a/share/ruby-package/ruby-package-install b/share/ruby-package/ruby-package-install new file mode 100755 index 00000000..6be5b6f8 --- /dev/null +++ b/share/ruby-package/ruby-package-install @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -e + +prefix="$1" +if [ -z "$prefix" ]; then + echo "usage: ruby-package-install PREFIX" >&2 + exit 1 +fi + +resolve_link() { + $(type -p greadlink readlink | head -1) $1 +} + +abs_dirname() { + local cwd="$(pwd)" + local path="$1" + + while [ -n "$path" ]; do + cd "${path%/*}" + local name="${path##*/}" + path="$(resolve_link "$name" || true)" + done + + pwd + cd "$cwd" +} + +bin_root="$(abs_dirname "$0")" +package_root="${bin_root}/.." +package_prefix="$(cat "${package_root}/metadata/prefix")" +package="$(cat "${package_root}/metadata/package")" + +mkdir -p "$prefix" +cd "$prefix" +prefix="$(pwd)" + +rewrite() { + kind="$1" + while read file; do + path="${prefix}/$file" + cp "$path" "${path}.orig" + "${bin_root}/ruby-package-rewrite-$kind" "$package_prefix" "$prefix" < "${path}.orig" > "$path" + rm "${path}.orig" + done < "${package_root}/metadata/${kind}-files" +} + +tar xzf "${package_root}/package.tar" +rewrite binary +rewrite text diff --git a/share/ruby-package/ruby-package-rewrite-binary b/share/ruby-package/ruby-package-rewrite-binary new file mode 100755 index 00000000..debd559f --- /dev/null +++ b/share/ruby-package/ruby-package-rewrite-binary @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -e + +src_prefix="$1" +dst_prefix="$2" + +if [ -z "$src_prefix" ] || [ -z "$dst_prefix" ]; then + echo "usage: ruby-package-install-binary SRC_PREFIX DST_PREFIX" >&2 + exit 1 +fi + +hexify() { + xxd -c 1 -p | sed 's/^00$/,/' | tr ',\n' '\n ' +} + +search_hex="$(echo -n "$src_prefix" | hexify)" +replace_hex="$(echo -n "$dst_prefix" | hexify)" + +hexify | +awk ' + BEGIN { + search_hex=" '"$search_hex"'"; + replace_hex=" '"$replace_hex"'"; + split(search_hex, search); + split(replace_hex, replace); + search_len=length(search); + replace_len=length(replace); + } + { matched=0 } + / '"$search_hex"'/ { + result=replace_hex substr($0, length(search_hex) + 1); + for (i=length(replace)+1; i<=length(search); i++) + result=result "00 "; + print result; + matched=1 + } + { if (matched == 0) print $0 } +' | +sed 's/$/00/' | +xxd -p -r diff --git a/share/ruby-package/ruby-package-rewrite-text b/share/ruby-package/ruby-package-rewrite-text new file mode 100755 index 00000000..8d9d87bf --- /dev/null +++ b/share/ruby-package/ruby-package-rewrite-text @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -e + +src_prefix="$1" +dst_prefix="$2" + +if [ -z "$src_prefix" ] || [ -z "$dst_prefix" ]; then + echo "usage: ruby-package-install-text SRC_PREFIX DST_PREFIX" >&2 + exit 1 +fi + +sed "s:${src_prefix}:${dst_prefix}:g"