#!/bin/sh
###################################################################################################
##  |\   ======   /|  ##             MasarLabs Mini Utilities              ##  |\   ======   /|  ##
##  |__\========/__|  ##     simple little programs to do util things      ##  |__\========/__|  ##
##  |\            /|  ##  ===============================================  ##  |\            /|  ##
##  |  \        /  |  ##           Maurizio Sartori (c) 2000-2004          ##  |  \        /  |  ##
##  |    \    /    |  ##  ===============================================  ##  |    \    /    |  ##
##  |    /=\/=\    |  ##                    Written  by                    ##  |    /=\/=\    |  ##
##  |  /========\  |  ##              Maurizio Sartori 'masar'             ##  |  /========\  |  ##
##  |/   ======   \|  ##            e-mail:  masar@MasarLabs.com           ##  |/   ======   \|  ##
###################################################################################################
## $Id: normalizeall 202 2004-12-01 14:32:50Z svn $
###################################################################################################
##
## 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.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
##
###################################################################################################

REGEX_FILES_EXT='ac|asc|awk|c|cfg|conf|cron|css|cpp|dtx|emg|fig|gng|in|ini|h|html|java|js|jsp'
REGEX_FILES_EXT="$REGEX_FILES_EXT|m4|mf|php|pl|po|pot|pov|res|sh|shape|sheet|sql|sty|svg"
REGEX_FILES_EXT="$REGEX_FILES_EXT|tex|texi|tld|txt|xhtml|xml|xpm|xsl|xsd|[1-8]"
REGEX_MAKES='Makefile.*|.*\.am'
REGEX_SKIPS_EXT='au|bz2|cgi|eps|exe|gif|gz|ico|jar|jpg|pdf|png|wav|wmf|xcf'

REGEX_FILES="(.*\\.($REGEX_FILES_EXT))|([^.][^.]*)"
REGEX_SKIPS=".*\\.($REGEX_SKIPS_EXT)"
REGEX_FULL_SKIPS='/(.svn|CVS)/'

set -e

case "$1" in
-h|--h|--he|--hel|--help)
    echo "\
$0 [OPTION] FILES

Normalize the given files changing TAB with SPACES and
removing leading spaces.

Options:
  -h, --help      display this help and exit
  -v, --version   output version information and exit
  -m, --make      preserve the first TAB, used for files like Makefile"
    exit 0
    ;;
-v|--version)
    VER=$(echo '$Rev: 202 $' | sed -e 's/ *\$$//g' -e 's/\$Rev: //')
    echo "normalize $VER"
    exit 0
    ;;
-m|--make)
    MAKE=1
    shift
    ;;
-*)
    echo 1>&2 "$0: Unknown \`$1' option"
    echo 1>&2 "Try \`$0 --help' for more information"
    exit 1
    ;;
esac


for i in which egrep expand printf sed mktemp
do
   if ! which $i > /dev/null 2>&1
   then
      echo "error: the '$i' program is missing"
      exit 1
   fi
done

CR=$(printf "\r")
TAB=$(printf "\t")
B1=$(printf "\x01")

find . -type f |
while read file
do
   filename=$(basename "$file")
   if echo "$file" | egrep -q "$REGEX_FULL_SKIPS"
   then
      true      # No Operation
   elif echo "$filename" | egrep -q "^($REGEX_SKIPS)\$"
   then
      true      # No Operation
   elif echo "$filename" | egrep -q "^($REGEX_MAKES)\$"
   then
      echo "make  $file"
      tmp=$(mktemp -t "$filename.XXXXXX")
      if [ $? -ne 0 ]
      then
         echo "$(basename $0): cannot create temporary file" 1>&2
         exit 1
      fi
      sed -e "s/^$TAB/$B1/" -e "s/$CR\$//g" < "$file" | expand >> "$tmp"
      sed -e 's/  *$//' -e "s/^$B1/$TAB/" < "$tmp" > "$file"
      rm "$tmp"
   elif echo "$filename" | egrep -q "^($REGEX_FILES)\$"
   then
      echo "file  $file"
      tmp=$(mktemp -t "$filename.XXXXXX")
      if [ $? -ne 0 ]
      then
         echo "$(basename $0): cannot create temporary file" 1>&2
         exit 1
      fi
      expand < "$file" >> "$tmp"
      sed -e "s/$CR\$//g" -e 's/  *$//' < "$tmp" > "$file"
      rm "$tmp"
   else
      echo "SKIPP $file"
   fi
done

