diff bin/putcrc @ 4:94ee419ad047

Added bin-ec - a collection of scripts.
author Edho Prima Arief <me@myconan.net>
date Sat, 10 Jul 2010 12:38:52 +0000
parents
children 08d5f6023998
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/putcrc	Sat Jul 10 12:38:52 2010 +0000
@@ -0,0 +1,32 @@
+#!/bin/sh
+safe_path()
+{
+	# It all depends on the first character.
+	__start=$(printf "%s" "$*" | cut -c 1)
+	__path=
+	case "${__start}" in
+		.|/) __path="$*";; # . and / is safe. No change.
+		*) __path="./$*";; # Anything else must be prefixed with ./
+	esac
+	printf "%s" "${__path}" # Return.
+}
+
+if [ "$#" -lt 1 ]; then
+	cat <<EOF
+Usage: $(basename "/$0") file1 file2 ... fileN
+EOF
+	exit 1
+fi
+
+for file in "$@"; do
+	if [ -f "${file}" ] && [ -r "${file}" ] && [ -n "$(printf "%s\n" "${file}" | sed -e 's/.*[A-F0-9]\{8\}.*//')" ]; then
+		filename="$(basename "$(safe_path "${file}")")"
+		dirname="$(dirname "$(safe_path "${file}")")"
+		myfile="$(printf "%s\n" "${filename%.*}" | sed -e 's/\([^]]\)$/\1 /')"
+		myext="${filename##*.}"; if [ "${myext}" = "${filename}" ]; then myext=""; else myext=".${myext}"; fi
+		crc=$(cek.rb "$(safe_path "${file}")" | tail -1 | sed -e 's/.*\([A-F0-9]\{8\}\)$/\1/')
+		mv -- "${file}" "${dirname}/${myfile}[${crc}]${myext}" && printf "%s => %s\n" "${file}" "${myfile}[${crc}]${myext}"
+	else
+		printf "%s\n" "${file} is not a file or unreadable"
+	fi
+done