#!/bin/bash
# A script to generate woof-CE patches against a running puppy (run without
# any arguments)  or the puppy.sfs (run with the 'sfs' argument).

setvar Version = '0.7'

source /etc/DISTRO_SPECS
source /etc/rc.d/PUPSTATE

# Check if we are good to run
if test $(which git) = "" {
 Xdialog --title "Error" --msgbox \
 "Please install the devx sfs or just git from the repo." 0 0
 exit 1
}
setvar WDIR = $(pwd) # Should be just above your freshly cloned woof-CE git
if test ! -d $WDIR/woof-CE {
 # Check if script was started by "dropping" a file on it with ROX
 setvar SCRIPT_PATH = "$(realpath $0)"
 if test ${SCRIPT_PATH%/woof-CE/ztools/patch-generator.sh} != $SCRIPT_PATH -a $1 != '' {
  setvar WDIR = "${SCRIPT_PATH%/woof-CE/ztools/patch-generator.sh}"
  cd $WDIR
 } else {
  Xdialog --title "Error" --msgbox \
  "This script should be in the same folder as the woof-CE git folder" 0 0
  exit 1
 }
}
setvar GIT_BRANCH = $(cut -f3 -d'/' $WDIR/woof-CE/.git/HEAD)
if test ! $GIT_BRANCH {
 Xdialog --title "Error" --msgbox \
 "The woof-CE folder is not a git repo. Please clone the woof-CE git" 0 0
 exit 1
}
if test $GIT_BRANCH != "testing" -a $GIT_BRANCH != "rationalise" {
 Xdialog --title "Error" --msgbox \
 "Your woof-CE repo is not in the testing or rationalise branch. Please run 'git checkout testing' or 'git checkout rationalise'" 0 0
 exit 1
}
if test $GIT_BRANCH = "testing" {
 setvar GIT_HEAD = $(cut -c 1-6 $WDIR/woof-CE/.git/refs/heads/testing)
 setvar REMOTE_HEAD = $(git ls-remote https://github.com/puppylinux-woof-CE/woof-CE.git |
 grep 'refs/heads/testing' | cut -c 1-6)
} elif test $GIT_BRANCH = "rationalise" {
 setvar GIT_HEAD = $(cut -c 1-6 $WDIR/woof-CE/.git/refs/heads/rationalise)
 setvar REMOTE_HEAD = $(git ls-remote https://github.com/puppylinux-woof-CE/woof-CE.git |
 grep 'refs/heads/rationalise' | cut -c 1-6)
}
if test $REMOTE_HEAD != $GIT_HEAD {
 Xdialog --title "Error" --msgbox \
 "Your local repo is not in sysnc with the remote. Please run 'git pull --all'" 0 0
 exit 1
}

if test $1 = "sfs" -a $PUPMODE = "2" {
 Xdialog --title "Error" --msgbox \
 "There is no pup.sfs in full installs. Please run without the 'sfs' argument" 0 0
 exit 1
}
# Make the patches
test $BUILD_FROM_WOOF && setvar FROMWOOF = "_$(echo $BUILD_FROM_WOOF | cut -f 2 -d ';' | cut -c 1-6)"
case (1) {
 sfs { setvar COMP_DIR = "/initrd/pup_ro2"
      setvar PATCHES = "woof_"$GIT_BRANCH""$GIT_HEAD"_patches_to_"$DISTRO_FILE_PREFIX"-"$DISTRO_VERSION"_sfs"$FROMWOOF""
 }
 '' { setvar COMP_DIR = ''
    setvar PATCHES = "woof_"$GIT_BRANCH""$GIT_HEAD"_patches_to_"$DISTRO_FILE_PREFIX"-"$DISTRO_VERSION""$FROMWOOF""
 }
 * { if test ! -f $1 {
     echo "No such file: $1"
     Xdialog --title "Error" --msgbox "No such file: $1" 0 0
     exit 1
    } elif test $(expr $1 : '.*\(woof-out\)) = '' {
     echo "File must exist in woof-out_*"
     Xdialog --title "Error" --msgbox "File must exist in woof-out_*" 0 0
     exit 1
    }
    mkdir -p $WDIR/patches
    cd $WDIR/woof-CE/woof-code
    setvar WOOFOUT_DIR = "$(expr $1 : '.*\(woof-out[^/]*\))"
    setvar FILE = "$(expr $1 : '.*woof-out[^/]*/\(.*\))"
    echo "Making patch from $FILE"
    diff -u -N $FILE ../../$WOOFOUT_DIR/$FILE > $WDIR/patches/$(basename $FILE).patch
    cd $WDIR
    Xdialog --title "Finished" --msgbox "Made patch from $FILE \n \
It can be found in \n \
$WDIR/patches/" 0 0
    exit
 }
}
mkdir -p $WDIR/$PATCHES
rm -rf $WDIR/$PATCHES/*
setvar DIRS = 'rootfs-skeleton'
for i in $(ls woof-CE/woof-code/rootfs-packages/)
{
 setvar DIRS = ""$DIRS rootfs-packages/$i""
}
for D in $DIRS
{
 setvar FOLDER = "woof-CE/woof-code/$D"
 cd $WDIR/$FOLDER
 for f in $(find ./ -type f)
 {
 test -f $COMP_DIR/$f && test $(diff -u -N $f $COMP_DIR/$f) != "" \
  && diff -u -N $f $COMP_DIR/$f > $WDIR/$PATCHES/$(basename $f).patch
 }
 #exit 0 #Uncomment to check if compared correctly
 cd $WDIR/$PATCHES
 setvar DS = "$(echo $D | sed 's/\//\\\//')"
 sed -i "s/\-\-\-\ \.\//\-\-\-\ a\/woof\-code\/$DS\//" *
 if test $1 = "sfs" {
  sed -i "s/\+\+\+\ \/initrd\/pup_ro2\/\.\//\+\+\+\ b\/woof\-code\/$DS\//" *
 } else {
  sed -i "s/\+\+\+\ \/\.\//\+\+\+\ b\/woof\-code\/$DS\//" *
 }
}
# check init
setvar INITPATH = $(echo "$PUPSFS" | cut -f 2 -d '/' | grep -v sfs)
mkdir -p /tmp/initfs
cd /tmp/initfs
gunzip -c /mnt/home/$INITPATH/initrd.gz | cpio -i
cd $WDIR/woof-CE
diff -u initrd-progs/0initrd/init /tmp/initfs/init > $WDIR/$PATCHES/init.patch
sed -i "s/\-\-\-\ /\-\-\-\ a\//" $WDIR/$PATCHES/init.patch
sed -i "s/\+\+\+\ \/tmp\/initfs/\+\+\+\ b\/initrd\-progs\/0initrd/" $WDIR/$PATCHES/init.patch
rm -rf /tmp/initfs
# Move some patches to folders for easy reviewing
cd $WDIR/$PATCHES
mkdir binary_files SVGs defaults docs running desktop
for f in $( grep ^Binary * | cut -f 1 -d':')
{
  mv $f binary_files/
}
mv *.svg.patch SVGs/
mv default*.patch defaults/
mv *.rules.patch running/
mv {PUPSTATE,BOOTCONFIG,clock,current_month,issue,hosts,hostname,ld.so.conf,resolv.conf,profile}.patch running/
mv *.{htm,html,css,txt}.patch docs/
mv *.html.*.patch docs/
mv *.desktop.patch desktop/
rm -f messages.patch
cd $WDIR

 exit 0 #Uncomment to review if patches are OK FOR woof-CE
rm -f $WDIR/$PATCHES.tar.gz
tar cvzf $PATCHES.tar.gz $PATCHES/
exit 0