Skip to content

Instantly share code, notes, and snippets.

@elwinar
Created March 17, 2015 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elwinar/c8658987699e62ee201b to your computer and use it in GitHub Desktop.
Save elwinar/c8658987699e62ee201b to your computer and use it in GitHub Desktop.
DEB Packaging Script
#! /bin/bash
echo "DEB Packaging Tool"
# Create a temp directory
root=`mktemp -d /tmp/pkg.XXXXX`
echo "Creating temporary directory $root"
# Reproduce the tree in the temp directory
for dir in `find deb/ -type d -print`
do
dirname=${dir:4}
if [ -n "$dirname" ]
then
echo "Creating directory $dirname"
mkdir $root/$dirname
fi
done
if [ ! -f "./package" ]; then
echo "No metadata file (\"./package\") found"
exit 0
fi
# Loading the package variables
. ./package
# Retrieving additionnal variables
tag=$(git describe --tags --first-parent --abbrev=0)
version=${tag//"-"/"~"}
if [ $# != 0 ]
then
architecture=$1
else
architecture=`uname -m`
fi
case $architecture in
i686)
goarch="386"
shift
;;
amd64)
goarch="amd64"
shift
;;
esac
# Include each file into the temp directory
for filepath in `find deb/ -type f -print`
do
filename=${filepath:4}
file=$(cat $filepath)
file=${file//"[PROGRAM]"/$program}
file=${file//"[PACKAGE]"/$package}
file=${file//"[DESCRIPTION]"/$description}
file=${file//"[ARCHITECTURE]"/$architecture}
file=${file//"[MAINTAINER]"/$maintainer}
file=${file//"[VERSION]"/$version}
echo "$file" > $root/$filename
done
echo "Building binary for $goarch"
GOARCH=$goarch go build -o $root/usr/bin/$package -ldflags="-s -W"
echo "Fixing package permissions"
chmod 0755 $root/etc/init.d/*
chmod 0755 $root/usr/bin/*
sudo chown root:root -R $root
echo "Building package ${program}_${version}_${architecture}"
sudo dpkg-deb --build $root "./${program}_${version}_${architecture}.deb" > /dev/null
echo "Cleaning temporary directory"
sudo rm -r $root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment