Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2015 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fe2c15ab1b81e62a5e06 to your computer and use it in GitHub Desktop.
Save anonymous/fe2c15ab1b81e62a5e06 to your computer and use it in GitHub Desktop.
/home/romster/bin/pkg-url
#!/bin/bash
#
# Copyright (c) 2012 by Daniel K Rawlins, monster dot romster at gmail dot com
#
# 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.
test_url() {
unset list
for port in $PORTS; do
unset source name version
if [ -e $port/Pkgfile ]; then
. $port/Pkgfile
else
. Pkgfile
fi
echo "name: ${name}"
for i in ${!source[*]}; do
if [ -n "$(echo "${source[$i]}" | grep '://')" ]; then
for i in ${!source[*]}; do
curl="$(curl -m 15 -sI ${source[$i]} |grep HTTP/)"
echo "url: ${source[$i]}"
echo "result: ${curl}"
done
fi
done
done
}
parse_url() {
unset list
for port in $PORTS; do
unset source name version
if [ -e $port/Pkgfile ]; then
. $port/Pkgfile
else
. Pkgfile
fi
for i in ${!source[*]}; do
if [ -n "$(echo "${source[$i]}" | grep '://')" ]; then
list=(${list[*]} ${source[$i]})
fi
done
done
}
parse_url_base() {
unset list
for port in $PORTS; do
unset source name version
if [ -e $port/Pkgfile ]; then
. $port/Pkgfile
else
. Pkgfile
fi
for i in ${!source[*]}; do
if [ -n "$(echo "${source[$i]}" | grep '://')" ]; then
list=( ${list[*]} $( echo ${source[$i]} | sed -e 's|\(^.*://.*/\).*|\1|g' ) )
fi
done
done
}
show_result() {
unset result
IFS=$'\n'
result=( $( printf "%s\n" "${list[@]}" | awk 'x[$0]++ == 0 { print $0 }' ) )
for u in ${!result[*]}; do
echo "${result[$u]}"
done
}
print_help() {
echo "usage: pkg-url [options]"
echo "options:"
echo " -b, --base-url print base url only"
echo " -m --maintainer print ports for maintainer only"
echo " -p --port print port name"
echo " -t --test test port url"
echo " -h, --help print help and exit"
}
parse_options() {
while [ "$1" ]; do
case $1 in
-b|--base-url)
PKGURL_BASE_URL="yes" ;;
-m|--maintainer)
if [ ! "$2" ]; then
echo "pkg-url: option $1 requires an argument"
exit 1
fi
PKGURL_MAINTAINER="$2"
shift ;;
-p|--port)
if [ ! "$2" ]; then
echo "pkg-url: option $1 requires an argument"
exit 1
fi
PKGURL_PORT="$2"
shift ;;
-t|--test)
if [ ! "$2" ]; then
echo "pkg-url: option $1 requires an argument"
exit 1
fi
PKGURL_TEST="$2"
shift ;;
-h|--help)
print_help
exit 0 ;;
*)
echo "pkg-url: invalid option $1"
exit 1 ;;
esac
shift
done
}
main() {
parse_options "$@"
if [ -n "$PKGURL_TEST" ]; then
PORTS="$(find /usr/ports -type d -name $PKGURL_TEST)"
test_url
unset PKGURL_PORT PKGURL_MAINTAINER
exit
fi
if [ -n "$PKGURL_PORT" ]; then
PORTS="$(find /usr/ports -type d -name $PKGURL_PORT)"
else
if [ -n "$PKGURL_MAINTAINER" ]; then
PORTS="$(egrep "# Maintainer:[[:space:]]*$(echo "$PKGURL_MAINTAINER" | sed -e 's|\(.*\)-\(.*\)|\1 \2|')" */Pkgfile | awk -F / '{print $1}' | xargs)"
else
PORTS="$(find -name Pkgfile | sed -e 's|\./\(.*\)/.*|\1|' | xargs)"
fi
fi
if [ "$PKGURL_BASE_URL" = "yes" ]; then
parse_url_base
else
parse_url
fi
show_result
exit 0
}
main "$@"
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment