All pastes #804953 Raw Edit

Stuff

public shellscript v1 · immutable
·published 2007-12-03 22:36 UTC
rendered paste body
#!/bin/bashbuildpkgver=1.4.0startdir="$(pwd)"err(){        echo "$1"        exit 1}help_msg(){cat <<EOHELPUsage: buildpkg [OPTIONS] [prototype]OPTIONS:  no option                   creates build-\$pkgname.sh  -g, --generate [prototype]  copy a ZENBUILD prototype  -c, --clean-dirs            remove pkg and src directories    -x, --execute               creates build-\$pkgname.sh, then executes it  -X, --x-and-clean           combines -x and -c together    -v, --version               outputs the version    -h, --help                  display this messageReport bugs or suggestions to ei10066@alunos.ipb.pt.EOHELPexit 1}generate_zenbuild() {        EXTENSION="$1"        ACTUALFILE=ZENBUILD        [ -n "$EXTENSION" ] && ACTUALFILE+=".$EXTENSION"        TEMPLATE="/etc/buildpkg/$ACTUALFILE"        if [ ! -f "$TEMPLATE" ]; then                err "No $TEMPLATE found in /etc/buildpkg, exiting."        elif [ -f "$startdir/ZENBUILD" ]; then                echo "There is already a ZENBUILD in the current directory."                echo -n "Generate ZENBUILD and overwrite it anyways? [y/n] "                while :; do #infinitely                        read answer                        case $answer in                                ''|'y'|'Y'|'yes'|'YES')                                        rm $startdir/ZENBUILD                                        cp $TEMPLATE $startdir/ZENBUILD                                        echo "ZENBUILD created."                                        exit                                        ;;                                'n'|'N'|'no'|'NO')                                        echo "Not creating ZENBUILD, exiting."                                        exit                                        ;;                                *)                                        echo -n "Incorrect response, reply y or n: "                        esac                done        else                cp $TEMPLATE $startdir/ZENBUILD                exit        fi}#Set Optionsunset EXECUTEunset GENERATEunset REMOVEDIRSSHORTOPTS="hXxg::cv"LONGOPTS="help,version,x-and-clean,execute,generate::,clean-dirs"# Note that we use `"$@"' to let each command-line parameter expand to a# separate word. The quotes around `$@' are essential!# We need TEMP as the `eval set --' would nuke the return value of getopt.#TEMP=`getopt -o $SHORTOPTS --long $LONGOPTS -n build --"$@"`TEMP=$(getopt -o $SHORTOPTS --long $LONGOPTS -n "$progname" -- "$@")# Note the quotes around `$TEMP': they are essential!eval set -- "$TEMP"while [ $# -gt 0 ]; do        case $1 in                '-h'|'--help')                        help_msg                        exit                        ;;                '-X'|'--x-and-clean')                        EXECUTE='1'                        REMOVEDIRS='1'                        ;;                '-x'|'--execute')                        EXECUTE='1'                        ;;                '-g'|'--generate')                        GENERATE='1'                        generate_zenbuild "$2"                        exit                        ;;                '-c'|'--clean-dirs')                        REMOVEDIRS='1'                        ;;                '-v'|'--version')                        echo "buildpkg version: $buildpkgver"                        exit                        ;;                '')                        ;;                --)                        break                        ;;                *)                        err "Incorrect usage, see buildpkg --help"        esac        shiftdoneif [ -f "${startdir}/ZENBUILD" ]; then        unset pkgname pkgver pkgrel zenver arch source extradepends options        unset dotnew url CFLAGS CXFLAGS slackdesc doinst build docs        . ${startdir}/ZENBUILDelse        err "No ZENBUILD found in directory, see buildpkg --help"fi##################Sanity Tests Begin#######################################Check that variables existfor VAR in pkgname pkgver pkgrel zenver arch slackdesc; do        [[ ! "${!VAR}" ]] &&                err "variable \"${VAR}\" is not set"done#special source checkif [[ ! "$source" ]]; then        echo "source should be filled except in rare occassions (cvs,svn,etc.)"fi#does build() exist?[[ ! "$(grep '^build\(\)' ${startdir}/ZENBUILD)" ]] &&        err "No build() function found"#check that variables are sane[[ "$(echo $pkgver | grep -E '\-|_')" ]] &&        err "No dashes or underscores allowed in pkgver"[[ "${slackdesc[10]}" ]] &&        err "No more than 10 lines allowed in slackdesc"if [ "$(grep -e "^#Maintainer: Name <email@address.com>" $startdir/ZENBUILD)" ]; then                err "Maintainer isn't set in ZENBUILD, exiting"fi#Check for numerous commentariesNUMCOM=$(grep -e "^#[a-zA-Z]*=[a-zA-Z]*" ${startdir}/ZENBUILD | wc -l)[[ $NUMCOM -ge 2 ]] && echo "WARNING: You appear to have too many commented variables, you should look into it."#source sanityfor SOURCES in "${source[@]}"; do        if [ ! -f "$(basename $SOURCES)" ]; then                protocol=$(echo $SOURCES | sed 's|:.*||')                if [ "$protocol" = "http" -o "$protocol" = "https" -o "$protocol" = "ftp" ]; then                        echo -e "\nDownloading $(basename $SOURCES)\n"                        wget $SOURCES                        if [ ! "$?" == "0" ]; then                                echo "Download failed"                                exit 2                        fi                 else                        err "$SOURCES does not appear to be a url nor is it in the directory"                fi        else                echo "$(basename $SOURCES) found in current directory"        fidone#slackdesc sanityfor ((LINE=0 ; LINE < 10 ; LINE++)); do        REALLINE=$(expr $LINE + 1)        if [ "${#slackdesc[$LINE]}" -gt "70" ] ; then                let "extrachar=${#slackdesc[$LINE]}-70"                err "Line ${REALLINE}: '${slackdesc[$LINE]}' is greater than 70 characters, make it $extrachar characters shorter."        fidone#clean dotnewif [[ "$dotnew" ]]; then        NUM='0'        for dot in ${dotnew[@]}; do                if [ "${dotnew[$NUM]:0:1}" == "/" ]; then                        dotnew[$NUM]=${dotnew[$NUM]:1}                fi                let "NUM++"        donefi#########Sanity Tests End#########################################options=()if [ -n "$options" ]; then        unset NOEXTRACT        unset NOREQUIREDBUILDER        unset REQUIREDBUILDERFLATDEPS        unset NOSTRIP        unset KEEPDEPFILE        for opts in ${options[@]}; do                case $opts in                        noextract)                                echo "Setting option noextract"                                NOEXTRACT='1'                                ;;                        norequiredbuilder)                                echo "Setting option norequiredbuilder"                                NOREQUIREDBUILDER='1'                                ;;                        nostrip)                                echo "Setting option nostrip"                                NOSTRIP='1'                                ;;                        requiredbuilderflatdeps)                                echo "Setting option requiredbuilderflatdeps"                                REQUIREDBUILDERFLATDEPS='1'                                ;;                        keepdepfile)                                echo "Setting option keepdepfile"                                KEEPDEPFILE='1'                                ;;                        *)                                echo "$opts is not a correct option, exiting."                                exit 2                                ;;                esac        donefi#######Generate Build Script#####################################cat <<EOA > ${startdir}/build-${pkgname}.sh#!/bin/bash#Automatically Created by buildpkg $buildpkgverEOAif [ "$(grep -e "^#Maintainer" $startdir/ZENBUILD)" ]; then        grep -e "^#Maintainer" $startdir/ZENBUILD >> $startdir/build-$pkgname.shfi[[ "$(grep -e "^#Former Maintainer(s)" $startdir/ZENBUILD)" ]] &&        grep -e "^#Former Maintainer(s)" $startdir/ZENBUILD >> $startdir/build-$pkgname.sh[[ "$url" ]] && echo "#url: $url" >> $startdir/build-$pkgname.sh[[ "$KEEPDEPFILE" ]] && echo "KEEPDEPFILE='1'" >> $startdir/build-$pkgname.shcat <<"EOREDUNDANT" >> $startdir/build-$pkgname.sh######Begin Redundant Code######################################check_for_root() {        if [ "$UID" != "0" ]; then                echo "You need to be root"                exit 1        fi}clean_dirs () {        for COMPLETED in src pkg; do                if [ -e $COMPLETED ]; then                        rm -rf $COMPLETED                fi        done}clean_old_builds () {        for COMPLETED in $package.{tgz,md5,dep}; do                if [ "$KEEPDEPFILE" = "1" ] && [ $COMPLETED = $package.dep ]; then                        continue                elif [ -e $COMPLETED ]; then                        rm -rf $COMPLETED                fi        done        clean_dirs}set_pre_permissions() {        cd $startdir/src        find . -perm 664 -exec chmod 644 {} \;        find . -perm 600 -exec chmod 644 {} \;        find . -perm 444 -exec chmod 644 {} \;        find . -perm 400 -exec chmod 644 {} \;        find . -perm 440 -exec chmod 644 {} \;        find . -perm 777 -exec chmod 755 {} \;        find . -perm 775 -exec chmod 755 {} \;        find . -perm 511 -exec chmod 755 {} \;        find . -perm 711 -exec chmod 755 {} \;        find . -perm 555 -exec chmod 755 {} \;}check_for_menu_compliance() {        if [ -d "$startdir/pkg/usr/share/applications" ]; then                cd $startdir/pkg/usr/share/applications                for desktop in *.desktop; do                        Icon=$(grep -e "^Icon=" $desktop | sed 's/Icon=//')                        if [[ "$Icon" ]]; then                                if [ ! "$Icon" == "${Icon%.*}" ]; then                                        sed '/Icon/s/\..*$//' -i $desktop                                        Icon=$(grep -e "^Icon=" $desktop | sed 's/Icon=//')                                fi                                if [ -d "$startdir/pkg/usr/share/pixmaps" ]; then                                        cd $startdir/pkg/usr/share/pixmaps                                        for pixmaps in *; do                                                [ -d $pixmaps ] && continue                                                [ ! "${pixmaps##*.}" == "png" -a ! "${pixmaps##*.}" == "svg" ] && continue                                                if [ "${pixmaps%.*}" == "$Icon" ]; then                                                        DIR=$(file $pixmaps | awk -F, '{print $2}' | sed 's/ //g')                                                        [ o"$DIR" == "o" ] && DIR="scalable"                                                        mkdir -p $startdir/pkg/usr/share/icons/hicolor/$DIR/apps                                                        mv $pixmaps $startdir/pkg/usr/share/icons/hicolor/$DIR/apps                                                        if [[ ! "$(ls $startdir/pkg/usr/share/pixmaps)" ]]; then                                                                rm -rf $startdir/pkg/usr/share/pixmaps                                                        fi                                                fi                                        done                                fi                                if [ -d "$startdir/pkg/usr/share/icons" ]; then                                        cd $startdir/pkg/usr/share/icons                                        for icons in *; do                                                if [ "${icons%.*}" == "$Icon" ]; then                                                        [ ! "${pixmaps##*.}" == "png" -a ! "${pixmaps##*.}" == "svg" ] && continue                                                        DIR=$(file $icons | awk -F, '{print $2}' | sed 's/ //g')                                                        [ o"$DIR" == "o" ] && DIR="scalable"                                                        mkdir -p $startdir/pkg/usr/share/icons/hicolor/$DIR/apps                                                        mv $icons $startdir/pkg/usr/share/icons/hicolor/$DIR/apps                                                fi                                        done                                fi                        fi                done        fi}gzip_man_and_info_pages() {        for DOCS in man info; do                if [ -d "$startdir/pkg/usr/share/$DOCS" ]; then                        mv $startdir/pkg/usr/share/$DOCS $startdir/pkg/usr/$DOCS                        if [[ ! "$(ls $startdir/pkg/usr/share)" ]]; then                                rm -rf $startdir/pkg/usr/share                        fi                fi                if [ -d "$startdir/pkg/usr/$DOCS" ]; then                        # I've never seen symlinks in info pages....                        if [ "$DOCS" == "man" ]; then                                (cd $startdir/pkg/usr/$DOCS                                for manpagedir in $(find . -type d -name "man*" 2> /dev/null) ; do                                        ( cd $manpagedir                                        for eachpage in $( find . -type l -maxdepth 1 2> /dev/null) ; do                                                ln -s $( readlink $eachpage ).gz $eachpage.gz                                                rm $eachpage                                        done )                                done)                        fi                        find $startdir/pkg/usr/$DOCS -type f -exec gzip -9 '{}' \;                fi        done}set_post_permissions() {        for DIRS in usr/share/icons usr/doc; do                if [ -d "$startdir/pkg/$DIRS" ]; then                        if [ "$DIRS" == "usr/doc" ]; then                                find $startdir/pkg/$DIRS -type f -exec chmod 644 {} \;                                find $startdir/pkg/$DIRS -type d -exec chmod 755 {} \;                        fi                fi                [ -d $startdir/pkg/$DIRS ] && chown root:root -R $startdir/pkg/$DIRS        done        [ -d $startdir/pkg/usr/bin ] && find $startdir/pkg/usr/bin -user root -group bin -exec chown root:root {} \;}copy_build_script() {        mkdir -p $startdir/pkg/usr/src/$pkgname-$pkgver/        cp $startdir/build-$pkgname.sh $startdir/pkg/usr/src/$pkgname-$pkgver/build-$pkgname.sh        [ -f $startdir/ZENBUILD ] && cp $startdir/ZENBUILD $startdir/pkg/usr/src/$pkgname-$pkgver/ZENBUILD}create_package() {        cd $startdir/pkg        /sbin/makepkg -l y -c n $startdir/$package.tgz        cd $startdir        md5sum $package.tgz > $startdir/$package.md5}EOREDUNDANTif [ -z "$NOSTRIP" ]; thencat <<"EOREDUNDANT1" >> $startdir/build-$pkgname.shstrip_binaries() {        cd $startdir/pkg        find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \        xargs strip --strip-unneeded 2> /dev/null        find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \        xargs strip --strip-unneeded 2> /dev/null}EOREDUNDANT1ficat <<"EOREDUNDANT2" >> $startdir/build-$pkgname.sh#########End Redundant Code##############################################Begin Non Redundant Code##############################EOREDUNDANT2if [[ "$source" ]]; thencat <<EOPREPAREDIRECTORY >>$startdir/build-$pkgname.shprepare_directory() {        mkdir \$startdir/src        mkdir -p \$startdir/pkg/usr/src/\$pkgname-\$pkgver        for SOURCES in \${source[@]}; do                protocol=\$(echo \$SOURCES | sed 's|:.*||')                if [ ! -f "\$(basename \$SOURCES)" ]; then                        if [ "\$protocol" = "http" -o "\$protocol" = "https" -o "\$protocol" = "ftp" ]; then                                echo -e "\nDownloading \$(basename \$SOURCES)\n"                                wget \$SOURCES                                if [ ! "\$?" == "0" ]; then                                        echo "Download failed"                                        exit 2                                fi                         else                                echo "\$SOURCES does not appear to be a url nor is it in the directory"                                exit 2                        fi                fi                cp -R \$(basename \$SOURCES) \$startdir/src                if ! [ "\$protocol" = "http" -o "\$protocol" = "https" -o "\$protocol" = "ftp" ]; then                        cp -R \$startdir/\$(basename \$SOURCES) \$startdir/pkg/usr/src/\$pkgname-\$pkgver/                fi                if [ "\${SOURCES##*.}" == "desktop" ]; then                        [[ "\$(grep -e "^Categories=[a-zA-Z]*;" \$(basename \$SOURCES))" ]] && install -D \$startdir/\$(basename \$SOURCES) $startdir/pkg/usr/share/applications/\$(basename \$SOURCES)                fi        done}EOPREPAREDIRECTORYelsecat <<"EOPREPAREDIRECTORY" >>$startdir/build-$pkgname.shprepare_directory() {        mkdir $startdir/src        mkdir $startdir/pkg}EOPREPAREDIRECTORYfiif [ -z "$NOEXTRACT" ]; thencat <<EOEXTRACTSOURCES >>$startdir/build-$pkgname.shextract_source() {        cd \$startdir/src        if [[ "\$(ls \$startdir/src)" ]]; then                for FILES in \${source[@]}; do                        FILES="\$(basename \$FILES)"                        file_type=\$(file -biz "\$FILES")                        unset cmd                        case "\$file_type" in                                *application/x-tar*)                                        cmd="tar -xf" ;;                                *application/x-zip*)                                        cmd="unzip" ;;                                *application/x-gzip*)                                        cmd="gunzip -d -f" ;;                                *application/x-bzip*)                                        cmd="bunzip2 -f" ;;                        esac                        if [ "\$cmd" != "" ]; then                                echo "\$cmd \$FILES"                                \$cmd \$FILES                        fi                done        elif [ ! "\$source" ]; then                echo -n "" # lame fix        else                echo "no files in the src directory \$startdir/src"                exit 2        fi}EOEXTRACTSOURCESfised -n '/^build()/,/^}$/p' $startdir/ZENBUILD >> $startdir/build-$pkgname.shcat <<EOM >>$startdir/build-$pkgname.shcreate_slackdesc() {mkdir \$startdir/pkg/installcat <<"EODESC" >\$startdir/pkg/install/slack-desc$pkgname: ${slackdesc[0]}$pkgname: $pkgname: ${slackdesc[1]}$pkgname: ${slackdesc[2]}$pkgname: ${slackdesc[3]}$pkgname: ${slackdesc[4]}$pkgname: ${slackdesc[5]}$pkgname: ${slackdesc[6]}$pkgname: ${slackdesc[7]}$pkgname: ${slackdesc[8]}$pkgname: ${slackdesc[9]}EODESC}EOMif [[ "$dotnew" ]]; then        cat<<EOP >>$startdir/build-${pkgname}.shsetup_dotnew() {        for files in ${dotnew[@]} ; do                fullfile="\${startdir}/pkg/\${files}"                if [ -e "\$fullfile" ]; then                        mv \$fullfile \${fullfile}.new                else                        echo "\$fullfile was not found"                        exit 2                fi        done        cat<<"EODOTNEW" >\$startdir/pkg/install/doinst.sh#Added by buildpkg $buildpkgverdotnew() {        NEW="\${1}.new"        OLD="\$1"        if [ ! -e \$OLD ]; then                mv \$NEW \$OLD        elif [ -z "\$(cmp \$NEW \$OLD)" ]; then                rm \$NEW        fi}EOPfor i in ${dotnew[@]}; doecho "dotnew $i" >> $startdir/build-$pkgname.shdonecat <<"EOQ" >>$startdir/build-$pkgname.shEODOTNEW}EOQfiif [[ "$(grep '^doinst\(\)' $startdir/ZENBUILD)" ]]; then        cat <<EOPOSTINSTALL >>$startdir/build-$pkgname.shsetup_doinst() {cat <<"EODOINST" >>\$startdir/pkg/install/doinst.sh$(sed -n '/doinst()/,/^}$/p' $startdir/ZENBUILD)doinstEODOINST}EOPOSTINSTALLfiif [[ "$docs" ]]; thencat <<EOEXTRADOCS >> $startdir/build-$pkgname.shcopy_docs() {        for stuff in \${docs[@]}; do                if [ ! -d "\$startdir/pkg/usr/doc/\$pkgname-\$pkgver" ]; then                        mkdir -p \$startdir/pkg/usr/doc/\$pkgname-\$pkgver                fi                find \$startdir/src -type f -iname "\$stuff" -exec cp -R '{}' \$startdir/pkg/usr/doc/\$pkgname-\$pkgver \;        done}EOEXTRADOCSfiif [[ "$extradepends" ]]; then        unset FIRST        for deps in ${extradepends[@]} ; do                if [[ ! "$FIRST" ]]; then                        depstring=",${deps}"                        FIRST="1"                        continue                fi                depstring="${depstring},${deps}"        doneficat <<EODEPS1 >>$startdir/build-$pkgname.shdetect_dependencies() {        echo "Detecting dependencies..."EODEPS1if [ -z "$NOREQUIREDBUILDER" ]; then        if [ -z "$REQUIREDBUILDERFLATDEPS" ]; then                echo '  depends=$(requiredbuilder -z $startdir/$package.tgz)' >> $startdir/build-$pkgname.sh        else                echo '  depends=$(requiredbuilder -r -z $startdir/$package.tgz)' >> $startdir/build-$pkgname.sh        fielse        echo '  depends=""' >> $startdir/build-$pkgname.shfiif [[ "$lessdepends" ]]; then                for deps in ${lessdepends[@]}; docat <<EODEPS2 >>$startdir/build-$pkgname.sh        depends="\$(echo \$depends | sed -e 's/\<${deps}\>//' -e 's/,,/,/')"EODEPS2        doneficat <<EODEPS3>>$startdir/build-$pkgname.sh        depstring=\$depends$depstring        unset FIRST        for deps in \$(echo "\${depstring}" | sed 's/,/\n/g' | sort -u); do                if [[ ! "\$FIRST" ]]; then                        finaldeps=\$deps,                        FIRST='1'                        continue                fi                finaldeps=\${finaldeps}\${deps},        done        echo "\$finaldeps" | sed -e 's/^,//' -e 's/,$//' > \$startdir/\$package.dep}EODEPS3cat <<EOVARIABLES >>$startdir/build-$pkgname.sh####End Non Redundant Code#############################Variablesstartdir=\$(pwd)SRC=\$startdir/srcPKG=\$startdir/pkgpkgname=$pkgnamepkgver=$pkgverpkgrel=$pkgrelzenver=$zenverarch=$archpackage=\$pkgname-\$pkgver-\$arch-\$zenver.\$pkgrelEOVARIABLESgrep -e "^_[a-zA-Z]*=[a-zA-Z]*" $startdir/ZENBUILD >> $startdir/build-$pkgname.shecho "source=(${source[@]})" >> $startdir/build-$pkgname.shecho "docs=(${docs[@]})" >> $startdir/build-$pkgname.shCFLAGS=${CFLAGS:-"-O2 -march=$arch -mtune=i686"}CXXFLAGS=${CFLAGS:-"-O2 -march=$arch -mtune=i686"}for FLAGS in CFLAGS CXXFLAGS; do        echo "export $FLAGS=\"${!FLAGS}\"" >>$startdir/build-$pkgname.shdonecat <<"EOEXECUTION1" >> $startdir/build-$pkgname.sh#Executioncheck_for_rootclean_old_buildsprepare_directoryEOEXECUTION1[ -z "$NOEXTRACT" ] && echo "extract_source" >>$startdir/build-$pkgname.shcat <<"EOEXECUTION2">>$startdir/build-$pkgname.shset_pre_permissionsbuildif [ ! "$?" = "0" ]; then        echo "build() failed."        exit 2ficreate_slackdescEOEXECUTION2[[ "$dotnew" ]] && echo "setup_dotnew" >>$startdir/build-$pkgname.sh[[ "$(grep '^doinst\(\)' $startdir/ZENBUILD)" ]] && echo "setup_doinst" >>$startdir/build-$pkgname.sh[[ "$docs" ]] && echo "copy_docs" >>$startdir/build-$pkgname.shcat <<"EOEXECUTION31" >> $startdir/build-$pkgname.shcheck_for_menu_complianceEOEXECUTION31[ -z "$NOSTRIP" ] && echo "strip_binaries" >>$startdir/build-$pkgname.shcat <<"EOEXECUTION32" >> $startdir/build-$pkgname.shgzip_man_and_info_pagesset_post_permissionscopy_build_scriptcreate_packageEOEXECUTION32[[ -z "$KEEPDEPFILE" ]] && echo "detect_dependencies" >> $startdir/build-$pkgname.shcat <<"EOEXECUTION31" >> $startdir/build-$pkgname.shecho "Package has been built."EOEXECUTION31if [ "$REMOVEDIRS" == "1" ]; then        echo 'echo "Cleaning pkg and src directories"' >> $startdir/build-$pkgname.sh        echo "clean_dirs" >> $startdir/build-$pkgname.shfichmod +x $startdir/build-$pkgname.shecho "build-$pkgname.sh has been created"if [ "$EXECUTE" == "1" ]; then        ./build-$pkgname.sh 2>&1 | tee build-$pkgname.logfi