将源代码一步打包成deb包的脚本

#!/bin/bash
error_check()
{
    if [ $1 -ne 0 ];then
        echo "$2"
        exit $1
    fi
}

copy_file()
{
    cp -f -a -v $1 $2
    error_check $? "$1 not found"
}

rm_file()
{
    for file in $@;do
        if [ -e "$file" ];then
            rm -vf $file
        fi
    done
}
rm_dir()
{
    for dir in $@;do
        if [ -d "$dir" ];then
            rm -vrf $dir
        fi
    done
}

show_opts()
{
    echo "deb-build is help for generating deb packages"
    echo "基本功能:"
    echo "  create            create a debian rule when building package for the first time"
    echo "  build             build packages"
    echo "  clean             clean all generated files"
    echo "参数详细说明:"
    echo "    -p,--project      specify what the package name is"
    echo "    -s,--srcdir       specify where the source code is"
    echo "    -v,--version      specify the version number,if not set,it will get it from changlog"
    echo "    -n,-r,--release   set the release number,if not set,it will get it from changelog"
    echo "    -h,--help       show helps"
}

version_gt()
{
    test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"
}
version_ge()
{
    test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"
}

parse_changelog()
{
    ver=$(dpkg-parsechangelog -S $1 -l ${SRC_DIR}/debian/changelog)
    if [ $? -ne 0 ];then
        echo "parse changelog failed"
        exit 1
    fi
    echo "$ver"
}

get_check_config()
{
    if [ ! -d "${SRC_DIR}" ];then
        echo "$SRC_DIR not exist"
        exit 1
    fi

    BASE_NAME=$(parse_changelog source)
    error_check $? "not run create to generate debian"
    echo "targetname=$BASE_NAME"
    version=$(parse_changelog version)
    echo "$version"
    if [ -z "$VERSION" ];then 
        VERSION=`echo $version|awk '{split($_,a,"-");print a[1]}'`
    fi
    if [ -z "$RELEASE" ];then
        RELEASE=`echo $version|awk '{split($_,a,"-");print a[2]}'`
    fi
    if version_gt $version ${VERSION}-${RELEASE};then
        VERSION=`echo $version|awk '{split($_,a,"-");print a[1]}'`
        RELEASE=`echo $version|awk '{split($_,a,"-");print a[2]}'`
    fi
    echo "Version = $VERSION"

    echo "Release=$RELEASE"

    CODE_DIR="${BASE_NAME}-${VERSION}"
}

parse_opts()
{
    shift
    ARGS=`getopt -o h:s:v:n:r:p: -l help:,srcdir:,version:,release:,projectname: -- "$@"`
    eval set -- "$ARGS"
    while true
    do
        case $1 in
            -p|--projectname)
                BASE_NAME=$2
                echo "BASE_NAME=$BASE_NAME"
                shift 2
                ;;
            -s|--srcdir)
                SRC_DIR=$2
                echo "SRC_DIR=$SRC_DIR"
                shift 2
                ;;
            -v|--version)
                VERSION=$2
                echo "VERSION=$VERSION"
                shift 2
                ;;
            -n|-r|--release)
                RELEASE=$2
                echo "RELEASE=$RELEASE"
                shift 2
                ;;
            --)
                shift
                break
                ;;
            -h|--help)
                show_opts
                exit 0
                ;;
            *)
                echo "error parameter $1 $2"
                exit
                ;;
        esac
    done
    get_check_config
}

BASE_DIR=$(cd `dirname $0`;pwd)
BUILD_DIR=$(pwd)
BASE_NAME=""
SRC_DIR=""
VERSION=""
RELEASE=0
CODE_DIR=""

case $1 in
    create)
        parse_opts "$@"
        mkdir -p $CODE_DIR
        cd $CODE_DIR
        dh_make --createorig -s -t ../$SRC_DIR
        vi debian/rules
        cp -r debian ../$SRC_DIR/
        cd ..
        echo -e "\033[31;44;1mplease make sure the rules in $CODE_DIR/debian is valid!!\033[0m"
        echo "-------all done--------"
        exit 0
        ;;
    build)
        parse_opts "$@"
        ;;
    clean)
        parse_opts "$@"
        rm_dir $CODE_DIR/*
        rm_file *.buildinfo
        rm_file *.changes
        rm_file *.deb
        rm_file *.xz
        rm_file *.gz
        rm_file *.dsc
        exit 0
        ;;
    *|--help)
        show_opts
        exit 0
        ;;
esac

cur=$(parse_changelog version)
if version_gt "${VERSION}-${RELEASE}" "$cur";then
    cd $SRC_DIR
    dch -v "${VERSION}-${RELEASE}"
    error_check $? "dch exit"
    cd -
    VERSION=$(parse_changelog version|awk '{split($1,a,"-");print a[1]}')
    RELEASE=$(parse_changelog version|awk '{split($1,a,"-");print a[2]}')
fi

CODE_DIR="${BASE_NAME}-${VERSION}"
if [ -d "${CODE_DIR}" ];then
    rm -rf ${CODE_DIR}/*
else
    mkdir -p ${CODE_DIR}
fi

cp -r ${SRC_DIR}/debian ${CODE_DIR}/

cd ${CODE_DIR}

arch=`uname -m`
if [ "$arch"x = "x86_64"x ];then
    echo "install elf-packer"
    ../$SRC_DIR/elf-packer/elf-packer.sh install
    error_check $? "exit elf-packer.sh install"
fi

export QT_SELECT=qt5
export SOURCE_DIR=../$SRC_DIR
cd ..
tar -czf "${BASE_NAME}_${VERSION}.orig.tar.gz" "$SRC_DIR"
cd ${CODE_DIR}
dpkg-buildpackage -us -uc
if [ $? -ne 0 ];then
    echo "dpkg-buildpackage exit"
    unset SOURCE_DIR
    exit $?
fi
unset SOURCE_DIR
echo "------all done-------"
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值