#!/bin/sh
#
# Calculates the three hashes needed for a MacPort and outputs them
# in the proper format for easy copy'n'paste into the Portfile
#
# Usage:
#
# ./checksum.sh path/to/file or
# ./checksum.sh http://some.url/some.remote.file.tgz
#
# Example output:
#
# checksums md5 45daad482c81175dfa8e66fd33b62585 \
# sha1 093001234a73004c97fa706119031f726a6671c3 \
# rmd160 10ab84e72c16f0df4eb7048583ebe879dfd4e55b
#
# (c) Thomas Keller <me@thomaskeller.biz>
#
# Licensed under GPLv3
#
if [ -z "$1" ]; then
echo "No argument given"
exit 1
fi
is_url=0
case "$1" in
http*)
is_url=1
file=/tmp/$0.$$
curl $1 >$file 2>/dev/null
if [ ! -s "$file" ]; then
echo "$1 could not be downloaded"
exit 2
fi
;;
*)
file=$1
;;
esac
if [ ! -e "$file" ]; then
echo "'$file' does not exist"
exit 3
fi
md5=`md5 "$file"`
sha1=`openssl sha1 "$file"`
rmd160=`openssl rmd160 "$file"`
if [ $is_url -eq 1 ]; then
rm -f $file
fi
echo "checksums md5${md5#*=} \\"
echo " sha1${sha1#*=} \\"
echo " rmd160${rmd160#*=}"