#!/bin/bash

# findns - find non stripped executables
# Copyright © 2000-2009 by Pádraig Brady <P@draigBrady.com>.
#
# This 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 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details,
# which is available at www.gnu.org


# Notes:
# Because of limitiation of $(file), files
# with : in name will not be processed correctly
# Also note, don't sort by size before $(file)
# since there will be more seeking over disk and
# therefore will be much slower.

script_dir=$(dirname "$0")              #directory of this script
script_dir=$(readlink -f "$script_dir") #Make sure absolute path

. "$script_dir"/supprt/fslver

Usage() {
	ProgName=$(basename "$0")
	echo "find NonStripped executables.
Usage: $ProgName [[-r] [-f] paths(s) ...]

If no path(s) specified then the PATH is searched."
	exit
}

if [ $# -eq "0" ] #Nothing on cmdline means search binary directories
then
	eval set -- $(. "$script_dir"/supprt/getffp)
	FPF="%p"
else
	for arg
	do
		case "$arg" in
		-h|--help|-help)
			Usage ;;
		-v|--version)
			Version ;;
		*)
			argsToPassOn="$argsToPassOn $(shell_quote "$arg")" ;;
		esac
	done
	. "$script_dir"/supprt/getfpf "$argsToPassOn"
fi

find "$@" -type f -perm +111 ! -name "*$LF*" -printf "%i\t$FPF\n" |
sort -u |          #merge files (indirectly) specified multiple times
#sorting by inodes runs in 17s for my 2250 binaries
#compared to 20s without
sort -k1,1n | #sort by inodes
cut -f2- |
tr '\n' '\0' |
xargs -r0 file |
# This assumes ':' not in any executable descriptions
sed -n 's/\(.*\):[^:]*executable.* not stripped*/\1/p' |
if [ -p /proc/self/fd/1 ]; then
    cat
else
    tr '\n' '\0' |
    xargs -r0 ls -Ulb -- |
    sort -b -k5,5n
fi
