#!/bin/bash # The purpose of this thing is to pick the parts out of a best_version. # Working to add a way to make it skip a given part (package, version or # revision). # To add it to an ebuild/eclass, drop the 'portageq' call and the $ROOT mention # in it ('/'). $1 is the package you want to match, $2 is the variable you want # the revision set to at the end, $3 is the variable you want the version set # to, $4 is the variable you want the package name set to, $5 is the variable # you want the category name set to. # It is currently NOT exported to the environment. As written, this depends on # bash-2, portageq, cut, echo and seq. # Author: Donnie Berkholz # In a format suitable for a DEPEND string PACKAGE_ATOM="${1:-=sys-apps/coreutils-5*}" # Pick the name of the variable you want the revision set to REVISION_NAME="${2:-COREUTILS_REVISION}" # Pick the name of the variable you want the version set to VERSION_NAME="${3:-COREUTILS_VERSION}" # Pick the name of the variable you want the name set to PACKAGE_NAME="${4:-COREUTILS_PACKAGE}" # Pick the name of the variable you want the category set to CATEGORY_NAME="${5:-COREUTILS_CATEGORY}" # For testing, to optimize times without calling portage if [ -n "${SKIP_PORTAGEQ}" ]; then FULL_NAME="${1:-x11-libs/gtk+-1.2.10-r11}" else FULL_NAME="$(portageq best_version / ${PACKAGE_ATOM})" fi # Initialize parts array declare -a PARTS # Grab everything after the / (in other words, the pkg-ver-rev) PARTS=($(echo ${FULL_NAME} \ | cut -d/ -f2 --output-delimiter=' ' \ | cut -d- -f1- --output-delimiter=' ')) # Grab category declare ${CATEGORY_NAME}="$(echo ${FULL_NAME} \ | cut -d/ -f1 --output-delimiter=' ')" echo "Confirmed category ${!CATEGORY_NAME}." # Start countdown based on last part -- note arrays start at 0 PART_COUNTDOWN="$(( ${#PARTS[@]} - 1 ))" # Make sure these isn't around before we start unset ${VERSION_NAME} ${REVISION_NAME} ${PACKAGE_NAME} # Initialize declare LOOP_COUNT="0" REVISION_DONE="0" VERSION_DONE="0" PACKAGE_DONE="0" # The ${!VAR} thing is called an indirect variable reference. # a=b, b=c, ${a}=b, ${!a}=c # This particular way of doing it requires bash 2 or greater. # The other option involves eval echo \$$foo. until [ -n "${!PACKAGE_NAME}" ]; do if [ ${VERSION_DONE} -eq 0 ]; then # Test whether it starts with 'r' (i.e., is a revision) if [ "${PARTS[${PART_COUNTDOWN}]:0:1}" = "r" ] \ && [ ${REVISION_DONE} -eq 0 ]; then declare ${REVISION_NAME}="${PARTS[${PART_COUNTDOWN}]}" echo "Confirmed revision ${!REVISION_NAME}." # Test whether it starts with a number (i.e., is a version) else for NUMBER in $(seq 0 9); do if [ "${PARTS[${PART_COUNTDOWN}]:0:1}" = "${NUMBER}" ]; then declare ${VERSION_NAME}="${PARTS[${PART_COUNTDOWN}]}" echo "Confirmed version ${!VERSION_NAME}." fi done VERSION_DONE="1" fi REVISION_DONE="1" elif [ ${PACKAGE_DONE} -eq 0 ]; then # Unhyphenated package names if [ ${PART_COUNTDOWN} -eq 0 ]; then declare ${PACKAGE_NAME}="${PARTS[${PART_COUNTDOWN}]}" # Hyphenated package names else for PACKAGE_COUNTDOWN in $(seq ${PART_COUNTDOWN} -1 0); do declare ${PACKAGE_NAME}="${PARTS[${PACKAGE_COUNTDOWN}]}${!PACKAGE_NAME:+-${!PACKAGE_NAME}}" done fi echo "Confirmed package ${!PACKAGE_NAME}." PACKAGE_DONE="1" else # If it didn't have anything left to make a name with, die if [ -z ${!PACKAGE_NAME} ]; then echo "Not enough data fields. Quitting." break fi break fi # Counter (( PART_COUNTDOWN -= 1 )) if (( PART_COUNTDOWN < 0 )); then echo "Countdown less than zero. Quitting." break fi echo "Looping..." done echo "R=${!REVISION_NAME:-(none)} V=${!VERSION_NAME:-(none)} P=${!PACKAGE_NAME:-(none)} C=${!CATEGORY_NAME:-(none)}" echo "Reconstructed: ${!CATEGORY_NAME}/${!PACKAGE_NAME}-${!VERSION_NAME}${!REVISION_NAME:+-${!REVISION_NAME}}"