#!/bin/bash updatemdsum() { if [ ! -z $source ]; then for sources in ${source[@]}; do if [[ $sources =~ ^(http|https|ftp)://.*/(.+) ]]; then sourcename=$SOURCE_DIR/$(echo $sources | rev | cut -d / -f 1 | rev) else sourcename="$sources" fi needupdatechecksum="$needupdatechecksum $sourcename" done for file in ${needupdatechecksum[@]}; do if [ ! -f $file ]; then msg "Error updating checksum, source not found." exitscript1 fi done md5sum $needupdatechecksum | awk '{ print $1 }' > .md5sum [ -f .md5sum ] && msg "md5sum updated." else touch .md5sum fi } checkmdsum() { for sources in ${source[@]}; do if [[ $sources =~ ^(http|https|ftp)://.*/(.+) ]]; then sourcename=$SOURCE_DIR/$(echo $sources | rev | cut -d / -f 1 | rev) else sourcename="$sources" fi sourcemd5sum=$(md5sum $sourcename | awk '{ print $1 }') if [ ! $(grep $sourcemd5sum .md5sum) ]; then msg "${color_red}Checksum error:${color_reset} $sourcename" CHECKSUM_ERROR=yes fi done [ "$CHECKSUM_ERROR" ] && exitscript1 } getsource() { for sources in ${source[@]}; do if [[ $sources =~ ^(http|https|ftp)://.*/(.+) ]]; then downloadsource $sources fi done } downloadsource(){ tarballname=$(echo $1 | rev | cut -d / -f 1 | rev) WGETCMD="wget --passive-ftp --no-directories --tries=3 --waitretry=3 --output-document=$SOURCE_DIR/$tarballname.partial" WGETRESUME="wget -c --passive-ftp --no-directories --tries=3 --waitretry=3 --output-document=$SOURCE_DIR/$tarballname.partial" if [ -n $1 ]; then if [ "$REDOWNLOAD_SOURCE" ]; then if [ -f $SOURCE_DIR/$tarballname ]; then rm $SOURCE_DIR/$tarballname fi fi if [ -f $SOURCE_DIR/$tarballname ]; then msg "Source file ${color_green}$tarballname${color_reset} found." else if [ -f $SOURCE_DIR/$tarballname.partial ]; then msg "Resuming ${color_green}$1${color_reset}." $WGETRESUME $1 && mv $SOURCE_DIR/$tarballname.partial $SOURCE_DIR/$tarballname || exitscript1 else msg "Downloading ${color_green}$1${color_reset}." $WGETCMD $1 && mv $SOURCE_DIR/$tarballname.partial $SOURCE_DIR/$tarballname || exitscript1 fi fi fi } preparesource() { SRC=$WORK_DIR/$name/src PKG=$WORK_DIR/$name/pkg [ -d "$WORK_DIR/$name" ] && rm -fr "$WORK_DIR/$name" mkdir -p $SRC $PKG for sources in ${source[@]}; do if [[ $sources =~ ^(http|https|ftp)://.*/(.+) ]]; then tarballname=$(echo $sources | rev | cut -d / -f 1 | rev) NO_EXTRACT="" for i in ${noextract[@]}; do if [ "$i" = "$tarballname" ]; then NO_EXTRACT=yes msg "Preparing ${color_green}$tarballname${color_reset}..." && cp $SOURCE_DIR/$tarballname $SRC || ERROR_PREPARE_SOURCE=yes break fi done if [ ! "$NO_EXTRACT" ]; then case $tarballname in *.tar|*.tar.gz|*.tar.Z|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.txz|*.tar.lzma|*.zip|*.rpm) COMMAND="$EXTPROG -p -o -C $SRC -xf $SOURCE_DIR/$tarballname" MODE="Unpacking" ;; *) COMMAND="cp $SOURCE_DIR/$tarballname $SRC" MODE="Preparing" ;; esac msg2 "$MODE ${color_green}$tarballname${color_reset}..." $COMMAND if [ $? != 0 ]; then msg "$MODE ${color_red}$tarballname${color_reset} failed." clearworkdir exitscript1 fi fi else msg2 "Preparing ${color_green}$sources${color_reset}..." && cp $sources $SRC || ERROR_PREPARE_SOURCE=yes fi done if [ "$ERROR_PREPARE_SOURCE" ]; then msg "Error preparing ${color_red}$sources${color_reset}." clearworkdir exitscript1 fi for runscript in preinstall postinstall preupgrade postupgrade preremove postremove readme; do if [ -f $runscript ]; then [ ! -d $PKG/.pkginstall ] && mkdir $PKG/.pkginstall cp $runscript $PKG/.pkginstall fi done } loadspkgbuild() { if [ -f $BUILD_SCRIPT ]; then . $BUILD_SCRIPT getpkginfo else msg "${color_red}Error no $BUILD_SCRIPT found.${color_reset}" exitscript1 fi } buildpackage() { msg "Start build ${color_green}$name-$version-$release${color_reset}." pushd $SRC (set -e -x ; build) if [ $? != 0 ]; then msg "Build ${color_red}$PKGNAME${color_reset} failed." clearworkdir exitscript1 else msg "Successfully build ${color_green}$name-$version-$release${color_reset}." fi popd } runpreinstall() { if [ -f preinstall ]; then msg "Running preinstall script..." sh preinstall && PREINSTALL_STATUS=OK || PREINSTALL_STATUS=KO fi } packaging() { pushd $PKG if [ -f usr/share/info/dir ]; then rm usr/share/info/dir fi if [ ! "$NO_STRIP" ]; then strip_files fi compressinfomanpages purgefiles # only installed depends will get written into package for dep in ${depends[@]}; do if [ -d $INDEX_DIR/$dep ]; then if [ -z "$newdep" ]; then newdep="$dep" else newdep="$newdep $dep" fi fi done # only installed makedepends will get written into package for mdep in ${makedepends[@]}; do if [ -d $INDEX_DIR/$mdep ]; then if [ -z "$newmdep" ]; then newmdep="$mdep" else newmdep="$newmdep $mdep" fi fi done echo "# Generated by buildpkg" > .pkginfo echo "# `date`" >> .pkginfo echo "name = $name" >> .pkginfo echo "version = $version" >> .pkginfo echo "release = $release" >> .pkginfo [ -n "$description" ] && echo "description = $description" >> .pkginfo [ -n "$homepage" ] && echo "homepage = $homepage" >> .pkginfo [ -n "$maintainer" ] && echo "maintainer = $maintainer" >> .pkginfo [ -n "$backup" ] && echo "backup = ${backup[@]}" >> .pkginfo [ -n "$conflict" ] && echo "conflict = ${conflict[@]}" >> .pkginfo [ -n "$newdep" ] && echo "depends = ${newdep[@]}" >> .pkginfo [ -n "$newmdep" ] && echo "makedepends = ${newmdep[@]}" >> .pkginfo [ -n "$noextract" ] && echo "noextract = $noextract" >> .pkginfo msg "Packaging ${color_green}$name-$version-$release${color_reset}..." if [ -d .pkginstall ]; then tar -c -J -p -f $PACKAGE_DIR/$PKGNAME * .pkginfo .pkginstall else tar -c -J -p -f $PACKAGE_DIR/$PKGNAME * .pkginfo fi if [ $? != 0 ]; then msg "Packaging ${color_red}$PKGNAME${color_reset} failed." if [ -f $PACKAGE_DIR/$PKGNAME ]; then rm $PACKAGE_DIR/$PKGNAME fi exitscript1 else pkgsize="$(ls -lh $PACKAGE_DIR/$PKGNAME | awk '{print $5}')" $EXTPROG -t -v -f $PACKAGE_DIR/$PKGNAME msg "Successfully create package ${color_green}$PKGNAME${color_reset}. (${pkgsize})" fi case $PREINSTALL_STATUS in OK) msg "preinstall : ${color_green}OK${color_reset}" ;; KO) msg "preinstall : ${color_red}FAIL${color_reset}" ;; esac popd } purgefiles() { for option in ${PURGE_FILE[@]}; do if [ -e $option ]; then msg2 "Purging $option..." rm -fr $option fi done } strip_files() { msg2 "Stripping binaries..." find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null msg2 "Stripping libraries..." find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null } compressinfomanpages() { if [ -d usr/share/man ]; then msg2 "Compressing man pages..." ( cd usr/share/man find . -type f ! -name "*.gz" -exec gzip -9 {} \; for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done ) fi if [ -d usr/share/info ]; then msg2 "Compressing info pages..." ( cd usr/share/info find . -name "*.info*" -exec gzip -9 {} \; ) fi } buildpkg() { getsource if [ ! "IGNORE_MDSUM" ]; then if [ -f .md5sum ]; then checkmdsum else updatemdsum fi fi preparesource [ "$UPGRADE_PKG" ] && NO_PREINSTALL=yes if [ ! "$NO_PREINSTALL" ]; then runpreinstall NO_PREINSTALL=yes fi buildpackage if [ "$FORCE_REBUILD" ]; then if [ -f $PACKAGE_DIR/$PKGNAME ]; then rm $PACKAGE_DIR/$PKGNAME fi fi packaging clearworkdir } checkdeps() { for dep in ${depends[@]}; do if [ ! -d $INDEX_DIR/$dep ]; then MSDEP+=($dep) #msg "Missing dep: ${color_yellow}$dep${color_reset}" MISSING_DEP=yes fi done for makedep in ${makedepends[@]}; do if [ ! -d $INDEX_DIR/$makedep ]; then MSMKDEP+=($makedep) #msg "Missing dep: ${color_yellow}$makedep${color_reset} (make)" MISSING_DEP=yes fi done if [ "$MISSING_DEP" ]; then msg "Missing dependencies:" for msd in ${MSDEP[@]}; do msg2 "$msd" done for msmkd in ${MSMKDEP[@]}; do msg2 "$msmkd (make)" done exitscript1 fi } check_directory() { for dir in $WORK_DIR $SOURCE_DIR $PACKAGE_DIR; do if [ ! -d $dir ]; then msg "Directory ${color_yellow}$dir${color_reset} not exist." DIR_ERROR=yes elif [ ! -w $dir ]; then msg "Directory ${color_yellow}$dir${color_reset} not writable." DIR_ERROR=yes elif [ ! -x $dir ] || [ ! -r $1 ]; then msg "Directory ${color_yellow}$dir${color_reset} not readable." DIR_ERROR=yes fi done [ "$DIR_ERROR" ] && exitscript1 } # remove lock file and exit 1 exitscript1() { if [ -f /tmp/spkg.$name.lock ]; then rm /tmp/spkg.$name.lock fi exit 1 } # remove lock file and exit 0 exitscript0() { if [ -f /tmp/spkg.$name.lock ]; then rm /tmp/spkg.$name.lock fi exit 0 } # create lock file in /tmp prevent for build same package simultaneously lockbuild() { if [ ! -f /tmp/spkg.$name.lock ]; then touch /tmp/spkg.$name.lock fi } clearworkdir() { if [ ! "$KEEP_WORK" ]; then if [ ! -z $name ]; then if [ -d $WORK_DIR/$name ]; then rm -fr $WORK_DIR/$name fi fi fi } interrupted() { echo "" msg "${color_yellow}Interrupted!${color_reset}" clearworkdir exitscript1 } help() { echo -e "Usage:" echo -e "\t`basename $0` < options >" echo -e "" echo -e "Example:" echo -e "\t`basename $0` -fr -kw -i this will force rebuild, install package and keep working directory" echo -e "" echo -e "Options:" echo -e "\t-i,\t--install install package into system" echo -e "\t-u,\t--upgrade upgrade package" echo -e "\t-r,\t--reinstall reinstall package" echo -e "\t-id,\t--ignore-dependency ignore dependency" echo -e "\t-ic,\t--ignore-conflict ignore conflict when installing package" echo -e "\t-v,\t--verbose verbose install process" echo -e "\t\t--no-preinstall skip preinstall script when build/install package" echo -e "\t\t--no-postinstall skip postinstall script after install package" echo -e "\t\t--no-color disable color" echo -e "\t-fr,\t--force-rebuild rebuild package" echo -e "\t-im,\t--ignore-mdsum skip md5sum checking" echo -e "\t-um,\t--update-mdsum update md5sum" echo -e "\t-cm,\t--check-mdsum check md5sum for source file" echo -e "\t-do,\t--download-only download only source file" echo -e "\t-eo,\t--extract-only extract only source file" echo -e "\t-kw,\t--keep-work keep working directory" echo -e "\t-ns,\t--no-strip skip strip package library and binary" echo -e "\t-rd,\t--redownload re-download source file" echo -e "\t-sd,\t--source-dir override source dir" echo -e "\t-o,\t--output override output path for packages" echo -e "\t-h,\t--help show this help page" echo -e "" echo -e "Note:" echo -e "\t* use `basename $0` without any options will only download source and build package by using other default options" echo -e "\t* buildpkg need run inside port directory" } parse_options() { while [ "$1" ]; do case $1 in # this option will pass to installpkg -i|--install) INSTALL_PKG=yes ;; -u|--upgrade) UPGRADE_PKG=yes ;; -r|--reinstall) REINSTALL_PKG=yes ;; -id|--ignore-dependency) IGNORE_DEP=yes ;; -ic|--ignore-conflict) IGNORE_CONFLICT=yes ;; --no-preinstall) NO_PREINSTALL=yes ;; --no-preinstall) NO_POSTINSTALL=yes ;; -v|--verbose) VERBOSE_INSTALL=yes ;; --no-color) NO_COLOR=yes ;; # this option not pass to installpkg -fr|--force-rebuild) FORCE_REBUILD=yes ;; -im|--ignore-mdsum) IGNORE_MDSUM=yes ;; -um|--update-mdsum) UPDATE_MDSUM=yes ;; -cm|--check-mdsum) CHECK_MDSUM=yes ;; -do|--download-only) DOWNLOAD_ONLY=yes ;; -eo|--extract-only) EXTRACT_ONLY=yes ;; -kw|--keep-work) KEEP_WORK=yes ;; -ns|--no-strip) NO_STRIP=yes ;; -rd|--redownload) REDOWNLOAD_SOURCE=yes ;; -h|--help) SHOW_HELP=yes ;; -sd|--source-dir) if [ ! "$2" ]; then msg "Option '$1' require an argument (source path for package)." exit 1 fi SOURCE_PKG="$2" shift ;; -o|--output) if [ ! "$2" ]; then msg "Option '$1' require an argument (output path for compiled package)." exit 1 fi OUTPUT_PKG="$2" shift ;; *) msg "Option invalid!" exit 1 ;; esac shift done } main() { . /usr/share/scratchpkg/functions || exit 1 loadconfigfile parse_options "$@" ### DISABLE COLOR ### if [ "$NO_COLOR" ]; then nocolor fi ### SHOW HELP ### if [ "$SHOW_HELP" ]; then help exit 0 fi ### SET OUTPUT PATH FOR PACKAGE ### if [ $OUTPUT_PKG ]; then PACKAGE_DIR="$OUTPUT_PKG" fi ### SET SOURCE PATH ### if [ $SOURCE_PKG ]; then SOURCE_DIR="$SOURCE_PKG" fi check_directory loadspkgbuild ### CHECK SPKGBUILD ### if [ -z "$name" ] || [ -z "$version" ] || [ -z "$release" ] || [ "$(basename `pwd`)" != "$name" ] || [ "`type -t build`" != "function" ]; then msg "Error, please check spkgbuild." exit 1 fi PKGNAME="$name-$version-$release.spkg.txz" ### UPDATE CHECKSUM ### if [ "$UPDATE_MDSUM" ]; then getsource updatemdsum exit 0 fi ### CHECK CHECKSUM ### if [ "$CHECK_MDSUM" ]; then checkmdsum exit 0 fi ### CHECK FOR LOCK FILE ### if [ -f /tmp/spkg.$name.lock ]; then msg "Cant build same package simultaneously." msg "remove ${color_yellow}/tmp/spkg.$name.lock${color_reset} if no build process for ${color_yellow}$name.${color_reset}" exit 1 fi ### DOWNLOAD ONLY ### if [ "$DOWNLOAD_ONLY" ]; then getsource exit 0 fi ### EXTRACT ONLY ### if [ "$EXTRACT_ONLY" ]; then getsource preparesource exit 0 fi ### CREATE LOCK FILE ### lockbuild ### BUILD ### if [ -f $PACKAGE_DIR/$PKGNAME ] && [ ! "$FORCE_REBUILD" ]; then # if package txz exist and not force rebuild if [ ! "$INSTALL_PKG" ] && [ ! "$REINSTALL_PKG" ] && [ ! "$UPGRADE_PKG" ]; then msg "${color_green}$PKGNAME${color_reset} is up-to-date." exitscript0 fi else # dependency check if [ ! "$IGNORE_DEP" ]; then checkdeps fi buildpkg fi ### INSTALL PACKAGE ### if [ "$INSTALL_PKG" ] || [ "$REINSTALL_PKG" ] || [ "$UPGRADE_PKG" ]; then IGNORE_CONFLICT="$IGNORE_CONFLICT" \ IGNORE_DEP="$IGNORE_DEP" \ REINSTALL_PKG="$REINSTALL_PKG" \ UPGRADE_PKG="$UPGRADE_PKG" \ VERBOSE_INSTALL="$VERBOSE_INSTALL" \ NO_PREINSTALL="$NO_PREINSTALL" \ NO_POSTINSTALL="$NO_POSTINSTALL" \ NO_COLOR="$NO_COLOR" \ installpkg $PACKAGE_DIR/$PKGNAME exitscript0 fi exitscript0 } trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM main "$@"