#!/bin/bash source /etc/mage.rc.global source /etc/mage.rc #### fixups #### MLIBDIR=/home/tjoke/svn/magellan-source/branches/mage-sql/usr/lib/mage INSTALLDB=/var/db/mage ################# source ${MLIBDIR}/mage4.functions.sh source ${MLIBDIR}/sql_functions.sh die() { echo -e "Exited ${BASH_SOURCE} at line no ${BASH_LINENO}." echo -e "$@" exit 1 } sql() { local sqlcmd="$*" [[ -z ${sqlcmd} ]] && die "no sqlcmd given." sqlite3 -nullvalue 'NULL' -list -separator '|' ${DBFILE} << EOF || die "error running '$@'" ${sqlcmd}; EOF } split_save_variables() { export SAVED_PNAME="${PNAME}" export SAVED_PVER="${PVER}" export SAVED_PBUILD="${PBUILD}" export SAVED_PCATEGORIE="${PCATEGORIE}" export SAVED_DESCRIPTION="${DESCRIPTION}" export SAVED_HOMEPAGE="${HOMEPAGE}" export SAVED_SPECIAL_VARS="${SPECIAL_VARS}" export SAVED_STATE="${STATE}" export SAVED_PKGTYPE="${PKGTYPE}" export SAVED_INHERITS="${INHERITS}" export SAVED_DEPEND="${DEPEND}" export SAVED_SDEPEND="${SDEPEND}" export SAVED_PROVIDE="${PROVIDE}" export SAVED_NOPKGBUILD="${NOPKGBUILD}" # bindir too export SAVED_BINDIR="${BINDIR}" # export the SPLIT_PACKAGE_BASE export SPLIT_PACKAGE_BASE="${SAVED_PNAME}" # functions if [[ ! -z $(typeset -f preinstall) ]] then # rename the old one local saved_preinstall saved_preinstall=SAVED_$(typeset -f preinstall) eval "${saved_preinstall}" export -f SAVED_preinstall fi if [[ ! -z $(typeset -f postinstall) ]] then # rename the old one local saved_postinstall saved_postinstall=SAVED_$(typeset -f postinstall) eval "${saved_postinstall}" export -f SAVED_postinstall fi if [[ ! -z $(typeset -f preremove) ]] then # rename the old one local saved_preremove saved_preremove=SAVED_$(typeset -f preremove) eval "${saved_preremove}" export -f SAVED_preremove fi if [[ ! -z $(typeset -f postremove) ]] then # rename the old one local saved_postremove saved_postremove=SAVED_$(typeset -f postremove) eval "${saved_postremove}" export -f SAVED_postremove fi } split_restore_variables() { export PNAME="${SAVED_PNAME}" export PVER="${SAVED_PVER}" export PBUILD="${SAVED_PBUILD}" export PCATEGORIE="${SAVED_PCATEGORIE}" export DESCRIPTION="${SAVED_DESCRIPTION}" export HOMEPAGE="${SAVED_HOMEPAGE}" export SPECIAL_VARS="${SAVED_SPECIAL_VARS}" export STATE="${SAVED_STATE}" export PKGTYPE="${SAVED_PKGTYPE}" export INHERITS="${SAVED_INHERITS}" export DEPEND="${SAVED_DEPEND}" export SDEPEND="${SAVED_SDEPEND}" export PROVIDE="${SAVED_PROVIDE}" export NOPKGBUILD="${SAVED_NOPKGBUILD}" # bindir too export BINDIR="${SAVED_BINDIR}" # functions if [[ ! -z $(typeset -f SAVED_preinstall) ]] then # rename the old one local saved_preinstall saved_preinstall=$(typeset -f SAVED_preinstall) eval "${saved_preinstall/SAVED_/}" export -f preinstall fi if [[ ! -z $(typeset -f SAVED_postinstall) ]] then # rename the old one local saved_postinstall saved_postinstall=$(typeset -f SAVED_postinstall) eval "${saved_postinstall/SAVED_/}" export -f postinstall fi if [[ ! -z $(typeset -f SAVED_preremove) ]] then # rename the old one local saved_preremove saved_preremove=$(typeset -f SAVED_preremove) eval "${saved_preremove/SAVED_/}" export -f preremove fi if [[ ! -z $(typeset -f SAVED_postremove) ]] then # rename the old one local saved_postremove saved_postremove=$(typeset -f SAVED_postremove) eval "${saved_postremove/SAVED_/}" export -f postremove fi } split_unset_variables() { # unset saved vars; not needed anymore unset SAVED_PNAME unset SAVED_PVER unset SAVED_PBUILD unset SAVED_PCATEGORIE unset SAVED_DESCRIPTION unset SAVED_HOMEPAGE unset SAVED_SPECIAL_VARS unset SAVED_STATE unset SAVED_PKGTYPE unset SAVED_INHERITS unset SAVED_DEPEND unset SAVED_SDEPEND unset SAVED_PROVIDE unset SAVED_BINDIR unset SAVED_NOPKGBUILD unset SPLIT_PACKAGE_BASE unset -f SAVED_preinstall unset -f SAVED_postinstall unset -f SAVED_preremove unset -f SAVED_postremove } create_database_layout() { sql "create table categories(id integer primary key, pcat text unique)" sql "create table packages(id integer primary key, pname text, pver text, pbuild text, pcat_id numeric, state text, provide text, pkgtype text )" sql "create table depends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)" sql "create table sdepends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)" sql "create table packages_info(id integer primary key, pkg_id numeric, arch text, md5 text, mtime numeric, homepage text, description text, size numeric)" #sql "create table packages_content(id integer primary key, pkginfo_id numeric, char text, dirs text, files text, pipes text, symlinks text)" sql "create table packages_content(id integer primary key, pkginfo_id numeric, char blob, dirs blob, files blob, pipes blob, symlinks blob)" # sql "create table packages_routines(id integer primary key, # pkg_id numeric, # mincludes text, # preinstall text, # postinstall text, # preremove text, # postremove text, # extra_functions text, # extra_vars text)" sql "create table packages_routines(id integer primary key, pkg_id numeric, mincludes text, preinstall numeric, postinstall numeric, preremove numeric, postremove numeric, script text)" } smage2sqlite() { local i local pcatid local pkgid local dep local sym local depname local depver local depbuild local depcat local depcatid local md5file local md5sum pcatid=$(sql "select id from categories where pcat='${PCATEGORIE}'") sql "insert into packages( pname, pver, pbuild, pcat_id, state, provide, pkgtype ) values( '${PNAME}', '${PVER}', '${PBUILD}', '${pcatid}', '${STATE}', '${PROVIDE}', '${PKGTYPE}' )" pkgid=$(sql "select id from packages where pname='${PNAME}' and pver='${PVER}' and pbuild='${PBUILD}'") # depends while read sym dep do # ignore scripts atm -FIXME!!! case ${sym} in \$\(*) continue ;; esac [[ -z ${dep} ]] && continue depcat="$(dep2pcat ${dep})" depcatid=$(sql "select id from categories where pcat='${depcat}'") depname="$(dep2pname ${dep})" depver="$(dep2pver ${dep})" if [[ -z ${depcatid} ]] then die "Error: '${PNAME}' -> depcat '${depcat}' has no depcatid, it does no exist in database!" fi sql "insert into depends(pkg_id, relation, pcat_id, pname, pver, pbuild) values('${pkgid}', '${sym}', '${depcatid}', '${depname}', '${depver}', '${depbuild}')" done << EOF ${DEPEND} EOF # sdepends while read sym dep do [[ -z ${dep} ]] && continue depcat="$(dep2pcat ${dep})" depcatid=$(sql "select id from categories where pcat='${depcat}'") depname="$(dep2pname ${dep})" depver="$(dep2pver ${dep})" sql "insert into sdepends(pkg_id, relation, pcat_id, pname, pver, pbuild) values('${pkgid}', '${sym}', '${depcatid}', '${depname}', '${depver}', '${depbuild}')" done << EOF ${SDEPEND} EOF for arch in i686 x86_64 do md5file="${MAGEDIR}/${PCATEGORIE}/${PNAME}/md5/${PNAME}-${PVER}-${arch}-r${PBUILD}.md5" [[ -f ${md5file} ]] && md5sum="$(cat ${md5file} | cut -d' ' -f1)" # fix descriptions DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)" sql "insert into packages_info(pkg_id, arch, md5, description, homepage) values('${pkgid}','${arch}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')" done # using $INHERITS directly #mincludes="$(grep -r 'minclude ' ${magefile} | cut -d: -f2 | sed s:minclude\ ::)" # # # # # # install routines & includes # # # # # # needs special magic: the last line of typeset has no ';' # # # but to eval the function we must append it # # # so this sed deletes the last line of the typeset output # # # and replaces it with an ;} and a newline between # # # # # # but we will not add this fun when no postinstall etc are given # # # -> [[ -n $(typeset -f postinstall ]] && # # # # # # also the character ' is a problem while importing to sql # # # because we use ' as escape char. so we replace it with '' # # # # # # $([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}") # # # # # sql "insert into packages_routines(pkg_id, # # mincludes, # # postinstall, # # preinstall, # # preremove, # # postremove) # # values('${pkgid}', # # '${mincludes}', # # '$([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}")', # # '$([[ -n $(typeset -f preinstall) ]] && echo -e "$(typeset -f preinstall | sed s:\':\'\':g | sed '$d');\n}")', # # '$([[ -n $(typeset -f preremove) ]] && echo -e "$(typeset -f preremove | sed s:\':\'\':g | sed '$d');\n}")', # # '$([[ -n $(typeset -f postremove) ]] && echo -e "$(typeset -f postremove | sed s:\':\'\':g | sed '$d');\n}")')" local do_preinst=0 local do_postinst=0 local do_prerm=0 local do_postrm=0 local do_script=0 local script="" # # [[ -n $(typeset -f preinstall) ]] && do_preinst=1 && do_script=1 # # [[ -n $(typeset -f postinstall) ]] && do_postinst=1 && do_script=1 # # [[ -n $(typeset -f preremove) ]] && do_prerm=1 && do_script=1 # # [[ -n $(typeset -f postremove) ]] && do_postrm=1 && do_script=1 # # # # [[ ${do_script} = 1 ]] && script="$(basename ${DBFILE} .sql).routines/${pkgid}/routines.sh" [[ -n $(typeset -f preinstall) ]] && do_preinst=1 && do_script=1 [[ -n $(typeset -f postinstall) ]] && do_postinst=1 && do_script=1 [[ -n $(typeset -f preremove) ]] && do_prerm=1 && do_script=1 [[ -n $(typeset -f postremove) ]] && do_postrm=1 && do_script=1 [[ ${do_script} = 1 ]] && script="$(basename ${DBFILE} .sql).data/${PCATEGORIE}/${PNAME}-${PVER}-${PBUILD}/routines.sh" sql "insert into packages_routines(pkg_id, mincludes, postinstall, preinstall, preremove, postremove, script) values('${pkgid}', '${INHERITS}', '${do_postinst}', '${do_pretinst}', '${do_prerm}', '${do_postrm}', '${script}')" # # # create it only if do_script=1 # # if [[ ${do_script} = 1 ]] # # then # # # create a routines script # # local routines=$(dirname ${DBFILE})/${script} # # install -d $(dirname ${routines}) # # # # echo '#!/bin/sh' > ${routines} # # echo "# routines.sh script for ${pkgid}, ${PNAME}-${PVER}-${PBUILD}" >> ${routines} # # echo >> ${routines} # # # # # # # special functions and variables # # if [[ -n ${SPECIAL_VARS} ]] # # then # # local i # # for i in ${SPECIAL_VARS} # # do # # # being tricky here :) # # echo "${i}=\"$(eval echo \$${i})\"" >> ${routines} # # done # # echo >> ${routines} # # fi # # # # # add special functions # # if [[ -n ${SPECIAL_FUNCTIONS} ]] # # then # # local i # # for i in ${SPECIAL_FUNCTIONS} # # do # # # add to mage (quotes needed !) # # typeset -f "${i}" >> ${routines} # # echo >> ${routines} # # # unset to be safe (quotes needed !) # # unset "${i}" # # done # # echo >> ${routines} # # fi # # # # # postinstall and co # # for i in preinstall postinstall preremove postremove # # do # # if [[ -n $(typeset -f "${i}") ]] # # then # # typeset -f "${i}" >> ${routines} # # echo >> ${routines} # # fi # # done # # # # # create start logic # # echo >> ${routines} # # echo 'case $1 in' >> ${routines} # # echo ' preinstall) preinstall ;;' >> ${routines} # # echo ' postinstall) postinstall ;;' >> ${routines} # # echo ' preremove) preremove ;;' >> ${routines} # # echo ' postremove) postremove ;;' >> ${routines} # # echo 'esac' >> ${routines} # # echo >> ${routines} # # fi # create it only if do_script=1 if [[ ${do_script} = 1 ]] then # create a routines script local routines=$(dirname ${DBFILE})/${script} install -d $(dirname ${routines}) echo '#!/bin/sh' > ${routines} echo "# routines.sh script for ${PCATEGORIE}/${PNAME}-${PVER}-${PBUILD}" >> ${routines} echo >> ${routines} # special functions and variables if [[ -n ${SPECIAL_VARS} ]] then local i for i in ${SPECIAL_VARS} do # being tricky here :) echo "${i}=\"$(eval echo \$${i})\"" >> ${routines} done echo >> ${routines} fi # add special functions if [[ -n ${SPECIAL_FUNCTIONS} ]] then local i for i in ${SPECIAL_FUNCTIONS} do # add to mage (quotes needed !) typeset -f "${i}" >> ${routines} echo >> ${routines} # unset to be safe (quotes needed !) unset "${i}" done echo >> ${routines} fi # postinstall and co for i in preinstall postinstall preremove postremove do if [[ -n $(typeset -f "${i}") ]] then typeset -f "${i}" >> ${routines} echo >> ${routines} else echo "${i} () { return 0; }" >> ${routines} fi done # create start logic echo >> ${routines} echo 'case $1 in' >> ${routines} echo ' preinstall) preinstall ;;' >> ${routines} echo ' postinstall) postinstall ;;' >> ${routines} echo ' preremove) preremove ;;' >> ${routines} echo ' postremove) postremove ;;' >> ${routines} echo 'esac' >> ${routines} echo >> ${routines} fi unset PCATEGORIE unset PNAME unset PVER unset PBUILD unset STATE unset DESCRIPTION unset HOMEPAGE unset PKGTYPE unset PKGNAME unset DEPEND unset SDEPEND unset PROVIDE unset SPECIAL_VARS unset SPECIAL_FUNCTIONS unset INHERITS unset SPLIT_PACKAGES # unset these functions unset -f preinstall unset -f postinstall unset -f preremove unset -f postremove } categories2sqlite() { local file="$1" if [[ ! -f ${file} ]] then echo "${file} does not exist, ignoring..." return 0 fi echo "using categories from ${file}..." # create categories cat ${file} | while read pcat do case ${pcat} in include|profiles) continue;; \#*|"") continue ;; esac # "upsert" if [[ -z $(sql "select pcat from categories where pcat='${pcat}'") ]] then sql "insert into categories (pcat) values('${pcat}')" fi done } convert_smage_db() { local smagefile local PCATEGORIE local PNAME local PVER local PBUILD local STATE local PKGNAME local DESCRIPTION local HOMEPAGE local DEPEND local SDEPEND local PROVIDE local PKGTYPE local SPLIT_PACKAGES local preinstall local postinstall local preremove local postremove # create categories - global categories2sqlite "${SMAGESCRIPTSDIR}/.known_categories" # create categories - repo specific categories2sqlite "${SMAGESCRIPTSDIR}/${REPO}/.known_categories" # create packages for smagefile in $(find ${SMAGESCRIPTSDIR}/${REPO} -type f -name \*.smage2 | sort) do case ${DEBUG} in 1|true) echo "converting ${smagefile}" ;; esac source ${smagefile} # build several subpackages if [[ -n ${SPLIT_PACKAGES} ]] then split_save_variables for subpackage in ${SPLIT_PACKAGES} do echo "Running for subpackage '${subpackage}'..." # get the right variables for the split export PNAME="${subpackage}" split_info_${subpackage} smage2sqlite # restore smage environment split_restore_variables done # unset all saved smage variables split_unset_variables else smage2sqlite fi unset PCATEGORIE unset PNAME unset PVER unset PBUILD unset STATE unset DESCRIPTION unset HOMEPAGE unset PKGTYPE unset PKGNAME unset DEPEND unset SDEPEND unset PROVIDE unset SPECIAL_VARS unset SPECIAL_FUNCTIONS unset INHERITS unset SPLIT_PACKAGES # unset these functions unset -f preinstall unset -f postinstall unset -f preremove unset -f postremove done } convert_install_db() { local i local magefile local pcat local pname local pver local pbuild local pcatid local state local preinstall local postinstall local preremove local postremove local pkgid local dep local PKGNAME local STATE local DESCRIPTION local HOMEPAGE local DEPEND local SDEPEND local PROVIDE local PKGTYPE local preinstall local postinstall local preremove local postremove local sym local depname local depver local depbuild local depcat local depcatid local md5file local md5sum local pkginfoid local entrydir # first of all add virtual categorie sql "insert into categories(pcat) values('virtual')" # create categories for i in virtual ${INSTALLDB}/* do pcat="$(basename ${i})" case ${pcat} in include|profiles) continue;; esac # "upsert" if [[ -z $(sql "select pcat from categories where pcat='${pcat}'") ]] then sql "insert into categories (pcat) values('${pcat}')" fi done # create packages for magefile in $(find ${INSTALLDB} -type f -name *.mage| sort) do case ${DEBUG} in 1|true) echo "converting ${magefile}" ;; esac pcat="$(magename2pcat ${magefile})" pname="$(magename2pname ${magefile})" pver="$(magename2pver ${magefile})" pbuild="$(magename2pbuild ${magefile})" source ${magefile} pcatid=$(sql "select id from categories where pcat='${pcat}'") sql "insert into packages( pname, pver, pbuild, pcat_id, state, provide, pkgtype ) values( '${pname}', '${pver}', '${pbuild}', '${pcatid}', '${STATE}', '${PROVIDE}', '${PKGTYPE}' )" pkgid=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'") # depends while read sym dep do [[ -z ${dep} ]] && continue depcat="$(dep2pcat ${dep})" depcatid=$(sql "select id from categories where pcat='${depcat}'") depname="$(dep2pname ${dep})" depver="$(dep2pver ${dep})" sql "insert into depends(pkg_id, relation, pcat_id, pname, pver, pbuild) values('${pkgid}', '${sym}', '${depcatid}', '${depname}', '${depver}', '${depbuild}')" done << EOF ${DEPEND} EOF # sdepends while read sym dep do [[ -z ${dep} ]] && continue depcat="$(dep2pcat ${dep})" depcatid=$(sql "select id from categories where pcat='${depcat}'") depname="$(dep2pname ${dep})" depver="$(dep2pver ${dep})" sql "insert into sdepends(pkg_id, relation, pcat_id, pname, pver, pbuild) values('${pkgid}', '${sym}', '${depcatid}', '${depname}', '${depver}', '${depbuild}')" done << EOF ${SDEPEND} EOF DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)" sql "insert into packages_info(pkg_id, arch, md5, description, homepage) values('${pkgid}','${ARCH}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')" # install routines & includes sql "insert into packages_routines(pkg_id, mincludes, postinstall, preinstall, preremove, postremove) values('${pkgid}', '${mincludes}', '$(typeset -f postinstall | sed s:\':\'\':g)', '$(typeset -f preinstall | sed s:\':\'\':g)', '$(typeset -f preremove | sed s:\':\'\':g)', '$(typeset -f postremove | sed s:\':\'\':g)')" # # #### #### # # # record contents # # # #### #### # # pkginfoid=$(sql "select id from packages_info where pkg_id='${pkgid}' and arch='${ARCH}'") # # entrydir="$(dirname ${magefile})" # # sql "insert into packages_content(pkginfo_id, # # char, # # dirs, # # files, # # pipes, # # symlinks) # # values('${pkginfoid}', # # '$(basename ${DBFILE} .sql).records/${pkginfoid}/char.bz2', # # '$(basename ${DBFILE} .sql).records/${pkginfoid}/dirs.bz2', # # '$(basename ${DBFILE} .sql).records/${pkginfoid}/files.bz2', # # '$(basename ${DBFILE} .sql).records/${pkginfoid}/pipes.bz2', # # '$(basename ${DBFILE} .sql).records/${pkginfoid}/symlinks.bz2')" # # # create compressed content files # # local entryfile # # for entryfile in char dirs files pipes symlinks # # do # # install -d $(dirname ${DBFILE})/$(basename ${DBFILE} .sql).records/${pkginfoid} # # cat ${entrydir}/.${entryfile} | bzip2 -9 >> $(dirname ${DBFILE})/$(basename ${DBFILE} .sql).records/${pkginfoid}/${entryfile}.bz2 # # done unset PKGNAME unset DEPEND unset SDEPEND unset PROVIDE unset SPECIAL_VARS unset SPECIAL_FUNCTIONS # unset these functions unset -f preinstall unset -f postinstall unset -f preremove unset -f postremove done } convert_virtual_defaults() { local virtual local package local vcat local vname local pcat local pname sql "create table virtual_defaults(id integer primary key, vcat text,vname text, pcat text, pname text)" while read virtual package do vcat="$(dirname ${virtual})" vname="$(basename ${virtual})" pcat="$(dirname ${package})" pname="$(basename ${package})" sql "insert into virtual_defaults(vcat,vname,pcat,pname) values('${vcat}','${vname}','${pcat}','${pname}')" done < /etc/mage-profile/virtuals.defaults } METHOD="$1" DBFILE="$2" REPO="$3" [[ -z ${METHOD} ]] && die "No method given. Use --install, --packages or --virtual for your appropiate task" [[ -z ${DBFILE} ]] && die "No database given." [[ -z ${REPO} ]] && die "No repository given." case $1 in --install|-i) create_database_layout convert_install_db ;; --packages|-p) convert_virtual_defaults create_database_layout convert_smage_db ;; # --virtuals|-v) # convert_virtual_defaults # ;; *) die "Unkown method." ;; esac