#!bash
# vim:ff=unix:enc=utf8:ts=3:sw=3:et

# src2pkg-ng - package creation toolkit
# Copyright (C) 2005-2009 Gilbert Ashley
# Copyright (C) 2009 Timothy Goya

# src2pkg-ng is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2 as 
# published by the Free Software Foundation

# src2pkg-ng 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.

# You should have received a copy of the GNU General Public License
# along with src2pkg-ng.  If not, see <http://www.gnu.org/licenses/>.

{
   shopt -s nullglob
   declare HANDLER
   for HANDLER in "$SRC2PKG_NG_DIR/pkg"/* ; do
      source $HANDLER
   done
   shopt -u nullglob
}

fixup_pkg() {
   declare PKG_DIR="$1"

   if (( FIX_DOC )) ; then
      fix_doc "$PKG_DIR"
   fi

   if (( FIX_MAN )) ; then
      fix_man "$PKG_DIR"
   fi

   if (( FIX_INFO )) ; then
      fix_info "$PKG_DIR"
   fi

   if (( COMPRESS_DOCS )) ; then
      compress_docs "$PKG_DIR"
   fi

   if (( FIX_NOARCH )) ; then
      fix_noarch "$PKG_DIR"
   fi
   
   if [[ "$ARCH" != "noarch" ]] && (( STRIP_ELF )) ; then
      strip_elf "$PKG_DIR"
   fi

   if (( FIX_PKG_PERMS )) ; then
      fixup_pkg_perms "$PKG_DIR"
   fi
}

merge_dirs() {
   declare PKG_DIR="$1"
   declare DIR="$2"
   declare MERGE_DIR
   for MERGE_DIR in "$@" ; do
      if [[ -L "$PKG_DIR/$DIR" ]] ; then
         rm -f "$PKG_DIR/$MERGE_DIR"
      fi
   done
   mkdir -p "$PKG_DIR/$DIR"
   declare MERGE_DIR
   for MERGE_DIR in "$@" ; do
      if [[ -d "$PKG_DIR/$MERGE_DIR" && "$DIR" != "$MERGE_DIR" ]] ; then
         cp -r "$PKG_DIR/$MERGE_DIR"/* "$PKG_DIR/$DIR"
         rm -rf "$PKG_DIR/$MERGE_DIR"
      fi
   done
}

fix_doc() {
   declare PKG_DIR="$1"
   declare -a DOC_DIRS
   push_back DOC_DIRS "usr/share/doc/$NAME-$VERSION"
   push_back DOC_DIRS "usr/doc/$NAME-$VERSION"
   tprintf $"@summary{Fixing doc dirs} - "
   merge_dirs "$PKG_DIR" "$DOC_DIR" "${DOC_DIRS[@]}"
   tprintf $"@success{Done}\n"
}

fix_man() {
   declare PKG_DIR="$1"
   declare -a MAN_DIRS
   push_back MAN_DIRS "usr/share/man"
   push_back MAN_DIRS "usr/man"
   tprintf $"@summary{Fixing man dirs} - "
   merge_dirs "$PKG_DIR" "$MAN_DIR" "${MAN_DIRS[@]}"
   find "$PKG_DIR/$MAN_DIR" -type d -name "cat*" -depth -exec rm -rf "{}" "+"
   tprintf $"@success{Done}\n"
}

fix_info() {
   declare PKG_DIR="$1"
   declare -a INFO_DIRS
   push_back INFO_DIRS "usr/share/info"
   push_back INFO_DIRS "usr/info"
   tprintf $"@summary{Fixing info dirs} - "
   merge_dirs "$PKG_DIR" "$INFO_DIR" "${INFO_DIRS[@]}"
   tprintf $"@success{Done}\n"
}

compress_docs() {
   declare PKG_DIR="$1"
   declare MANS=($(find "$PKG_DIR/$MAN_DIR" -type f))
   if (( ${#MANS[@]} )) ; then
      tprintf $"@summary{Compressing man pages} - "
      declare MAN
      for MAN in "${MANS[@]}" ; do
         chmod 644 "$MAN"
         case "$MAN" in
            *.gz)
               gzip -d "$MAN"
               MAN=${MAN%.gz}
            ;;
            *.bz2)
               bzip2 -d "$MAN"
               MAN=${MAN%.bz2}
            ;;
            *.lzma)
               lzma -d "$MAN"
               MAN=${MAN%.lzma}
            ;;
            *.xz)
               xz -d "$MAN"
               MAN=${MAN%.xz}
            ;;
         esac
         gzip --best "$MAN"
      done
      tprintf $"@success{Done}\n"
   fi
   declare INFOS=($(find "$PKG_DIR/$INFO_DIR" -type f))
   if (( ${#INFOS[@]} )) ; then
      tprintf $"@summary{Compressing info pages} - "
      declare INFO
      for INFO in "${INFOS[@]}" ; do
         chmod 644 "$INFO"
         case "$INFO" in
            *.gz)
               gzip -d "$INFO"
               INFO=${INFO%.gz}
            ;;
            *.bz2)
               bzip2 -d "$INFO"
               INFO=${INFO%.bz2}
            ;;
            *.lzma)
               lzma -d "$INFO"
               INFO=${INFO%.lzma}
            ;;
            *.xz)
               xz -d "$INFO"
               INFO=${INFO%.xz}
            ;;
         esac
         gzip --best "$INFO"
      done
      tprintf $"@success{Done}\n"
   fi
}

fix_noarch() {
   declare PKG_DIR="$1"
   tprintf $"@summary{Checking if package is architecture independent} - "
   declare NOARCH=1
   declare FILE
   for FILE in $(find "$PKG_DIR/$BIN_DIR" -type f 2> /dev/null) $(find "$PKG_DIR/$SBIN_DIR" -type f 2> /dev/null) $(find "$PKG_DIR/$LIBEXEC_DIR" -type f 2> /dev/null) $(find "$PKG_DIR/$LIB_DIR" -type f 2> /dev/null) $(find "$PKG_DIR" -type f 2> /dev/null) ; do
      if [[ "$(dd if="$FILE" bs=1 count=4 2> /dev/null)" = $'\x7FELF' ]] ; then
         NOARCH=0
         break
      elif dd if="$FILE" bs=1 count=80 2> /dev/null | grep -q ".la - a libtool library file" ; then
         NOARCH=0
         break
      elif [[ "$(head -n 1 "$FILE")" == "!<arch>" ]] ; then
         NOARCH=0
         break
      fi
   done
   if (( NOARCH )) ; then
      ARCH=noarch
      echo $"it is"
   else
      echo $"it isn't"
   fi
}

strip_elf() {
   declare PKG_DIR="$1"
   tprintf $"@summary{Stripping ELF files} - "
   declare FILE
   for FILE in $(find "$PKG_DIR" -type f) ; do
      IFS=$' \n\t'
      if [[ "$(dd if="$FILE" bs=1 count=4 2> /dev/null)" = $'\x7FELF' ]] ; then
         $STRIP_CMD "$FILE"
      elif [[ "$(head -n 1 "$FILE")" = "!<arch>" ]] ; then
         $STRIP_CMD "$FILE"
      fi
      IFS=$'\n'
   done
   tprintf $"@success{Done}\n"
}

fixup_pkg_perms() {
   declare PKG_DIR="$1"
   tprintf $"@summary{Correcting package permissions} - "
   chown "$OWNER:$GROUP" "$PKG_DIR"
   if [[ -d "$PKG_DIR/tmp" ]] ; then
      chmod 1777 "$PKG_DIR/tmp"
   fi
   if [[ -d "$PKG_DIR/var/tmp" ]] ; then
      chmod 1777 "$PKG_DIR/var/tmp"
   fi
   if [[ -d "$PKG_DIR/var/lock" ]] ; then
      chmod 1777 "$PKG_DIR/var/lock"
   fi
   if [[ -d "$PKG_DIR/var/run" ]] ; then
      chmod 775 "$PKG_DIR/var/run"
   fi
   declare -a BIN_DIRS
   push_back BIN_DIRS "bin"
   push_back BIN_DIRS "sbin"
   push_back BIN_DIRS "usr/bin"
   push_back BIN_DIRS "usr/sbin"
   push_back BIN_DIRS "usr/local/bin"
   push_back BIN_DIRS "usr/local/sbin"
   push_back BIN_DIRS "usr/X11R6/bin"
   declare DIR
   for DIR in "${BIN_DIRS[@]}" ; do
      if [[ -d "$PKG_DIR/$DIR" ]] ; then
         chown "$OWNER:$GROUP" "$PKG_DIR/$DIR"
         chmod -R 755 "$PKG_DIR/$DIR"
      fi
   done
   declare -a LIB_DIRS
   push_back LIB_DIRS "lib"
   push_back LIB_DIRS "usr/lib"
   push_back LIB_DIRS "usr/local/lib"
   push_back LIB_DIRS "usr/X11/lib"
   push_back LIB_DIRS "opt/kde/lib"
   declare DIR
   for DIR in "${LIB_DIRS[@]}" ; do
      if [[ -d "$PKG_DIR/$DIR" ]] ; then
         chown -R "$OWNER:$GROUP" "$PKG_DIR/$DIR"
         chmod 755 "$PKG_DIR/$DIR"
         find "$PKG_DIR/$DIR" -type d -exec chown "$OWNER:$GROUP" "{}" "+"
         find "$PKG_DIR/$DIR" -type d -exec chmod 755 "{}" "+"
      fi
   done
   declare FILE
   for FILE in $(find "$PKG_DIR" -type f) ; do
      if [[ "$(dd if="$FILE" bs=1 count=4 2> /dev/null)" = $'\x7FELF' ]] ; then
         chmod 755 "$FILE"
      elif dd if="$FILE" bs=1 count=80 2> /dev/null | grep -q ".la - a libtool library file" ; then
         chmod 644 "$FILE"
      elif [[ "$(head -n 1 "$FILE")" == "!<arch>" ]] ; then
         chmod 644 "$FILE"
      fi
   done
   if [[ -d "$PKG_DIR/$SYSCONF_DIR" ]] ; then
      find "$PKG_DIR/$SYSCONF_DIR" -maxdepth 1 -type f -exec chmod "ogu-x" "{}" "+"
   fi
   if [[ -d "$PKG_DIR/$DOC_DIR" ]] ; then
      chmod 755 "$PKG_DIR/$DOC_DIR"
      find "$PKG_DIR/$DOC_DIR" -type d -exec chmod 755 "{}" "+"
      find "$PKG_DIR/$DOC_DIR" -type f -exec chmod 644 "{}" "+"
   fi
   if [[ -d "$PKG_DIR/$DATA_DIR" ]] ; then
      chmod 755 "$PKG_DIR/$DATA_DIR"
      find "$PKG_DIR/$DATA_DIR" -type d -a ! -name "$NAME" -exec chmod 755 "{}" "+"
      if [[ -d "$PKG_DIR/$DATA_DIR/applications" ]] ; then
         chown -R "$OWNER:$GROUP" "$PKG_DIR/$DATA_DIR/applications"
         find "$PKG_DIR/$DATA_DIR/applications" -type f -exec chmod 644 "{}" "+"
      fi
   fi
   if [[ -d "$PKG_DIR/$DATA_DIR/pixmaps" ]] ; then
      chown -R "$OWNER:$GROUP" "$PKG_DIR/$DATA_DIR/pixmaps"
      find "$PKG_DIR/$DATA_DIR/pixmaps" -type d -exec chmod 755 "{}" "+"
      find "$PKG_DIR/$DATA_DIR/pixmaps" -type f -exec chmod 644 "{}" "+"
   fi
   if [[ -d "$PKG_DIR/$DATA_DIR/icons" ]] ; then
      chown -R "$OWNER:$GROUP" "$PKG_DIR/$DATA_DIR/icons"
      find "$PKG_DIR/$DATA_DIR/icons" -type d -exec chmod 755 "{}" "+"
      find "$PKG_DIR/$DATA_DIR/icons" -type f -exec chmod 644 "{}" "+"
   fi
   if [[ -d "$PKG_DIR/$LIB_DIR/pkgconfig" ]] ; then
      chown -R "$OWNER:$GROUP" "$PKG_DIR/$LIB_DIR"
      chmod 755 "$PKG_DIR/$LIB_DIR"
      chmod 755 "$PKG_DIR/$LIB_DIR/pkgconfig"
      find "$PKG_DIR/$LIB_DIR/pkgconfig" -type f -exec chmod 644 "{}" "+"
   fi
   if [[ -d "$PKG_DIR/$SYSCONF_DIR/X11/xinit" ]] ; then
      chmod 755 "$PKG_DIR/$SYSCONF_DIR/X11/xinit"/*
   fi
   if [[ -d "$PKG_DIR/$SYSCONF_DIR/X11/app-defaults" ]] ; then
      chmod 644 "$PKG_DIR/$SYSCONF_DIR/X11/app-defaults"/*
   fi
   tprintf $"@success{Done}\n"
}

make_pkg() {
   declare PKG_DIR="$1"
   declare PKG_NAME="${2:-$NAME}"
   rm -rf "$PKG_DIR/$HOME"
   find "$PKG_DIR" -type d -empty -depth -exec rmdir "{}" "+"
   make_pkg_$PKG_FORMAT "$PKG_DIR" "$PKG_NAME"
}
