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