#! /bin/bash
  
echo "###################    count   #####################"

COUNT="$1/count"
timestamp=$(date "+%Y-%m-%d %H:%M:%S")

if [ ! -f "$COUNT" ]; then
        echo "$timestamp || task count=1" > "$COUNT"
        echo "start new project"
        current_count=$(tail -n 1 "$COUNT" | cut -d"=" -f2)
else
        before_count=$(tail -n 1 "$COUNT" | cut -d"=" -f2)
        echo "$before_count"
        current_count=$((before_count + 1))
        echo "$current_count"
        echo "$timestamp || task count=$current_count" >> "$COUNT"
fi



echo "####################################################"
echo "###################              ###################"
echo "###################   git pull   ###################"
echo "###################              ###################"
echo "####################################################"


for repo_path in $(find $1 -maxdepth 1 -type d)
do
    if [ -d "$repo_path/.git" ]; then
        echo "Pulling changes for repository: $repo_path"
        cd $repo_path
        git pull
        cd ../..
    fi
done

echo "####################################################"
echo "##############                      ################"
echo "##############     preparation      ################"
echo "##############                      ################"
echo "####################################################"


touch $1/utf-8
touch $1/list
touch $1/result
touch $1/iso
touch $1/us-ascii
touch $1/except




UTF="$1/utf-8"
ASCII="$1/us-ascii"
ISO="$1/iso"
LIST="$1/list"
RESULT="$1/result"
EXCEPT="$1/except"
LOG="$1/except-log"
ORIGIN="$1/original-encoding"

if [ ! -f "$LOG" ]; then
        touch $1/except-log
fi


if [ ! -f "$ORIGIN" ]; then
        touch "$ORIGIN"
fi


> "$EXCEPT"
> "$ASCII"
> "$ISO"
> "$LIST"
> "$RESULT"
> "$UTF"

echo "####################################################"
echo "##############                        ##############"
echo "##############  make a LIST of files  ##############"
echo "##############                        ##############"
echo "####################################################"


find /PATH/$1 -type f -name '*.pas'> "$LIST"
#grep -vE 'F[0-9A-Za-z].pas' > "$LIST" 
#| grep -E 'U([1-9]|10)\.pas'> "$LIST"

echo "####################################################"
echo "##############                        ##############"
echo "##############    classfy encoding    ##############"
echo "##############                        ##############"
echo "####################################################"


if [ "$current_count" == "1" ]; then
        while IFS= read -r cases; do
                encoding=$(file -ib "$cases" | cut -d'=' -f2)
                echo "$timestamp :: $files :: $encoding" >> $ORIGIN
        done < "$LIST"
fi

while IFS= read -r files; do
        fileset=$(file -ib "$files")
        encoding=$(file -ib "$files" | cut -d'=' -f2)
        if [ "$encoding" == "utf-8" ]; then
            echo "$files" >> "$UTF"
        elif [ "$encoding" == "us-ascii" ]; then
            echo "$files" >> "$ASCII"
        elif [ "$encoding" == "iso-8859-1" ]; then
            echo "$files" >> "$ISO"
        else
            echo "$files" >> "$EXCEPT"
        fi
done < "$LIST"
echo ""
echo ""
echo "####################################################"
echo "#############                           ############"
echo "############# convert us-ascii to UTF-8 ############"
echo "#############                           ############"
echo "####################################################"
echo ""
echo ""
while IFS= read -r line; do
        iconv -f "us-ascii" -t UTF-8 "$line" >  tempfile && mv -f tempfile "$line"
        echo "$line ::: done" >> "$RESULT"
done < "$ASCII"

echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"

echo ""
echo ""
echo "####################################################"
echo "############                             ###########"
echo "############ convert iso-8859-1 to UTF-8 ###########" 
echo "############                             ###########"
echo "####################################################"
while IFS= read -r line; do
        iconv -f "iso-8859-1" -t UTF-8 "$line" >  tempfile && mv -f tempfile "$line"
        echo "$line ::: done" >> "$RESULT"
done < "$ISO"

echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"
echo "##################################" >> "$RESULT"

echo ""
echo ""
echo "####################################################"
echo "###########                             ############"
echo "###########      reporting result       ############"
echo "###########                             ############"
echo "####################################################"
while IFS= read -r files; do
        fileset=$(file -ib "$files")
        encoding=$(file -ib "$files" | cut -d'=' -f2)
            echo "$files - $fileset" >> "$RESULT"
done < "$LIST"
echo ""
echo ""
echo "####################################################"
echo "###########                             ############"
echo "###########      set exception file     ############" 
echo "###########                             ############"
echo "####################################################"                                                                                                                                                       
while IFS= read -r exclus; do
        except=$(echo $exclus | cut -d' ' -f1)
        mv "$except" "$except-except"

        echo "$timestamp $exclus" >> "$LOG"
done < "$EXCEPT"
echo ""
echo ""
echo "####################################################"
echo "###########                             ############"
echo "###########   encoding conversion zip   ############"
echo "###########                             ############"
echo "####################################################"

zip -r ./$1.zip ./$1
echo ""
echo ""
echo "####################################################"
echo "###########                             ############"
echo "###########             done            ############"
echo "###########                             ############"
echo "####################################################"

+ Recent posts