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