#!/bin/bash
#------->Apaiteitai energopoiisi twn dialogs gia na treksei to script!!!<--------
#======================================FUNCTIONS=========================================================
#i function showAll emfanizei ta periexomena twn fakelwn pou exoun epilegei me mple fonto kai aspra #grammata

function showAll() {
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "--------------------------------------------------------------"
echo "---------------FIRST PATH-------------------------------------"
tragiko1=`cat ${arxikos}/first1.tmp`
for i in "${tragiko1[@]}"
do
 echo "$i"
done
echo ""
echo "--------------------------------------------------------------"
echo "---------------SECOND PATH------------------------------------"
tragiko2=`cat ${arxikos}/second1.tmp`
for k in "${tragiko2[@]}"
do
echo "$k"
done
echo "Press enter to continue"
read
}

function show() {
#Parousiasi twn onomatwn twn arxeiwn kai twn fakelwn pou periexontai stous fakelous pou exei epileksei o user
#Ginetai xrisi dialog me parametro fselect kai i parousiasi ginetai se ena 2 stiles, mia gia arxeia kai mia gia fakelous
dialog --fselect ${dir1}/ 20 40

dialog --fselect ${dir2}/ 20 40
}

function compare() {
#Sigrisi 2 arxeiwn me idio onoma

#Eisagwgi tou onomatos tou arxeiou pou epithimei o xristis
cd $dir1
choice=0
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp
choicetmp=`cat ${arxikos}/choice1.tmp`
#Elegxos gia tin iparksi tou arxeiou KAI STOUS 2 fakelous
if [ -s ${arxikos}/choice1.tmp ]; then
        if [ -f $choicetmp ]; then
                cd $dir2
                if [ -f $choicetmp ]; then
                        choice=1
                else
                        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
                        echo "File doesn't exist in dir2"
                        echo "Press enter to continue"
                        read
                fi
        else
                echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
                echo "File doesn't exist in dir1"
                echo "Press enter to continue"
                read
        fi
else
        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
        echo "Prepei na epilekseis kati!"
        echo "Press enter to continue"
        read
fi
done

#Xrisi awk gia sigrisi twn periexomenwn twn arxeiwn.  Me ti getline entos tis awk diavazontai mia mia oi grammes twn arxeiwn
#kai apothikevontai se metavlites oi opoies sigrinontai.  AN DEN ektelestei i getline simainei oti to arxeio eftase sto 
#telos (EOF)
awk -v file1=${dir1}/$choicetmp -v file2=${dir2}/$choicetmp 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'

#An ta arxeia einai OMOIA i awk dinei exit 0, diaforetika dinei 1.  Elegxoume tin eksodo kai emfanizoume to katallilo minima
var=$?
if [ $var -eq 0 ];then
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "Same"
else
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "Different"
fi
#echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                            
echo "Press enter to continue"
read
}

function compareD() {
#Diavazontai grammi grammi ta arxeia pou periexoun ta onomata twn arxeiwn pou periexontai stous fakelous pou epilextikan
#otan ena arxeio apo ton 1o fakelo dn iparxei ston 2o i metavliti countl afksanetai
#antistoixws afksanetai kai i countr
#I anagnwsi twn grammwn ilopoieitai me while read line 

#Arxikopoiisi metavlitwn
test=1
countl=0
#Diavasma grammwn apo ta arxeia firstnames.tmp kai secondnames.tmp.  I diadikasia ekteleitai 2 fores.  
#Mia fora gia elegxo twn arxeiwn tou aristerou katalogou me aftous tou deksiou kai mia gia to antistrofo.  
#I kathe grammi tou arxeiou, pou antistoixei sto onoma enos arxeiou, dinetai ws timi se mia metavliti.
#stin i gia to firstnames.tmp kai stin j gia to secondnames.tmp
        while read i; do
                while [ $test -eq 1 ] && read j; do
#Sigrisi i kai j
                        if [ "$i" = "$j" ];then
                                test=0                  
                        fi
                done < ${arxikos}/secondnames.tmp
                if [ $test -eq 1 ];then
                        countl=$[ $countl + 1 ]
                fi      
                test=1
        done < ${arxikos}/firstnames.tmp

test=1
countr=0
        while read i; do
                while [ $test -eq 1 ] && read j; do
                        if [ "$i" = "$j" ];then
                                test=0                  
                        fi
                done < ${arxikos}/firstnames.tmp
                if [ $test -eq 1 ];then
                        countr=$[ $countr + 1 ]
                fi      
                test=1
        done < ${arxikos}/secondnames.tmp

#Arxikopoiisi metavlitwn
countsame1=0
countdiff1=0
countsame2=0
countdiff2=0

#Elegxos arxeiwn, xrhsimopoieitai o tropos tis function compare().
#Simeiwsi: DEN elegxontai MONO ta arxeia me to idio onoma.  Elegxontai OLA ta arxeia kai sigrinontai ws pros to 
#PERIEXOMENO tous kai OXI to ONOMA tous.
#I diadikasia ekteleitai 2 fores

while read line1; do
        count1=0
        while read line2; do
                awk -v file1=${dir1}/$line1 -v file2=${dir2}/$line2 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'
        var=$?
        if [ $var -eq 0 ];then  
                count1=$[ $count1 + 1 ]
                break
        fi

        done < ${arxikos}/secondnames.tmp
        
        if [ $count1 -eq 0 ];then
                countdiff1=$[ $countdiff1 + 1 ]
        else
                countsame1=$[ $countsame1 + $count1 ]           
        fi
done < ${arxikos}/firstnames.tmp

while read line2; do
        count2=0
        while read line1; do
                awk -v file1=${dir1}/$line1 -v file2=${dir2}/$line2 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'
        var=$?
        if [ $var -eq 0 ];then          
                count2=$[ $count2 + 1 ]
                break
        fi
        done < ${arxikos}/firstnames.tmp
        if [ $count2 -eq 0 ];then
                countdiff2=$[ $countdiff2 + 1 ]
        else
                countsame2=$[ $countsame2 + $count2 ]           
        fi
done < ${arxikos}/secondnames.tmp

#Emfanisi apotelesmatwn me xrisi dialog msgbox
dialog --msgbox "Ston ARISTERO fakelo iparxoun \n    $countsame1 arxeia pou exoun idio periexomeno me ta arxeia tou deksi fakelou \n    $countdiff1 arxeia me diaforetiko periexomeno kai \n    $countl arxeia me monadiko onoma" 12 80

dialog --msgbox "Ston DEKSI fakelo iparxoun\n    $countsame2 arxeia pou exoun idio periexomeno me arxeia tou aristerou fakelou  \n    $countdiff2 arxeia me diaforetiko periexomeno kai \n    $countr arxeia me monadiko onoma" 12 80

}

function sortd() {
#I function sortd emfanizei ta periexomena twn fakelwn pu exei epileksi o xristis me tetoio tropo wste an stous dio fakelous
#iparxei ena arxeio me idio onoma, afto na emfanizetai stin idia grammi
#Ilopoiisi me awk
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
awk -v dirA=${dir1}/ -v dirb=${dir2}/ 'BEGIN { 
        for(i=1; ( " ls -l " dirA ) | getline LINE; i++ ) {
          split(LINE,f," "); 
#dimiourgountai pinakes gia ton prwto fakelo. O size1 exei to megethos tou arxeiou, o fname1 to onoma tou ktl
          size1[i]  =  f[5]; 
          date1[i]  =  f[6]; 
          fname1[i] =  f[8];
          present_in_dir1[f[8]] = i;
        } 

#omoiws gia ton deftero fakelo
        for(i=1; (" ls -l " dirb) | getline LINE; i++ ) {
          split(LINE,f," "); 

          fname2[i] =  f[8]; 
          size2[i]  =  f[5]; 
          date2[i]  =  f[6]; 
          present_in_dir2[f[8]] = i;
        } 

#Dimioourgountai pinakes pou periexoun ta koina arxeia twn 2 fakelwn kai ta monadika.  
        for(j in fname1) 
          if( present_in_dir2[fname1[j]] ) 
            common[fname1[j]] = 1;
          else
            only1[fname1[j]] = 1;
       
        for(j in fname2)
          if( !present_in_dir1[fname2[j]] )
            only2[fname2[j]] = 1;

#EMfanisi mono twn monadikwn arxeiwn tou aristerou fakelou
        for(fname in only1) { 
          i = present_in_dir1[fname];
          printf("%-20s%-10s%-10s\t\n", fname, size1[i], date1[i]);
        }

#Emfanisi twn koinwn fakelwn
        for(fname in common) {
          i = present_in_dir1[fname];
          j = present_in_dir2[fname];
          printf("%-20s%-10s%-10s\t\t%-20s%-10s%-10s\n", fname, size1[i], date1[i], fname, size2[j], date2[j]);
        }

#Emfanisi mono twn monadikwn arxeiwn tou deksiou fakelou
        for(fname in only2) {
          j = present_in_dir2[fname];
          printf("%-20s%-10s%-10s\t\t%-20s%-10s%-10s\n", "", "", "", fname, size2[j], date2[j]);
        }
#Molis teleiwnei i awk ta apotelesmata tis dinontai ws eisodo sti sort
    }' | sort 

echo "Press enter to continue"
read
}

function copylr() {
#i function copylr antigrafei ena arxeio apo ton aristero fakelo ston deksi

#arxikopoiisi metavlitwn
choice=0

#Epilogi arxeiou apo ton xristi
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp

retval=$?
if [ $retval -eq 0 ];then
choicetmp=`cat ${arxikos}/choice1.tmp`

#elegxos iparksis tou fakelou ston arxiko kai teliko katalogo
cd $dir1
if [ -f $choicetmp ]; then
        cd $dir2
        if [ -f $choicetmp ]; then
                echo "File alrady exists in right folder"
                echo "Press enter to continue"
                read
        else
                choice=1
        fi
else
        echo "File doesn't exist in left folder"
        echo "Press enter to continue"
        read
fi
fi
done

#an to arxeio iparxei ston arxiko fakelo kai DEN iparxei ston teliko, antigrafetai 
#An i antigrafi oloklirwthei epitixws parousiazetai to katallilo minima
if [ $choice -eq 1 ]; then
        cp ${dir1}/$choicetmp ${dir2}/

        retval=$?
        if [ $retval -eq 0 ]; then
                dialog --msgbox "cp ${dir1}/$choicetmp ${dir2}/ ... Done!" 5 80
        else
                echo "Error occured!"
        fi
fi
}

function copyrl() {
#I function copyrl antigrafei kapoio arxeio apo ton deksi ston aristero fakelo

#Arxikopoiisi metavlitwn
choice=0

#Idia diadikaslia me ti function copylr
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp

retval=$?
if [ $retval -eq 0 ];then
choicetmp=`cat ${arxikos}/choice1.tmp`
cd $dir2
if [ -f $choicetmp ]; then
        cd $dir1
        if [ -f $choicetmp ]; then
                echo "File alrady exists in left folder"
                echo "Press enter to continue"
                read
        else
                choice=1
        fi
else
        echo "File doesn't exist in right folder"
        echo "Press enter to continue"
        read
fi
fi
done

if [ $choice -eq 1 ]; then
cp ${dir2}/$choicetmp ${dir1}/
retval=$?
if [ $retval -eq 0 ]; then
        dialog --msgbox "cp ${dir2}/$choicetmp ${dir1}/ ... Done!" 5 80
else
        echo "Error occured!"
        echo "Press enter to continue"
        read
fi
fi
}

function sync() {
#I function sync parousiazei 3 epiloges ston xristi gia sigxronismo mono tou deskiou fakelou, sigxronismo mono tou aristerou
#kai sigxronismo kai twn dio fakelwn.  Kalei meta tin antistoixi sinartisi

dialog --title "Synchronization" \
        --menu "Choose one of the following" 20 75 10 \
                "SyncL" "Synchronize LEFT folder" \
                "SyncR" "Synchronize RIGHT folder" \
                "SyncA" "Synchronize BOTH folders" 2> ${arxikos}/syncchoice.tmp

retval=$?
if [ $retval -eq 0 ];then

        choice=`cat ${arxikos}/syncchoice.tmp`
        case $choice in
                SyncL)
                        syncl;;
                SyncR)
                        syncr;;
                SyncA)
                        synca;;
        esac
else
        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
        echo "Problem occured... "
        echo "Press enter to continue"
        read
fi

read
}

function syncl(){
#I sinaritisi syncl sigxronizei ton aristero fakelo. 
echo "Synchronization of left folder.  All files existing in right folder and NOT existing in left, will now be copied!"
#Arxikopoiisi metavlitwn
var1=`wc -l < ${arxikos}/firstnames.tmp`
var2=`wc -l < ${arxikos}/secondnames.tmp`
k=0
i=1

#Diavazontai oi grammes twn arxeiwn firstnames.tmp kai secondnames.tmp ta opoia periexoun ta onomata twn arxeiwn twn fakelwn
#pu exei epileksei o xristis
#Ginetai sigrisi kai osa arxeia den iparxoun antigrafontai
while [ $i -le $var2 ];
do
        line2=`sed -n ${i}p ${arxikos}/secondnames.tmp`
        j=1
        w=0
        while [ $j -le $var1 ];
        do
                line1=`sed -n ${i}p ${arxikos}/firstnames.tmp`
                if [ "$line1" = "$line2" ]; then
                        w=1                     
                fi
                j=$[ $j + 1 ]
        done

        if [ $w -eq 0 ];then
                k=$[ $k + 1 ]
                only2[$k]=$line2
        fi
        i=$[ $i + 1 ]
done 

i=1
for i in ${only2[@]};do
cp ${dir2}/$i ${dir1}
done

retval=$?
if [ $retval -eq 0 ];then
dialog --msgbox "Synchronization of LEFT folder is complete!" 5 80
fi

}


function syncr(){
#i leitourgia tis function einai idia me tin parapanw mono pou afti ti fora ginetai i antistrofi diadikasia etsi wstw na 
#sigxronistei o deksis fakelos
#I ilopoiisi tis function ginetai diaforetika, me xrisi tis entolis read gia tin anagnwsi twn grammwn twn arxeiwn.
var1=`wc -l < ${arxikos}/firstnames.tmp`
var2=`wc -l < ${arxikos}/secondnames.tmp`
k=0
cat ${arxikos}/firstnames.tmp | while read line1; do
        w=0
        cat ${arxikos}/secondnames.tmp | while read line2;do
                if [ $line1 == $line2 ];then
                        w=1                     
                fi      
        done
        if [ $w -eq 0 ];then
                k=$[ $k + 1 ]
                only1[$k]=$line1
        fi
done

for i in ${only2[@]};do 
cp ${dir2}/$i ${dir1} 
done && dialog --msgbox "Synchronization of LEFT folder is complete!" 5 80
}

function synca(){
#Kalountai oi functions syncl kai syncr etsi wste na epitefxthei o sigxronismos kai twn 2 katalogwn.
syncl
syncr
}
#====================================END OF FUNCTIONS==============================================

main(){
arxikos=`pwd`
dir1=0

#Epilogi fakelwn
while [ ! -d $dir1 ];do
dialog --title "Folder choice" --clear \
        --inputbox "Type the 1rst path:" 0 0 2> ${arxikos}/tempfile.tmp
retval=$?
case $retval in
  0)
    dir1=`cat ${arxikos}/tempfile.tmp`;;
  1)
    echo "Error1";;
 255)
      echo "Error2";;
esac
done

dir2=0
while [ ! -d $dir2 ];do
dialog --title "Folder choice" --clear \
        --inputbox "Type second path:" 0 0 2> ${arxikos}/tempfile2.tmp

retval=$?
case $retval in
  0)
    dir2=`cat ${arxikos}/tempfile2.tmp`;;
  1)
    echo "Error1";;
  255)
      echo "Error2";;
esac
done

#Eksagwgi twn stoixeiwn pou mas endiaferoun gia ta arxeia twn fakelwn
ls -lA $dir1 | grep ^- | awk '{print $5 "\t" $6 "\t" $7 "\t" $NF}' > ${arxikos}/first.tmp
ls -lA $dir2 | grep ^- | awk '{print $5 "\t" $6 "\t" $7 "\t" $NF}' > ${arxikos}/second.tmp

ls -lA $dir1 | grep ^- | sed 1d > ${arxikos}/first1.tmp
ls -lA $dir2 | grep ^- | sed 1d > ${arxikos}/second1.tmp

ls -l $dir1 | grep ^- | awk '{ print $NF }' > ${arxikos}/temp1.tmp
ls -l $dir2 | grep ^- | awk '{ print $NF }' > ${arxikos}/temp2.tmp

cat ${arxikos}/temp1.tmp > ${arxikos}/firstnames.tmp
cat ${arxikos}/temp2.tmp > ${arxikos}/secondnames.tmp

#Emfanisi epilogwn
loop=1
while [ $loop -eq 1 ]; do
dialog --clear --title "Epiloges:" \
        --menu "Choose what you want to do" 20 75 10 \
        "Show" "Shows the folders that have been selected(Only Namefiles)" \
        "ShowAll" "Shows the folders that have been selected(with file info)"\
        "Compare" "Compare your selected files" \
        "CompareD" "Compare directories" \
        "Sort" "Sort content of folders" \
        "CopyLR" "Select and copy file from left to right" \
        "CopyRL" "Select and copy file from right to left" \
        "Sync" "Synhronize" \
        "Pathch" "Changes paths entered in the beginning" \
        "Exit" "Exits program" 2> ${arxikos}/tempchoice.tmp

retval=$?

choice=`cat ${arxikos}/tempchoice.tmp`

case $retval in
  0)
    echo "You have chosen: '$choice'";;
  1)
    echo "Canceled!!";;
  255)
    echo "Exits... Problem ?";;
esac

if [ $retval -eq 0 ];then
        case $choice in 
                Compare)
                        compare;;
                CompareD)
                        compareD;;
                Sort)                   
                        sortd;;
                CopyLR)
                        copylr;;
                CopyRL)
                        copyrl;;
                Sync)
                        sync;;
                Show)
                        show;;
                ShowAll)
                        showAll;;
                Pathch) 
                        main;;
                Exit)
                        loop=0;;
        esac
else
loop=0
fi

done
}

#=====================================ARXI SCRIPT=========================================================
#Erwtisi gia dialogs, elegxos apantisis, an einai y kalei tin main, an einai n vgainei apo to script.  
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
dialog --yesno "Gia na treksei swsta afto to script tha prepei na exete energopoiimena ta dialogs!!" 7 60
#!/bin/bash
#------->Apaiteitai energopoiisi twn dialogs gia na treksei to script!!!<--------
#======================================FUNCTIONS=========================================================
#i function showAll emfanizei ta periexomena twn fakelwn pou exoun epilegei me mple fonto kai aspra #grammata

function showAll() {
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "--------------------------------------------------------------"
echo "---------------FIRST PATH-------------------------------------"
tragiko1=`cat ${arxikos}/first1.tmp`
for i in "${tragiko1[@]}"
do
 echo "$i"
done
echo ""
echo "--------------------------------------------------------------"
echo "---------------SECOND PATH------------------------------------"
tragiko2=`cat ${arxikos}/second1.tmp`
for k in "${tragiko2[@]}"
do
echo "$k"
done
echo "Press enter to continue"
read
}

function show() {
#Parousiasi twn onomatwn twn arxeiwn kai twn fakelwn pou periexontai stous fakelous pou exei epileksei o user
#Ginetai xrisi dialog me parametro fselect kai i parousiasi ginetai se ena 2 stiles, mia gia arxeia kai mia gia fakelous
dialog --fselect ${dir1}/ 20 40

dialog --fselect ${dir2}/ 20 40
}

function compare() {
#Sigrisi 2 arxeiwn me idio onoma

#Eisagwgi tou onomatos tou arxeiou pou epithimei o xristis
cd $dir1
choice=0
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp
choicetmp=`cat ${arxikos}/choice1.tmp`
#Elegxos gia tin iparksi tou arxeiou KAI STOUS 2 fakelous
if [ -s ${arxikos}/choice1.tmp ]; then
        if [ -f $choicetmp ]; then
                cd $dir2
                if [ -f $choicetmp ]; then
                        choice=1
                else
                        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
                        echo "File doesn't exist in dir2"
                        echo "Press enter to continue"
                        read
                fi
        else
                echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
                echo "File doesn't exist in dir1"
                echo "Press enter to continue"
                read
        fi
else
        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
        echo "Prepei na epilekseis kati!"
        echo "Press enter to continue"
        read
fi
done

#Xrisi awk gia sigrisi twn periexomenwn twn arxeiwn.  Me ti getline entos tis awk diavazontai mia mia oi grammes twn arxeiwn
#kai apothikevontai se metavlites oi opoies sigrinontai.  AN DEN ektelestei i getline simainei oti to arxeio eftase sto 
#telos (EOF)
awk -v file1=${dir1}/$choicetmp -v file2=${dir2}/$choicetmp 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'

#An ta arxeia einai OMOIA i awk dinei exit 0, diaforetika dinei 1.  Elegxoume tin eksodo kai emfanizoume to katallilo minima
var=$?
if [ $var -eq 0 ];then
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "Same"
else
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
echo "Different"
fi
#echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                            
echo "Press enter to continue"
read
}

function compareD() {
#Diavazontai grammi grammi ta arxeia pou periexoun ta onomata twn arxeiwn pou periexontai stous fakelous pou epilextikan
#otan ena arxeio apo ton 1o fakelo dn iparxei ston 2o i metavliti countl afksanetai
#antistoixws afksanetai kai i countr
#I anagnwsi twn grammwn ilopoieitai me while read line 

#Arxikopoiisi metavlitwn
test=1
countl=0
#Diavasma grammwn apo ta arxeia firstnames.tmp kai secondnames.tmp.  I diadikasia ekteleitai 2 fores.  
#Mia fora gia elegxo twn arxeiwn tou aristerou katalogou me aftous tou deksiou kai mia gia to antistrofo.  
#I kathe grammi tou arxeiou, pou antistoixei sto onoma enos arxeiou, dinetai ws timi se mia metavliti.
#stin i gia to firstnames.tmp kai stin j gia to secondnames.tmp
        while read i; do
                while [ $test -eq 1 ] && read j; do
#Sigrisi i kai j
                        if [ "$i" = "$j" ];then
                                test=0                  
                        fi
                done < ${arxikos}/secondnames.tmp
                if [ $test -eq 1 ];then
                        countl=$[ $countl + 1 ]
                fi      
                test=1
        done < ${arxikos}/firstnames.tmp

test=1
countr=0
        while read i; do
                while [ $test -eq 1 ] && read j; do
                        if [ "$i" = "$j" ];then
                                test=0                  
                        fi
                done < ${arxikos}/firstnames.tmp
                if [ $test -eq 1 ];then
                        countr=$[ $countr + 1 ]
                fi      
                test=1
        done < ${arxikos}/secondnames.tmp

#Arxikopoiisi metavlitwn
countsame1=0
countdiff1=0
countsame2=0
countdiff2=0

#Elegxos arxeiwn, xrhsimopoieitai o tropos tis function compare().
#Simeiwsi: DEN elegxontai MONO ta arxeia me to idio onoma.  Elegxontai OLA ta arxeia kai sigrinontai ws pros to 
#PERIEXOMENO tous kai OXI to ONOMA tous.
#I diadikasia ekteleitai 2 fores

while read line1; do
        count1=0
        while read line2; do
                awk -v file1=${dir1}/$line1 -v file2=${dir2}/$line2 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'
        var=$?
        if [ $var -eq 0 ];then  
                count1=$[ $count1 + 1 ]
                break
        fi

        done < ${arxikos}/secondnames.tmp
        
        if [ $count1 -eq 0 ];then
                countdiff1=$[ $countdiff1 + 1 ]
        else
                countsame1=$[ $countsame1 + $count1 ]           
        fi
done < ${arxikos}/firstnames.tmp

while read line2; do
        count2=0
        while read line1; do
                awk -v file1=${dir1}/$line1 -v file2=${dir2}/$line2 'BEGIN{

        while(line1 == line2) {
          if(!getline line1 < file1) eof1 = 1;
          if(!getline line2 < file2) eof2 = 1;
          if( eof1 || eof2 ) break;
        }
        
        if(eof1 && eof2)
        {
                exit 0;
        }
        else
        {
                exit 1;
        }

  }'
        var=$?
        if [ $var -eq 0 ];then          
                count2=$[ $count2 + 1 ]
                break
        fi
        done < ${arxikos}/firstnames.tmp
        if [ $count2 -eq 0 ];then
                countdiff2=$[ $countdiff2 + 1 ]
        else
                countsame2=$[ $countsame2 + $count2 ]           
        fi
done < ${arxikos}/secondnames.tmp

#Emfanisi apotelesmatwn me xrisi dialog msgbox
dialog --msgbox "Ston ARISTERO fakelo iparxoun \n    $countsame1 arxeia pou exoun idio periexomeno me ta arxeia tou deksi fakelou \n    $countdiff1 arxeia me diaforetiko periexomeno kai \n    $countl arxeia me monadiko onoma" 12 80

dialog --msgbox "Ston DEKSI fakelo iparxoun\n    $countsame2 arxeia pou exoun idio periexomeno me arxeia tou aristerou fakelou  \n    $countdiff2 arxeia me diaforetiko periexomeno kai \n    $countr arxeia me monadiko onoma" 12 80

}

function sortd() {
#I function sortd emfanizei ta periexomena twn fakelwn pu exei epileksi o xristis me tetoio tropo wste an stous dio fakelous
#iparxei ena arxeio me idio onoma, afto na emfanizetai stin idia grammi
#Ilopoiisi me awk
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
awk -v dirA=${dir1}/ -v dirb=${dir2}/ 'BEGIN { 
        for(i=1; ( " ls -l " dirA ) | getline LINE; i++ ) {
          split(LINE,f," "); 
#dimiourgountai pinakes gia ton prwto fakelo. O size1 exei to megethos tou arxeiou, o fname1 to onoma tou ktl
          size1[i]  =  f[5]; 
          date1[i]  =  f[6]; 
          fname1[i] =  f[8];
          present_in_dir1[f[8]] = i;
        } 

#omoiws gia ton deftero fakelo
        for(i=1; (" ls -l " dirb) | getline LINE; i++ ) {
          split(LINE,f," "); 

          fname2[i] =  f[8]; 
          size2[i]  =  f[5]; 
          date2[i]  =  f[6]; 
          present_in_dir2[f[8]] = i;
        } 

#Dimioourgountai pinakes pou periexoun ta koina arxeia twn 2 fakelwn kai ta monadika.  
        for(j in fname1) 
          if( present_in_dir2[fname1[j]] ) 
            common[fname1[j]] = 1;
          else
            only1[fname1[j]] = 1;
       
        for(j in fname2)
          if( !present_in_dir1[fname2[j]] )
            only2[fname2[j]] = 1;

#EMfanisi mono twn monadikwn arxeiwn tou aristerou fakelou
        for(fname in only1) { 
          i = present_in_dir1[fname];
          printf("%-20s%-10s%-10s\t\n", fname, size1[i], date1[i]);
        }

#Emfanisi twn koinwn fakelwn
        for(fname in common) {
          i = present_in_dir1[fname];
          j = present_in_dir2[fname];
          printf("%-20s%-10s%-10s\t\t%-20s%-10s%-10s\n", fname, size1[i], date1[i], fname, size2[j], date2[j]);
        }

#Emfanisi mono twn monadikwn arxeiwn tou deksiou fakelou
        for(fname in only2) {
          j = present_in_dir2[fname];
          printf("%-20s%-10s%-10s\t\t%-20s%-10s%-10s\n", "", "", "", fname, size2[j], date2[j]);
        }
#Molis teleiwnei i awk ta apotelesmata tis dinontai ws eisodo sti sort
    }' | sort 

echo "Press enter to continue"
read
}

function copylr() {
#i function copylr antigrafei ena arxeio apo ton aristero fakelo ston deksi

#arxikopoiisi metavlitwn
choice=0

#Epilogi arxeiou apo ton xristi
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp

retval=$?
if [ $retval -eq 0 ];then
choicetmp=`cat ${arxikos}/choice1.tmp`

#elegxos iparksis tou fakelou ston arxiko kai teliko katalogo
cd $dir1
if [ -f $choicetmp ]; then
        cd $dir2
        if [ -f $choicetmp ]; then
                echo "File alrady exists in right folder"
                echo "Press enter to continue"
                read
        else
                choice=1
        fi
else
        echo "File doesn't exist in left folder"
        echo "Press enter to continue"
        read
fi
fi
done

#an to arxeio iparxei ston arxiko fakelo kai DEN iparxei ston teliko, antigrafetai 
#An i antigrafi oloklirwthei epitixws parousiazetai to katallilo minima
if [ $choice -eq 1 ]; then
        cp ${dir1}/$choicetmp ${dir2}/

        retval=$?
        if [ $retval -eq 0 ]; then
                dialog --msgbox "cp ${dir1}/$choicetmp ${dir2}/ ... Done!" 5 80
        else
                echo "Error occured!"
        fi
fi
}

function copyrl() {
#I function copyrl antigrafei kapoio arxeio apo ton deksi ston aristero fakelo

#Arxikopoiisi metavlitwn
choice=0

#Idia diadikaslia me ti function copylr
while [ $choice -eq 0 ];do
dialog --title "Choose file" --clear \
        --inputbox "Type name of file:" 0 0 2> ${arxikos}/choice1.tmp

retval=$?
if [ $retval -eq 0 ];then
choicetmp=`cat ${arxikos}/choice1.tmp`
cd $dir2
if [ -f $choicetmp ]; then
        cd $dir1
        if [ -f $choicetmp ]; then
                echo "File alrady exists in left folder"
                echo "Press enter to continue"
                read
        else
                choice=1
        fi
else
        echo "File doesn't exist in right folder"
        echo "Press enter to continue"
        read
fi
fi
done

if [ $choice -eq 1 ]; then
cp ${dir2}/$choicetmp ${dir1}/
retval=$?
if [ $retval -eq 0 ]; then
        dialog --msgbox "cp ${dir2}/$choicetmp ${dir1}/ ... Done!" 5 80
else
        echo "Error occured!"
        echo "Press enter to continue"
        read
fi
fi
}

function sync() {
#I function sync parousiazei 3 epiloges ston xristi gia sigxronismo mono tou deskiou fakelou, sigxronismo mono tou aristerou
#kai sigxronismo kai twn dio fakelwn.  Kalei meta tin antistoixi sinartisi

dialog --title "Synchronization" \
        --menu "Choose one of the following" 20 75 10 \
                "SyncL" "Synchronize LEFT folder" \
                "SyncR" "Synchronize RIGHT folder" \
                "SyncA" "Synchronize BOTH folders" 2> ${arxikos}/syncchoice.tmp

retval=$?
if [ $retval -eq 0 ];then

        choice=`cat ${arxikos}/syncchoice.tmp`
        case $choice in
                SyncL)
                        syncl;;
                SyncR)
                        syncr;;
                SyncA)
                        synca;;
        esac
else
        echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
        echo "Problem occured... "
        echo "Press enter to continue"
        read
fi

read
}

function syncl(){
#I sinaritisi syncl sigxronizei ton aristero fakelo. 
echo "Synchronization of left folder.  All files existing in right folder and NOT existing in left, will now be copied!"
#Arxikopoiisi metavlitwn
var1=`wc -l < ${arxikos}/firstnames.tmp`
var2=`wc -l < ${arxikos}/secondnames.tmp`
k=0
i=1

#Diavazontai oi grammes twn arxeiwn firstnames.tmp kai secondnames.tmp ta opoia periexoun ta onomata twn arxeiwn twn fakelwn
#pu exei epileksei o xristis
#Ginetai sigrisi kai osa arxeia den iparxoun antigrafontai
while [ $i -le $var2 ];
do
        line2=`sed -n ${i}p ${arxikos}/secondnames.tmp`
        j=1
        w=0
        while [ $j -le $var1 ];
        do
                line1=`sed -n ${i}p ${arxikos}/firstnames.tmp`
                if [ "$line1" = "$line2" ]; then
                        w=1                     
                fi
                j=$[ $j + 1 ]
        done

        if [ $w -eq 0 ];then
                k=$[ $k + 1 ]
                only2[$k]=$line2
        fi
        i=$[ $i + 1 ]
done 

i=1
for i in ${only2[@]};do
cp ${dir2}/$i ${dir1}
done

retval=$?
if [ $retval -eq 0 ];then
dialog --msgbox "Synchronization of LEFT folder is complete!" 5 80
fi

}


function syncr(){
#i leitourgia tis function einai idia me tin parapanw mono pou afti ti fora ginetai i antistrofi diadikasia etsi wstw na 
#sigxronistei o deksis fakelos
#I ilopoiisi tis function ginetai diaforetika, me xrisi tis entolis read gia tin anagnwsi twn grammwn twn arxeiwn.
var1=`wc -l < ${arxikos}/firstnames.tmp`
var2=`wc -l < ${arxikos}/secondnames.tmp`
k=0
cat ${arxikos}/firstnames.tmp | while read line1; do
        w=0
        cat ${arxikos}/secondnames.tmp | while read line2;do
                if [ $line1 == $line2 ];then
                        w=1                     
                fi      
        done
        if [ $w -eq 0 ];then
                k=$[ $k + 1 ]
                only1[$k]=$line1
        fi
done

for i in ${only2[@]};do 
cp ${dir2}/$i ${dir1} 
done && dialog --msgbox "Synchronization of LEFT folder is complete!" 5 80
}

function synca(){
#Kalountai oi functions syncl kai syncr etsi wste na epitefxthei o sigxronismos kai twn 2 katalogwn.
syncl
syncr
}
#====================================END OF FUNCTIONS==============================================

main(){
arxikos=`pwd`
dir1=0

#Epilogi fakelwn
while [ ! -d $dir1 ];do
dialog --title "Folder choice" --clear \
        --inputbox "Type the 1rst path:" 0 0 2> ${arxikos}/tempfile.tmp
retval=$?
case $retval in
  0)
    dir1=`cat ${arxikos}/tempfile.tmp`;;
  1)
    echo "Error1";;
 255)
      echo "Error2";;
esac
done

dir2=0
while [ ! -d $dir2 ];do
dialog --title "Folder choice" --clear \
        --inputbox "Type second path:" 0 0 2> ${arxikos}/tempfile2.tmp

retval=$?
case $retval in
  0)
    dir2=`cat ${arxikos}/tempfile2.tmp`;;
  1)
    echo "Error1";;
  255)
      echo "Error2";;
esac
done

#Eksagwgi twn stoixeiwn pou mas endiaferoun gia ta arxeia twn fakelwn
ls -lA $dir1 | grep ^- | awk '{print $5 "\t" $6 "\t" $7 "\t" $NF}' > ${arxikos}/first.tmp
ls -lA $dir2 | grep ^- | awk '{print $5 "\t" $6 "\t" $7 "\t" $NF}' > ${arxikos}/second.tmp

ls -lA $dir1 | grep ^- | sed 1d > ${arxikos}/first1.tmp
ls -lA $dir2 | grep ^- | sed 1d > ${arxikos}/second1.tmp

ls -l $dir1 | grep ^- | awk '{ print $NF }' > ${arxikos}/temp1.tmp
ls -l $dir2 | grep ^- | awk '{ print $NF }' > ${arxikos}/temp2.tmp

cat ${arxikos}/temp1.tmp > ${arxikos}/firstnames.tmp
cat ${arxikos}/temp2.tmp > ${arxikos}/secondnames.tmp

#Emfanisi epilogwn
loop=1
while [ $loop -eq 1 ]; do
dialog --clear --title "Epiloges:" \
        --menu "Choose what you want to do" 20 75 10 \
        "Show" "Shows the folders that have been selected(Only Namefiles)" \
        "ShowAll" "Shows the folders that have been selected(with file info)"\
        "Compare" "Compare your selected files" \
        "CompareD" "Compare directories" \
        "Sort" "Sort content of folders" \
        "CopyLR" "Select and copy file from left to right" \
        "CopyRL" "Select and copy file from right to left" \
        "Sync" "Synhronize" \
        "Pathch" "Changes paths entered in the beginning" \
        "Exit" "Exits program" 2> ${arxikos}/tempchoice.tmp

retval=$?

choice=`cat ${arxikos}/tempchoice.tmp`

case $retval in
  0)
    echo "You have chosen: '$choice'";;
  1)
    echo "Canceled!!";;
  255)
    echo "Exits... Problem ?";;
esac

if [ $retval -eq 0 ];then
        case $choice in 
                Compare)
                        compare;;
                CompareD)
                        compareD;;
                Sort)                   
                        sortd;;
                CopyLR)
                        copylr;;
                CopyRL)
                        copyrl;;
                Sync)
                        sync;;
                Show)
                        show;;
                ShowAll)
                        showAll;;
                Pathch) 
                        main;;
                Exit)
                        loop=0;;
        esac
else
loop=0
fi

done
}

#=====================================ARXI SCRIPT=========================================================
#Erwtisi gia dialogs, elegxos apantisis, an einai y kalei tin main, an einai n vgainei apo to script.  
echo -ne "\e[44m\e[37m\e[2J\e[2;0f"                             
dialog --yesno "Gia na treksei swsta afto to script tha prepei na exete energopoiimena ta dialogs!!" 7 60

retval=$?
if [ $retval -eq 0 ];then
        main    
elif [ $retval -eq 1 ];then
        exit
else
        echo "Program occured"
fi
retval=$?
if [ $retval -eq 0 ];then
        main    
elif [ $retval -eq 1 ];then
        exit
else
        echo "Program occured"
fi