scripts/unify-sources.sh
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 13 Jul 2008 16:46:56 +0100
changeset 208 4317d277ba21
parent 38 scripts/update-copyright-header.sh@a0cd9917c5a2
child 336 e05633b02e40
child 538 a76f55d7d397
permissions -rwxr-xr-x
Better source unifier

- now it is called scripts/unify-sources.sh
- replaces each tab with 8 spaces
- remove trailing spaces (and tabs)
- warnings on long lines (i.e. on lines that are more than 80 characters)
- the standard file header now turns off the space-to-tab replacement in Emacs
     1 #!/bin/bash
     2 
     3 YEAR=`date +2003-%Y`
     4 HGROOT=`hg root`
     5 
     6 function update_header() {
     7     TMP_FILE=`mktemp`
     8     FILE_NAME=$1
     9 
    10     (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
    11  *
    12  * This file is a part of LEMON, a generic C++ optimization library.
    13  *
    14  * Copyright (C) "$YEAR"
    15  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    16  * (Egervary Research Group on Combinatorial Optimization, EGRES).
    17  *
    18  * Permission to use, modify and distribute this software is granted
    19  * provided that this copyright notice appears in all copies. For
    20  * precise terms see the accompanying LICENSE file.
    21  *
    22  * This software is provided \"AS IS\" with no warranty of any kind,
    23  * express or implied, and with no claim as to its suitability for any
    24  * purpose.
    25  *
    26  */
    27 "
    28 	awk 'BEGIN { pm=0; }
    29      pm==3 { print }
    30      /\/\* / && pm==0 { pm=1;}
    31      /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
    32      /\*\// && pm==1 { pm=2;}
    33     ' $1
    34 	) >$TMP_FILE
    35 
    36     HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    37 
    38     rm $FILE_NAME
    39     mv $TMP_FILE $FILE_NAME
    40 }
    41 
    42 function update_tabs() {
    43     TMP_FILE=`mktemp`
    44     FILE_NAME=$1
    45 
    46     cat $1 |
    47     sed -e 's/\t/        /g' >$TMP_FILE
    48 
    49     TABS_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    50 
    51     rm $FILE_NAME
    52     mv $TMP_FILE $FILE_NAME
    53 }
    54 
    55 function remove_trailing_space() {
    56     TMP_FILE=`mktemp`
    57     FILE_NAME=$1
    58 
    59     cat $1 |
    60     sed -e 's/ \+$//g' >$TMP_FILE
    61 
    62     SPACES_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    63 
    64     rm $FILE_NAME
    65     mv $TMP_FILE $FILE_NAME
    66 }
    67 
    68 function long_line_test() {
    69     cat $1 |grep -q -E '.{81,}'
    70 }
    71 
    72 function update_file() {
    73     echo -n '    update' $i ...
    74 
    75     update_header $1
    76     update_tabs $1
    77     remove_trailing_space $1
    78 
    79     CHANGED=NO;
    80     if [[ $HEADER_CH = YES ]];
    81     then
    82 	echo -n '  [header updated]'
    83 	CHANGED=YES;
    84     fi
    85     if [[ $TABS_CH = YES ]];
    86     then
    87 	echo -n ' [tabs removed]'
    88 	CHANGED=YES;
    89     fi
    90     if [[ $SPACES_CH = YES ]];
    91     then
    92 	echo -n ' [trailing spaces removed]'
    93 	CHANGED=YES;
    94     fi
    95     if long_line_test $1 ;
    96     then
    97 	echo -n ' [LONG LINES]'
    98 	((LONG_LINE_FILES++))
    99     fi
   100     echo
   101     if [[ $CHANGED = YES ]];
   102     then
   103 	((CHANGED_FILES++))
   104     fi
   105 }
   106 
   107 CHANGED_FILES=0
   108 TOTAL_FILES=0
   109 LONG_LINE_FILES=0
   110 if [ $# == 0 ]; then
   111     echo Update all source files...
   112     for i in `hg manifest|grep -E  '\.(cc|h|dox)$'`
   113     do
   114 	update_file $HGROOT/$i
   115 	((TOTAL_FILES++))
   116     done
   117     echo '  done.'
   118 else
   119     for i in $*
   120     do
   121 	update_file $i
   122 	((TOTAL_FILES++))
   123     done
   124 fi
   125 echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
   126 if [[ $LONG_LINE_FILES -gt 1 ]]; then
   127     echo
   128     echo WARNING: $LONG_LINE_FILES files contains long lines!    
   129     echo
   130 elif [[ $LONG_LINE_FILES -gt 0 ]]; then
   131     echo
   132     echo WARNING: a file contains long lines!
   133     echo
   134 fi