#!/bin/bash # Rename patch files according to the Content-Disposition header line # if they do have one, or Subject if they don't. # # Copyright (C) 2005, 2006 Jean Delvare # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details: # http://www.gnu.org/copyleft/gpl.html # # original found at http://jdelvare.pck.nerim.net/linux/rename-patch # findname() { local file=$1 pattern; # Try Content-Disposition first pattern='^Content-Disposition:[[:space:]]*inline[[:space:]]*;[[:space:]]*filename[[:space:]]*='; if grep -q $pattern "$file" then sed -ne "/$pattern/{s/$pattern[[:space:]]*//p;q}" "$file" return fi # Fallback to Subject pattern='^Subject:'; if grep -q $pattern "$file" then sed -ne "/$pattern/{ s/$pattern[[:space:]]*//; s/\[[^]]*\]//g; s/([^)]*)//g; s/[^a-z0-9._-]/-/ig; s/--*/-/g; s/^--*//; s/[.-]*$/.patch/; p;q}" "$file" | \ tr A-Z a-z return fi } for file in $@ do name=$(findname "$file") if [ -z "$name" ] then echo "No name found for $file" >&2 continue fi if [ "$name" == "$file" ] then # It's already OK continue fi if [ -e "$name" ] then echo "Can't rename $file to $name which already exists" >&2 continue fi mv -f "$file" "$name" echo $name done