#! /bin/bash # # rpm2fpm # # by Marcus Habermehl (BMH1980) # # Converting rpm archives to a Frugalware fpm archive. export USE_COLOR="y" if [ -f /usr/lib/frugalware/fwmakepkg ] ; then . /usr/lib/frugalware/fwmakepkg else Fmessage() { echo "$@" } fi CURDIR=$(pwd) CPIOPKG=$(mktemp) BUILDDIR=$(mktemp -d) INSTALL=0 REMOVE=0 # Usage informations. usage() { echo "$1" echo "usage: $1 [options]" echo "options:" echo " -i, --install install the created fpm package" echo " -r, --remove delete the rpm package after converting" echo } # Check the command line. while (( $# > 1 )) ; do case $1 in -h|--help) usage && exit 0 ;; -i|--install) INSTALL=1 ;; -r|--remove) REMOVE=1 ;; *) usage && exit 1 ;; esac shift done # Get the absolute path of the given rpm archive. RPMPKG=$(cd `dirname $1`;echo `pwd`/`basename $1`) # Create directory structure from the rpm archive. Fmessage "Creating directories... " for i in $(rpm -qlp ${RPMPKG}) ; do mkdir -p ${BUILDDIR}/$(dirname $i) 2> /dev/null done # Convert the rpm archive to a cpio archive. Fmessage "Converting rpm archive to cpio archive... " rpm2cpio ${RPMPKG} > ${CPIOPKG} 2> /dev/null cd ${BUILDDIR} # Extract the cpio archive. Fmessage "Extracting cpio archive... " cpio -ivI ${CPIOPKG} 2> /dev/null # Creating the .FILELIST for the fpm archive. Fmessage "Creating .FILELIST... " find . -mindepth 1 | sed 's|^./||' | sort > ${BUILDDIR}/.FILELIST 2> /dev/null # Extract needed informations for the fpm archive from the rpm archive. Fmessage "Getting informations from the rpm archive... " PKGNAME=$(rpm -q --queryformat=%{name} -p ${RPMPKG}) PKGVER=$(rpm -q --queryformat=%{version}-%{release} -p ${RPMPKG}) PKGDESC=$(rpm -q --queryformat=%{summary} -p ${RPMPKG}) URL=$(rpm -q --queryformat=%{url} -p ${RPMPKG}) LICENSE=$(rpm -q --queryformat=%{license} -p ${RPMPKG}) BUILDDATE=$(python -c "import time; print time.ctime($(rpm -q --queryformat=%{buildtime} -p ${RPMPKG}))") PACKAGER="rpm2fpm" SIZE=$(rpm -q --queryformat=%{size} -p ${RPMPKG}) ARCH=$(rpm -q --queryformat=%{arch} -p ${RPMPKG}) GROUP=$(rpm -q --queryformat=%{group} -p ${RPMPKG} | tr [:upper:] [:lower:] | tr / - | tr \ _) # Write the .PKGINFO file for the fpm archive. Fmessage "Generating .PKGINFO... " echo "# Generated by rpm2fpm # $(LC_ALL=C date) pkgname = ${PKGNAME} pkgver = ${PKGVER} pkgdesc = ${PKGDESC} url = ${URL} license = ${LICENSE} builddate = ${BUILDDATE} packager = ${PACKAGER} size = ${SIZE} arch = ${ARCH} group = ${GROUP}" > ${BUILDDIR}/.PKGINFO # Get the dependencies and write they to the .PKGINFO file. Fmessage "Query dependencies... " eval "`chkdep -d .`" for i in ${depends[*]} ; do echo "depend = ${i}" >> ${BUILDDIR}/.PKGINFO done # Extracting existing install scripts and write them to .INSTALL. Fmessage "Checking for install scripts... " post_install="$(rpm -q --queryformat=%{postin} -p ${RPMPKG})" pre_install="$(rpm -q --queryformat=%{prein} -p ${RPMPKG})" post_uninstall="$(rpm -q --queryformat=%{postun} -p ${RPMPKG})" pre_uninstall="$(rpm -q --queryformat=%{preun} -p ${RPMPKG})" if [[ ${post_install} != \(none\) ]] ; then echo -e "post_install(){ ${post_install}\n}" >> ${BUILDDIR}/.INSTALL fi if [[ ${pre_install} != \(none\) ]] ; then echo -e "pre_install(){ ${pre_install}\n}" >> ${BUILDDIR}/.INSTALL fi if [[ ${post_uninstall} != \(none\) ]] ; then echo -e "post_uninstall(){ ${post_uninstall}\n}" >> ${BUILDDIR}/.INSTALL fi if [[ ${pre_uninstall} != \(none\) ]] ; then echo -e "pre_uninstall(){ ${pre_uninstall}\n}" >> ${BUILDDIR}/.INSTALL fi # Compressing the files. FPMPKG=${CURDIR}/${PKGNAME}-${PKGVER}-${ARCH}.fpm Fmessage "Creating fpm archive $(basename ${FPMPKG})... " cd ${BUILDDIR} if [ -f ${BUILDDIR}/.INSTALL ]; then tar -cvjf ${FPMPKG} .PKGINFO .FILELIST .INSTALL * 2> /dev/null > /dev/null else tar -cvjf ${FPMPKG} .PKGINFO .FILELIST * 2> /dev/null > /dev/null fi cd ${CURDIR} # Removing temporary build directory. Fmessage "Removing build dir... " rm -fr ${BUILDDIR} ${CPIOPKG} 2> /dev/null # Remove the rpm archive. if (( ${REMOVE} == 1 )) ; then Fmessage "Removing ${RPMPKG}... " rm -f ${RPMPKG} 2> /dev/null fi # Install the fpm archive. if (( ${INSTALL} == 1 )) ; then su -c "pacman -A ${FPMPKG}" fi exit 0