# This file is part of the src2pkg program:
# Copyright 2005-2013 Gilbert Ashley <amigo@ibilio.org>
# src2pkg is released under the GNU General Public License Version 2

### fix_source_perms
fix_source_perms() {
if [[ $ALLOW_USER_EXTENSIONS = "YES" ]] ; then
 # check if the user has any pre-execution extensions to this file and run them, if so.
 [[ -f "$HOME"/.src2pkg/extensions/05.pre ]] && . "$HOME"/.src2pkg/extensions/05.pre
fi
if [[ "$FAILED" = "" ]] && [[ ! $USER_CANCELLED ]] ; then
  if [[ ! $RESUME ]] || [[ $RESUME = $FUNCNAME ]] || [[ $HAVE_RESUMED ]] ; then
	[[ $RESUME = $FUNCNAME ]] && HAVE_RESUMED=1 && echo $CYAN"Notice - "$BLUE"Resuming build at: "$NORMAL "$FUNCNAME"
	if [[ $PAUSE = "BEFORE" ]] || [[ $PAUSE = $FUNCNAME ]] ; then
		echo $MAGENTA"Notice - "$BLUE"Pausing before: "$NORMAL" '$FUNCNAME'  Press ENTER to continue"
		read
	fi
	
	if [[ "$CORRECT_PERMS" = "YES" ]] ; then
		echo -n $BLUE"Correcting source permissions - "$NORMAL
		if [ "$(whoami)" = "root" ] ; then
			chown -R root:root "$SRC_DIR"
		else
			chown -R $OWNER:$GROUP "$SRC_DIR"
		fi
		cd "$SRC_DIR"
		find . -type d -exec chmod 755 {} \;
		find . -perm 400 -exec chmod 644 {} \; -o -perm 440 -exec chmod 644 {} \; -o -perm 444 -exec chmod 644 {} \; \
			-o -perm 600 -exec chmod 644 {} \; -o -perm 664  -exec chmod 644 {} \; -perm 666  -exec chmod 644 {} \; \
			-o -perm 511 -exec chmod 755 {} \; -o -perm 555 -exec chmod 755 {} \; \
			-o -perm 711 -exec chmod 755 {} \; -o -perm 754 -exec chmod 755 {} \; \
			-o -perm 774 -exec chmod 755 {} \; -o -perm 775 -exec chmod 755 {} \; -o -perm 777 -exec chmod 755 {} \;
		if ! [[ $CORRECT_SUID_PERMS = "NO" ]] ; then 
			find . -perm 2775 -exec chmod 755 {} \; -o -perm 2777 -exec chmod 755 {} \; \
			-o -perm 2755 -exec chmod 755 {} \;
		fi
		echo $GREEN"Done"$NORMAL
	fi
	
	# Auto-Patching  works for most patches, but patch -p option and application order 
	# can be controlled by renaming the files. patches should be in the CWD or CWD/patches directory
	# we are patching from the toplevel of SRC_DIR which may not work for some patches at all...
	apply_patches() {
		echo $BLUE"Applying:$NORMAL $(basename $p)"
		PATCH_HAS_FAILED=0
		cd "$SRC_DIR"
		for PATCH_OPTIONS in -p1 -p0 -p2 -p3 -p4 -p5 ; do
			[[ $DEBUG ]] && echo -n $CYAN"   Notice - "$NORMAL"Trying with option $PATCH_OPTIONS "
			cat /dev/null > "$SRC_DIR"/patchlist.this
			case $p in
			*.gz) #echo "   Applying: $(basename $p)" ;
				if [[ $(file "$PATCHES_DIR"/$p |grep compressed) != "" ]] ; then
				  zcat "$PATCHES_DIR"/$p | patch  "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
				else
				  # Some Debian patches are plain-text patches named with a .gz suffix to avoid corruption
				  cat "$PATCHES_DIR"/$p | patch  "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
				fi
			;;
			*.bz2) #echo "   Applying: $(basename $p)" ;
				bzcat "$PATCHES_DIR"/$p | patch "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
			;;
			*.dpatch) #echo "   Applying: $(basename $p)" ;
				patch "$PATCH_OPTIONS" -f -l < "$PATCHES_DIR"/$p >> "$SRC_DIR"/patchlist.this
			;;
			*.lzma) #echo "   Applying: $(basename $p)" ;
				lzcat "$PATCHES_DIR"/$p | patch "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
			;;
			*.xz) #echo "   Applying: $(basename $p)" ;
				xzcat "$PATCHES_DIR"/$p | patch "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
			;;
			*.patch|*.diff) #echo "   Applying: $(basename $p)" ;
				cat "$PATCHES_DIR"/$p | patch  "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
				# patch "$PATCH_OPTIONS" -l < "$PATCHES_DIR"/$p >> "$SRC_DIR"/patchlist.this
			;;
			*) #echo "   Applying: $(basename $p)" ;
				cat "$PATCHES_DIR"/$p | patch  "$PATCH_OPTIONS" -l >> "$SRC_DIR"/patchlist.this
				# patch "$PATCH_OPTIONS" -l < "$PATCHES_DIR"/$p >> "$SRC_DIR"/patchlist.this
			;;
			esac
			if [ $? -ne 0 ] ; then
				rm -f "$SRC_DIR"/patchlist.this
				PATCH_HAS_FAILED=1
				[[ $DEBUG ]] && echo $YELLOW"Failed"$NORMAL
				continue
			else
				PATCH_HAS_FAILED=0
				[[ $DEBUG ]] && echo $GREEN"Okay"$NORMAL
				break
			fi
		done
		
		if [[ $PATCH_HAS_FAILED = 1 ]] ; then
			if [[ $EXIT_ON_PATCH_FAILURE = "YES" ]] ; then
				echo $RED"Failed! "$NORMAL"Application of $p has failed. Exiting..."
				FAILED="PATCH_FAILURE"
				post_process
			else
				echo $YELLOW"Warning! "$NORMAL"Possible failure in patch: $p"
			fi
		fi
		
		# clean up the list of files patched with this patch
		if [[ -f "$SRC_DIR"/patchlist.this ]] ; then 
		  cat "$SRC_DIR"/patchlist.this| cut -f3 -d' ' | sort | uniq >  "$SRC_DIR"/patched-files.this
		  rm -f "$SRC_DIR"/patchlist.this
		fi
		
		if [[ $DEBUG ]] && [[ -f "$SRC_DIR"/patched-files.this ]] ; then
		  echo $BLUE"Files Patched:"$NORMAL
		  cat "$SRC_DIR"/patched-files.this |grep -v succeeded
		fi
		# write the entry for this patch to the main patched-files list
		echo $p >> "$SRC_DIR"/$NAME-patched-files.log
		echo ${p##*/} >> "$SRC_DIR"/$NAME-patches-applied.log
		if [[ -f "$SRC_DIR"/patched-files.this ]] ; then 
		  cat "$SRC_DIR"/patched-files.this |grep -v succeeded >> "$SRC_DIR"/$NAME-patched-files.log
		fi
		echo "" >> "$SRC_DIR"/$NAME-patched-files.log
		rm -f "$SRC_DIR"/patched-files.this
	}
	
	if [[ $PATCHLIST ]] && [[ ! "$AUTO_PATCH" = "NO" ]] ; then
		echo $BLUE"Applying patches - "$NORMAL"from User-Supplied PATCHLIST"
		cat /dev/null > "$SRC_DIR"/$NAME-patched-files.log
		for p in $PATCHLIST ; do
			apply_patches ;
		done
		PATCHES_APPLIED=1
	elif [[ ! "$AUTO_PATCH" = "NO" ]] ; then
		echo -n $BLUE"Checking for patches - "$NORMAL
		get_patch_list
		if [[ "$PATCHLIST" = "" ]] ; then
			echo "None found"
		else
			echo "Found"
			cat /dev/null > "$SRC_DIR"/$NAME-patched-files.log
			for p in $PATCHLIST ; do
				apply_patches ;
			done
			PATCHES_APPLIED=1
		fi
	fi
	
	if [[ "$AUTO_PATCH" != "NO" ]] ; then
		for SEARCH_PATH in $SOURCES_DIR $PATCHES_DIR/$NAME-patches $CWD ; do
			for FILE in ${NAME}_${VERSION}*.debian.tar.gz ${NAME}_${VERSION}*.debian.tar.bz2 ; do
				if [[ -f $SEARCH_PATH/$FILE ]] ; then
					echo $BLUE"Found 'debianize' tarball - "$NORMAL"Unpacking inside SRC_DIR"
					( cd $SRC_DIR && tar -xvf $SEARCH_PATH/$FILE >> $NAME-patched-files.log )
					PATCHES_APPLIED=1
					DONE=1
				fi
				case $DONE in '1') break ;; esac
			done
		done
	fi
	
	if [[ $PATCHES_APPLIED ]] ; then
		# check the patchlist for keyfiles to determine whether to run autoreconf
		#if [[ "$(cat $SRC_DIR/$NAME-patched-files.log |grep -w configure |egrep -v '(configure.ac|configure.in|Makefile.am|Makefile.in)' | grep -v '#')" != "" ]] ; then
		#if [[ $(cat "$SRC_DIR"/$NAME-patched-files.log |grep -w configure |egrep -v '(configure.ac|configure.in|Makefile.am)' | egrep -v '(^#|configure.*)') != "" ]] ; then
		if [[ $(cat "$SRC_DIR"/$NAME-patched-files.log |egrep -w 'configure$') != "" ]] ; then 
			true
		# elif [[ "$(cat $SRC_DIR/$NAME-patched-files.log |egrep '(configure.ac|configure.in|Makefile.am|Makefile.in)' | grep -v '#')" != "" ]] ; then
		elif [[ $(cat "$SRC_DIR"/$NAME-patched-files.log |egrep '(configure.ac|configure.in|Makefile.am)' | grep -v '^#') != "" ]] ; then
				SHOULD_AUTORECONF=1
		fi
		if [[ $DEBUG_FILELISTS ]] ; then
			cat "$SRC_DIR"/$NAME-patched-files.log >> "$CWD"/$NAME-patched-files.log
		fi
	fi
	
	if [[ $PAUSE = "AFTER" ]] || [[ $PAUSE = $FUNCNAME ]] ; then
		echo $MAGENTA"Notice - "$BLUE"Pausing after: "$NORMAL" '$FUNCNAME'  Press ENTER to continue"
		read
	fi
  fi
fi

if [[ $ALLOW_USER_EXTENSIONS = "YES" ]] ; then
 # check if the user has any post-execution extensions to this file and run them, if so.
 [[ -f "$HOME"/.src2pkg/extensions/05.post ]] && . "$HOME"/.src2pkg/extensions/05.post
fi

}
# end fix_source_perms

get_patch_list() {
cd "$CWD" ;
if [[ ! -d "$CWD"/patches ]] && [[ ! -d "$CWD"/$NAME-patches ]] && [[ ! -d "$PATCHES_DIR"/$NAME-patches ]] && [[ ! -d "$PATCHES_DIR"/$NAME ]]; then
	# if there are patches in the CWD use them
	PATCHLIST=$(find * -maxdepth 0 -type f -name '*.patch*' -o -name '*.diff*' -o -name '*.dpatch' | sort)
elif [[ -d "$CWD"/patches ]] ; then
	# otherwise if there are patches in the CWD/patches use them
	PATCHLIST=$(find patches -maxdepth 1 -type f -name '*.patch*' -o -name '*.diff*' -o -name '*.dpatch' | sort)
elif [[ -d "$CWD"/$NAME-patches ]] ; then
	# otherwise if there are patches in the CWD/patches use them
	PATCHLIST=$(find $NAME-patches -maxdepth 1 -type f -name '*.patch*' -o -name '*.diff*' -o -name '*.dpatch' | sort)
elif [[ -d "$PATCHES_DIR"/$NAME-patches ]] ; then
	cd "$PATCHES_DIR" ;
	# otherwise if there are patches in the CWD/patches use them
	PATCHLIST=$(find $NAME-patches -maxdepth 1 -type f -name '*.patch*' -o -name '*.diff*' -o -name '*.dpatch' | sort)
elif [[ -d "$PATCHES_DIR/$NAME" ]] ; then
	cd "$PATCHES_DIR" ;
	# otherwise if there are patches in the CWD/patches use them
	PATCHLIST=$(find $NAME -maxdepth 1 -type f -name '*.patch*' -o -name '*.diff*' -o -name '*.dpatch' | sort)
fi
}
