#!/bin/bash
########################################################
#                    MkPatch                           # 
#  Simple gui bash program useful for file patching.   #
#                                                      #
#------------------Requirements------------------------#
#                  yad diffutils                       #
#        Copyright (c) 2019 lxgator @gmail.com  	   #
#							                           #
#   Licensed under the GNU GENERAL PUBLIC LICENSE 3    #
########################################################

ICON='/usr/share/mkpatch/mkpatch.png'
OICON='/usr/share/mkpatch/orig.png'
PICON='/usr/share/mkpatch/patch.png'
PFLD="$HOME/Desktop/mkpatch"

rm -rf "$PFLD" 2>/dev/null
orig_fld=$(yad --window-icon=$ICON --image=$OICON --on-top --dnd --fixed --title "Original Folder" --button=Cancel!/usr/share/mkpatch/cancel.svg:1 --button=Ok!/usr/share/mkpatch/ok.svg:0) 
if [ $? = 1 ]; then
rm -rf "$PFLD" 2>/dev/null
exit 0
else
if ! test -d "$PFLD"; then
mkdir -p "$PFLD"
fi
OF=$(echo $orig_fld | awk '{ print $0 }' | sed  's .\{7\}  ')
cp -r "$OF" "$PFLD" 2>/dev/null
fi
if [ "${orig_fld}" == "" ]; then
rm -rf "$PFLD" 2>/dev/null   
`yad --error --width=350 --window-icon=$ICON --title="Original Error" --text="\n<b>No original file or folder was selected</b>\n\n\
Please choose the original file or folder to use." --button=Ok!/usr/share/mkpatch/ok.svg:0`
rm -rf "$PFLD" 2>/dev/null
exit 1&
exec '/usr/bin/mkpatch' 
fi
patc_fld=$(yad --window-icon=$ICON --image=$PICON --on-top --dnd --fixed --title "Patched Folder" --button=Cancel!/usr/share/mkpatch/cancel.svg:1 --button=Ok!/usr/share/mkpatch/ok.svg:0) 
if [ $? = 1 ]; then
rm -rf "$PFLD" 2>/dev/null
exit 0
else
PF=$(echo $patc_fld | awk '{ print $0 }' | sed  's .\{7\}  ')
cp -r "$PF" "$PFLD" 2>/dev/null
fi
if [ "${patc_fld}" == "" ]; then    
`yad --error --width=350 --window-icon=$ICON --title="Patched Error" --text="\n<b>No patched file or folder was selected</b>\n\nRestarting the Mkpatch Program..." --button=Ok!/usr/share/mkpatch/ok.svg:0`
rm -rf "$PFLD" 2>/dev/null
exit 1&
sleep 1
exec '/usr/bin/mkpatch' 
fi
short_orig_fld=$(echo $orig_fld | sed -e 's#.*/##g')
orig_fld=$(echo $orig_fld | awk '{ print $0 }' | sed  's .\{7\}  ' > $PFLD/.tmp) 
patc_fld=$(echo $patc_fld | awk '{ print $0 }' | sed  's .\{7\}  ' > $PFLD/.tmp1) 

OR=$(cat $PFLD/.tmp | sed 's@.*/@@') 
PD=$(cat $PFLD/.tmp1 | sed 's@.*/@@')

cd "$PFLD"
diff -uNr "$OR" "$PD" > "$PFLD/$short_orig_fld.patch"
sleep 0.50
cd "$PFLD"
    ENTRY=$(yad --width=375 --height=115 --window-icon=$ICON --on-top --center \
        --form --title="MkPatch" \
        --field="Name" "$short_orig_fld.patch" \
        --text="\n  Optional renaming of patched file or folder" \
--button=About!/usr/share/mkpatch/about.svg:3 --button=Cancel!/usr/share/mkpatch/cancel.svg:1 --button=Save!/usr/share/mkpatch/save.svg:0)
if [ "$?" = "3" ]; then
`yad --width=450 --height=100 --center --title='MkPatch' \
--text='\<b>                      MkPatch </b> 0.2.2\n\nSimple gui, bash program useful for file patching.\n\n                © 2019 lxgator@gmail.com \n\nThis program is free software; you can redistribute it 
and/or modify it under the terms of the GNU General 
Public License as published by the Free Software 
Foundation; either version 3 of the License, or (at
your option) any later version..' \
--window-icon=$ICON \
--image=$ICON \
--button=Cancel!/usr/share/mkpatch/cancel.svg:4`
exec "/usr/bin/mkpatch"
   fi
    RET=$?

    OIFS=$IFS 
    IFS="|" # separator is "|" in returned choices
    i=0
    retChoice=()

    for ret in $ENTRY;do 
        retChoice[$i]="$ret"
        i=$(($i+1))
    done

    IFS=$OIFS 
    RENAME=${retChoice[0]}
    
    if (( $RET == 1 ));then     # Cancelled
        exit
    fi
if [[ -z $RENAME ]] ;then
exec
    else
ORIG=(`echo "$short_orig_fld.patch"`)
if [[ -e $ORIG ]]; then
mv "$ORIG" "$RENAME".patch
sleep 0.50
cd "$PFLD"
mv "$RENAME".patch "$RENAME"
sleep 0.50
xdg-open "$PFLD"
fi
 fi
sleep 0.50
xdg-open "$PFLD"
rm -f $PFLD/.tmp* 2>/dev/null 

