#!/bin/bash

SET_PERMISSIONS=yes
MAINTAIN_TIMESTAMP=yes
DO_STRIP=yes
COMMENTS=
VERBOSE=no

usage() {
  echo "pkgbuild_postprocess [options] [files...]"
  echo 
  echo "Postprocess the list or directories (recursively): set sane"
  echo "permissions, strip binaries, replace .comments in ELF binaries"
  echo
  echo "  -v, --verbose"
  echo "        verbose mode"
  echo "  -c comment, --comment comment"
  echo "        Set the ELF .comments to comment.  The default is to"
  echo "        remove the comments."
  echo "  -p, --noperm"
  echo "        Do not change permissions. The default is to set 755 for"
  echo "        directories, libraries and executables, a+r for other files."
  echo "  -s, --nostrip"
  echo "        Do not strip symbolic information from ELF binaries"
  echo "        The default is to run \"strip -x\"."
  echo "  -m, --touch"
  echo "        Change the file modification timestamp for the changed files"
  echo "        The default is to maintain the original timestamp."
  echo "  -h, --help"
  echo "        Display this help"
}

set_elf_comments () {
  if [ $VERBOSE = yes ]; then
      echo "Setting ELF comments in binary $1"
  fi
  /usr/ccs/bin/mcs -d $1
  if [ -n "$COMMENTS" ]; then
      /usr/ccs/bin/mcs -a "$COMMENTS" $1
  fi
}

set_permissions () {
  if [ -d $1 ]; then
    chmod a+X,u+w,go-w $1
  fi
  if [ $FILE_IS_ELF = yes -a -f $1 ]; then
    case "$1" in
    *.so | *.so.* )
      chmod a+rx,u+w,go-w $1
      ;;
    esac
  fi
}

check_if_elf () {
    test -d "$1" && return 1
    test -L "$1" && return 1
    test -b "$1" && return 1
    test -c "$1" && return 1
    test -S "$1" && return 1
    test -f "$1" || return 1
    /usr/bin/file $1 2>/dev/null | /usr/bin/grep "ELF " > /dev/null 2>&1 \
	&& return 0
    return 1
}

process_file () {
    TIMESTAMP_FILE=/tmp/.pkgbuild-postprocess.timestamp.$$
    
    check_if_elf $1 && FILE_IS_ELF=yes || FILE_IS_ELF=no
    
    if [ $MAINTAIN_TIMESTAMP = yes ]; then
	touch -r $1 $TIMESTAMP_FILE
    fi
    
    if [ $SET_PERMISSIONS = yes ]; then
	set_permissions $1
    fi
    
    if [ $DO_STRIP = yes -a $FILE_IS_ELF = yes ]; then
	/usr/ccs/bin/strip -x $1
    fi
    
    if [ $FILE_IS_ELF = yes ]; then
	set_elf_comments $1
    fi
    
    if [ $MAINTAIN_TIMESTAMP = yes ]; then
      touch -r $TIMESTAMP_FILE $1
      rm -f $TIMESTAMP_FILE
    fi

    if [ -d $1 ]; then
	if [ $VERBOSE = yes ]; then
	    echo "Processing directory $1"
	fi
	for f in $1/*; do
	    process_file "$f"
	done
    fi
}

# main

while [ $# -gt 0 ]; do
    case "$1" in
	-- )
	    shift
	    break
	    ;;
	-c|--comment )
	    shift
	    if [ $# -lt 1 ]; then
		echo "$0: option $1 requires an argument" 1>&2
		echo
		usage
		exit 1
	    fi
	    COMMENTS="$1"
	    ;;
	-p|--noperm )
	    SET_PERMISSIONS=no
	    ;;
	-s|--nostrip )
	    DO_STRIP=no
	    ;;
	-m|--touch )
	    MAINTAIN_TIMESTAMP=no
	    ;;
	-h|--help )
	    usage
	    exit 0
	    ;;
	-v|--verbose )
	    VERBOSE=yes
	    ;;
	-* )
	    echo "$0: $1: invalid option."
	    echo
	    usage
	    exit 1
	    ;;
	* )
	    break
    esac
    shift
done

for file in "${@}"; do
    process_file $file
done

exit 0
