#!/bin/bash # Magellan Linux Installer Functions (mage.functions.sh) # version: 0.3.6-r13 mage_setup() { install -d $INSTALLDB } fatal_error() { #$1 is the missing file echo -e "\nFatal Error: Package seemed to be corrupt." echo -e "$1 was not found in ${PKGNAME}" exit 1 } build_unpackpkg() { tar xjmf ${PKGDIR}/${PKGNAME}.${PKGSUFFIX} -C ${BUILDDIR} } update_mtime() { local M_MTIME DB_ENTRY=${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/} #forcing mtime to same value echo echo -n ">>>> Forcing mtime to the same value ..." #find ${BUILDDIR}/${PKGNAME}/binfiles \ # -exec touch -m -r ${BUILDDIR}/${PKGNAME}/.mtime '{}' ';' \ # && echo done || echo false #get new M_MTIME from .mtime M_MTIME=$(stat -c %Y ${DB_ENTRY}/.mtime) #updating .files db entries; only if not empty, or they get killed if [ -e "${DB_ENTRY}/.files" ] then if [ -n "$(cat ${DB_ENTRY}/.files)" ] then #make backup mv ${DB_ENTRY}/.files \ ${DB_ENTRY}/.files-old #sets fieldseperator to "§" instead of " " IFS=§ #update while read pathto posix user group mtime md5sum do echo "${pathto}§${posix}§${user}§${group}§${M_MTIME}§${md5sum}" \ >> ${DB_ENTRY}/.files touch -m -r ${DB_ENTRY}/.mtime ${pathto} done < ${DB_ENTRY}/.files-old #remove old list rm -f ${DB_ENTRY}/.files-old #very important: unsetting the '§' fieldseperator unset IFS fi else fatal_error .files fi #updating .symlink db entries; only if not empty, or they get killed if [ -e "${DB_ENTRY}/.symlinks" ] then if [ -n "$(cat ${DB_ENTRY}/.symlinks)" ] then #make backup mv ${DB_ENTRY}/.symlinks \ ${DB_ENTRY}/.symlinks-old #sets fieldseperator to "§" instead of " " IFS=§ #update while read pathto posix link mtime do echo "${pathto}§${posix}§${link}§${M_MTIME}" \ >> ${DB_ENTRY}/.symlinks touch -m -r ${DB_ENTRY}/.mtime ${pathto} done < ${DB_ENTRY}/.symlinks-old #remove old list rm -f ${DB_ENTRY}/.symlinks-old #very important: unsetting the '§' fieldseperator unset IFS fi else fatal_error .symlinks fi } ################################################### # function install_direcories # # install_direcories $PKGNAME # ################################################### install_directories() { local PKGNAME PKGNAME=$1 if [ -e ${BUILDDIR}/${PKGNAME}/.dirs ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix user group do if [ ! -z "$pathto" ] then if [ "$VERBOSE" == "on" ] then echo -e "\t>>> DIR: $pathto" fi install -m $posix -o $user -g $group -d "$pathto" fi done < ${BUILDDIR}/${PKGNAME}/.dirs #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .dir fi } ################################################### # function install_files # # install_files $PKGNAME # ################################################### install_files(){ local PKGNAME local RETVAL local COUNTER local FILENAME local DESTINATION PKGNAME=$1 if [ -e ${BUILDDIR}/${PKGNAME}/.files ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix user group mtime md5sum do if [ ! -z "${pathto}" ] then if [ "${VERBOSE}" == "on" ] then echo -e "\t>>> FILE: ${pathto}" fi ### kleiner notfall fix ### if [ ! -d "$(dirname "${pathto}")" ] then install -d "$(dirname "${pathto}")" fi #when file exists, check if protected if [ -e "${pathto}" ] then is_config_protected "${pathto}" RETVAL=$? # 0 - not protected # # 1 - error # # 2 - protected # # 3 - protected but masked # case ${RETVAL} in #file is not protected - overwrite it 0|3) FILE="$pathto" install -m ${posix} -o ${user} -g ${group} \ ${BUILDDIR}/${PKGNAME}/binfiles/"$FILE" \ "$pathto" ;; #file is protected - write backup file # "._cfg${COUNTER}_{FILENAME} 2) #moves the cursor up echo -en \\033[A echo -e "${COLRED}! prot ${COLDEFAULT} === FILE: $pathto" FILENAME="$(basename "${pathto}")" FILE="$pathto" COUNTER=$(count_protected_files "${FILE}") DESTINATION="$(dirname "$pathto")/._cfg${COUNTER}_${FILENAME}" install -m ${posix} -o ${user} -g ${group} \ ${BUILDDIR}/${PKGNAME}/binfiles/"$FILE" \ "${DESTINATION}" #set run_etc_update=true PROTECT_COUNT=${PROTECT_COUNT}+1 ;; esac else #install file because it does not exists FILE="$pathto" install -m ${posix} -o ${user} -g ${group} \ ${BUILDDIR}/${PKGNAME}/binfiles/$FILE \ "$pathto" fi #not needed anymore ? #if [ ! -z $user ] #then # chown $user:$group $FILE ### <---- test #fi fi done < ${BUILDDIR}/${PKGNAME}/.files #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .files fi } ################################################### # function install_symlinks # # install_symlinks $PKGNAME # ################################################### install_symlinks() { local PKGNAME PKGNAME=$1 if [ -e ${BUILDDIR}/${PKGNAME}/.symlinks ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix link mtime do if [ ! -z "$pathto" ] then if [ "$VERBOSE" == "on" ] then echo -e "\t>>> LINK: $pathto" fi ln -snf $link "$pathto" fi done < ${BUILDDIR}/${PKGNAME}/.symlinks #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .symlinks fi } ################################################### # function install_blockdevices # # install_blockdevices $PKGNAME # ################################################### install_blockdevices() { local PKGNAME PKGNAME=$1 if [ -e ${BUILDDIR}/${PKGNAME}/.pipes ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix do if [ ! -z "$pathto" ] then if [ "$VERBOSE" == "on" ] then echo -e "\t>>> PIPE: $pathto" fi mkfifo -m $posix "$pathto" fi done < ${BUILDDIR}/${PKGNAME}/.pipes #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .pipes fi } ################################################### # function install_characterdevices # # install_characterdevices $PKGNAME # ################################################### install_characterdevices() { local PKGNAME PKGNAME=$1 if [ -e ${BUILDDIR}/${PKGNAME}/.char ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix do if [ ! -z "$pathto" ] then if [ "$VERBOSE" == "on" ] then echo -e "\t>>> CHAR: $pathto" fi mknode -m $posix -c "$pathto" fi done < ${BUILDDIR}/${PKGNAME}/.char #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .char fi #very important: unsetting the '§' fieldseperator unset IFS } ################################################### # function build_doinstall # # build_doinstall $PKGNAME # # NOTE: this is an wrapper do install packages # ################################################### build_doinstall() { #this is only a wrapper # NOTE: # !! we use § as field seperator !! # doing so prevent us to get errors by filenames with spaces local PKGNAME PKGNAME=$1 install_directories ${PKGNAME} install_files ${PKGNAME} install_symlinks ${PKGNAME} install_blockdevices ${PKGNAME} install_characterdevices ${PKGNAME} } ################################################### # function install_database_entry # # install_database_entry $PKGNAME $PKGTYPE # # PKGTYPE can be "virtual", "sources" or nothing # ################################################### install_database_entry(){ local PKGNAME local PKGTYPE PKGNAME=$1 PKGTYPE=$2 #add package to database install -d ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/} #install mage-file to database install -m 0644 -o root -g root \ ${MAGEFILE} \ ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/} # create fake file descriptors # used by virtual and source packages touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.dirs touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.symlinks touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.files touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.pipes touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.char touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.mtime #put category to database echo ${CATEGORIE} > ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.categorie #install PKGTYPE specific files case ${PKGTYPE} in virtual) touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.virtual ;; sources) touch ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/}/.sources ;; *) # normal packages needs these files cp -f ${BUILDDIR}/${PKGNAME}/.{char,dirs,files,pipes,symlinks,mtime} \ ${INSTALLDB}/${CATEGORIE}/${PKGNAME/-${ARCH}/} ;; esac } ################################################### # function remove_database_entry # # remove_database_entry $PKGNAME $PKGTYPE # # PKGTYPE can be "virtual", "sources" or nothing # ################################################### remove_database_entry(){ local PKGNAME local PKGTYPE PKGNAME=$1 PKGTYPE=$2 #removes database entry if [ -d ${INSTALLDB}/${PKGNAME} ] then rm -rf ${INSTALLDB}/${PKGNAME} fi } ################################################### # function compare_mtime # # compare_mtime $PKGNAME /path/to/file # # # # returns: # # 0=delete me # # 1=keep me # # # # compares mtime of given files in packages # ################################################### compare_mtime(){ local DBDIR local pathto local x DBDIR=$1 pathto=$2 M_MTIME=$(stat -c %Y ${INSTALLDB}/${DBDIR}/.mtime) #if $pathto is a symlink than compare linked binary if [ -L ${pathto} ] then #readlink -f resolves full path of linked file x=$(stat -c %Y "$(readlink -f "${pathto}")") else x=$(stat -c %Y "${pathto}") fi if [ "${M_MTIME}" -eq "${x}" ] then #echo "delete me: ${pathto}" return 0 else #echo "keep me : ${pathto}" return 1 fi } ################################################### # function remove_symlinks # # remove_symlinks $PKGNAME # ################################################### remove_symlinks() { local PKGNAME local RETVAL PKGNAME=$1 if [ -e ${INSTALLDB}/${PKGNAME}/.symlinks ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix link mtime do if [ ! -z "$pathto" ] then if [ -L "$pathto" ] then compare_mtime ${PKGNAME} "${pathto}" RETVAL=$? # 0=delete me # # 1=keep me # case ${RETVAL} in 0) echo -e "\t<<< LINK: $pathto" rm "$pathto" ;; 1) echo -e "${COLRED}! mtime${COLDEFAULT} === LINK: $pathto" ;; esac else echo -e "${COLRED}! exist${COLDEFAULT} === LINK: $pathto" fi fi done < ${INSTALLDB}/${PKGNAME}/.symlinks #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .symlinks fi } ################################################### # function remove_files # # remove_files $PKGNAME # ################################################### remove_files() { local PKGNAME local RETVAL PKGNAME=$1 #uninstall of files if [ -e ${INSTALLDB}/${PKGNAME}/.files ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix user group mtime md5sum do if [ ! -z "$pathto" ] then if [ -e "$pathto" ] then compare_mtime ${PKGNAME} "${pathto}" RETVAL=$? # 0=delete me # # 1=keep me # case ${RETVAL} in 0) echo -e "\t<<< FILE: $pathto" rm "$pathto" ;; 1) echo -e "${COLRED}! mtime${COLDEFAULT} === FILE: $pathto" ;; esac else echo -e "${COLRED}! exist${COLDEFAULT} === FILE: $pathto" fi fi done < ${INSTALLDB}/${PKGNAME}/.files #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .files fi } ################################################### # function remove_blockdevices # # remove_blockdevices $PKGNAME # ################################################### remove_blockdevices() { local PKGNAME PKGNAME=$1 if [ -e ${INSTALLDB}/${PKGNAME}/.pipes ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix do if [ ! -z "$pathto" ] then echo -e "\t<<< PIPE: $pathto" rm "$pathto" fi done < ${INSTALLDB}/${PKGNAME}/.pipes #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .pipes fi } ################################################### # function remove_characterdevices # # remove_characterdevices $PKGNAME # ################################################### remove_characterdevices() { local PKGNAME PKGNAME=$1 if [ -e ${INSTALLDB}/${PKGNAME}/.char ] then #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix do if [ ! -z "$pathto" ] then echo -e "\t<<< CHAR: $pathto" rm "$pathto" fi done < ${INSTALLDB}/${PKGNAME}/.char #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .char fi } ################################################### # function remove_direcories # # remove_direcories $PKGNAME # ################################################### remove_directories() { local PKGNAME PKGNAME=$1 #uninstall of dirs ## added small hack to fix dirs # must be reverse -> smage2 doesn't sort them if [ -e ${INSTALLDB}/${PKGNAME}/.dirs ] then #sort directory order mv ${INSTALLDB}/${PKGNAME}/.dirs ${INSTALLDB}/${PKGNAME}/.dirs_old cat ${INSTALLDB}/${PKGNAME}/.dirs_old | sort > ${INSTALLDB}/${PKGNAME}/.dirs #sets fieldseperator to "§" instead of " " IFS=§ while read pathto posix do if [ ! -z "$pathto" ] then if [ -e "$pathto" ] then echo -e "\t<<< DIR: $pathto" rmdir "$pathto" &> /dev/null if [ "$?" -ne "0" ] then #moves the cursor up echo -en \\033[A echo -e "${COLRED}! empty${COLDEFAULT} === DIR: $pathto" fi else echo -e "${COLRED}! exist${COLDEFAULT} === DIR: $pathto" fi fi done < ${INSTALLDB}/${PKGNAME}/.dirs #very important: unsetting the '§' fieldseperator unset IFS else fatal_error .dirs fi } ################################################### # function build_douninstall # # build_douninstall $PKGNAME # # NOTE: this is an wrapper do remove packages # ################################################### build_douninstall() { #this is only a wrapper # NOTE: # !! we use § as field seperator !! # doing so prevent us to get errors by filenames with spaces local PKGNAME PKGNAME=$1 remove_symlinks ${PKGNAME} remove_files ${PKGNAME} remove_blockdevices ${PKGNAME} remove_characterdevices ${PKGNAME} remove_directories ${PKGNAME} } getpackages() { if [ -z "$MIRRORS" ] then echo "You have no mirrors defined. Please edit your /etc/mage.rc." exit 1 fi local i for i in $MIRRORS do wget \ --passive-ftp \ --tries 3 \ --continue \ --progress bar \ --directory-prefix=${PKGDIR} \ ${i}/packages/${PKGNAME}.${PKGSUFFIX} if [ "$?" == "0" ] then break else continue fi done } syncmage() { if [ -z "$RSYNC" ] then echo "You have no rsync-mirrors defined. Please edit your /etc/mage.rc." exit 1 fi local i for i in $RSYNC do rsync \ --recursive \ --links \ --perms \ --times \ --devices \ --timeout=600 \ --verbose \ --compress \ --progress \ --stats \ --delete \ --delete-after \ $i $MAGEDIR if [ "$?" == "0" ] then break else continue fi done #clean up backup files (foo~) find ${MAGEDIR} -name *~ -exec rm '{}' ';' } cleanpkg(){ if [ -d "$PKGDIR" ] then echo -n "Removing downloaded packages... " rm -rf ${PKGDIR}/* echo "done." fi } ################################################### # function keepfiles # # keepfiles "$CATEGORIE/$PNAME" "$filename" # # note wildchars allowed # ################################################### keepfiles() { local name local keep name="`echo $1| cut -d '/' -f2`" keep="$2" DELPKG="`find ${INSTALLDB} -name ${name}*.mage`" DELDIR="${INSTALLDB}/$(basename ${DELPKG} .mage)" cp ${DELDIR}/.files ${DELDIR}/.files-orig sed "s:${keep}::" \ ${DELDIR}/.files-orig > ${DELDIR}/.files rm ${DELDIR}/.files-orig } ################################################### # function injectpkg # # injectpkg "$CATEGORIE/$PNAME" # # note wildchars allowed # ################################################### injectpkg() { local name local categorie local magename name="`echo $1| cut -d '/' -f2`" categorie="`echo $1| cut -d '/' -f1`" INJPKG="`find ${MAGEDIR} -name ${name}-*.mage`" for i in ${INJPKG} do magename="$(basename ${INJPKG} .mage)" echo -e "Injecting fake package for ${COLBLUE}${categorie}${COLDEFAULT}/${COLGREEN}${magename}${COLDEFAULT}" install -d ${INSTALLDB}/${magename} touch ${INSTALLDB}/${magename}/{.injected,.files,.dirs,.symlinks,.pipes,.char} #installs magefile install -m 0644 -o root -g root \ ${MAGEDIR}/${categorie}/${magename}.mage \ ${INSTALLDB}/${magename} done } ################################################### # function reminjected # # reminjected # # note: removes all injected packages # ################################################### reminjected() { DELINJ="`find ${INSTALLDB} -name .injected`" for i in ${DELINJ} do magename=$(dirname ${i}) if [ -d "${magename}" ] then # small fix to protect the mage-db deleting itself, that is not so funny :) if [ "${magename}" != "${INSTALLDB}" ] then echo -e "removing fake package ${COLRED}${magename#${INSTALLDB}/*}${COLDEFAULT}" rm -rf ${magename} fi fi done } xtitle() { if [ ${TERM} == "xterm" ] then echo -ne "\033]0;Mage: $1\007" fi return 0 } xtitleclean() { if [ ${TERM} == "xterm" ] then echo -ne "\033]0;\007" fi return 0 } spin_processbar() { # does not works as it should -- disabled # you musst add and remove a tempfile outside this funktion # to leave this funktion ... # before you call this function do this: # # touch /var/tmp/proz # build_doinstall|spin_processbar echo -en "Processing: " while [ -f /var/tmp/proz ] do echo -en "-\b" sleep 0.01 echo -en "\\ \b\b" sleep 0.01 echo -en "|\b" sleep 0.01 echo -en "/\b" sleep 0.01 done echo "done" return 0 # when you want to leave the funktion do # something like this: # # #remove spinner tempfile # if [ "$VERBOSE" == "off" ] # then # if [ -f /var/tmp/proz ] # then # rm /var/tmp/proz # fi # fi } #cuts full pathnames or versioniezed names down to basename choppkgname(){ #we want this only if full name was used if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ] then #cuts ARCH and PBUILD #ARCH comes from /etc/mage.rc MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}-r*.::g") #cuts version number MAGENAME=$(basename ${MAGENAME%-*} .mage) fi } # get_categorie $PKGNAME, returns CATEGORIE # ret 0=ok, 1=not_found getcategorie(){ for CAT in ${MAGEDIR}/* do if [ -d ${CAT}/${MAGENAME} ] then #debug only if [ "$MAGEDEBUG" == "on" ] then echo "$MAGENAME found -> $CAT/$MAGENAME" echo "categorie = $(basename ${CAT})" fi CATEGORIE=$(basename ${CAT}) fi done #package does not exists if [ -z "$CATEGORIE" ] then echo -e "Package "${COLRED}"\"${MAGENAME}\""${COLDEFAULT}" does not exists." exit 1 fi } #check_stable_package /path/to/foo.mage # returns 0=stable 1=unstable check_stable_package(){ #be sure no previous $STATES exists unset STATE source $1 #if USE_UNSTABLE=true this must be prevented if [ "${USE_UNSTABLE}" != "true" ] then if [ "${STATE}" != "stable" ] then return 1 else if [ "${STATE}" == "old" ] then return 1 fi fi fi #unset the sourced varibales; #we'll got bad problems later on if we don't do this unset PKGNAME unset STATE unset DESCRIPTION unset HOMEPAGE unset DEPEND unset SDEPEND unset PROVIDE unset PKGTYPE unset preinstall unset postinstall return 0 } #get_highest_magefile ${CATEGORIE} ${PKGNAME} #fake at moment returns only stable pkgs (must set to be one) # return $HIGHEST_MAGEFILE get_highest_magefile(){ unset HIGHEST_MAGEFILE #CATEGORIE=$1 #PKGNAME=$2 for i in ${MAGEDIR}/${CATEGORIE}/${MAGENAME}/* do #we exclude subdirs (for stuff like a md5sum dir) if [ ! -d ${i} ] then check_stable_package ${i} if [ "$?" == "0" ] then HIGHEST_MAGEFILE=${i} #for debug only if [ "$MAGEDEBUG" == "on" ] then echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}" fi fi fi done # stop here if HIGHEST_MAGEFILE is zero # this package must be unstable or old if [ -z "${HIGHEST_MAGEFILE}" ] then echo echo -n "All packages named " echo -en ${COLRED}\""${PKGNAME%-*-*-*}\""${COLDEFAULT} echo -n " are marked " echo -en ${COLRED}"*UNSTABLE*"${COLDEFAULT} echo "." echo "You need to declare USE_UNSTABLE=true to install this." echo echo "Example:" echo " USE_UNSTABLE=true mage install ${PKGNAME%-*-*-*}" echo echo "Be warned that these packages are not stable and may cause serious problems." echo "You should know what you are doing, so don't complain about any damage." echo return 1 fi MAGEFILE=${HIGHEST_MAGEFILE} return 0 } ################################################### # function is_config_protected # # is_config_protected /path/to/file # # # # returns: # # 0 - not protected # # 1 - error # # 2 - protected # # 3 - protected but masked # # # ################################################### is_config_protected() { local EXPFILE local TEST local PROTECTED local IFS EXPFILE=$1 #to be safe; it may be '§' IFS=' ' #check ob in config protect for i in ${CONFIG_PROTECT} do #ersetzen von $i nur wenn am anfang der variable TEST="${EXPFILE/#${i}/Protected}" if [ "${TEST}" != "${EXPFILE}" ] then #setzen das es protected ist PROTECTED=TRUE #check ob nicht doch maskiert for x in ${CONFIG_PROTECT_MASK} do TEST="${EXPFILE/#${x}/Protect_Masked}" if [ "${TEST}" != "${EXPFILE}" ] then PROTECTED=MASKED fi done fi done unset IFS case ${PROTECTED} in TRUE) #echo "I'm protected" return 2 ;; MASKED) #echo "I'm protected, but masked - delete me" return 3 ;; *) #echo "delete me" return 0 ;; esac } ################################################### # function count_protected_files # # count_protected_files /path/to/file # # # # note: prints number of protected files # # exp: 0012 # ################################################### count_protected_files() { ${MLIBDIR}/writeprotected "$1" } #call with 'get_magefile_to_uninstall SEARCHSTRING' #returns /path/to/magefile get_magefile_to_uninstall() { local RESULT local SSTRING local i local x local z SSTRING=$1 for i in ${INSTALLDB}/*/* do #echo ${i} #to be sure if [ -d ${i} ] then #echo "DEBUG: stripped i: $(basename ${i%-*-*})" if [ $(basename ${i%-*-*}) == ${SSTRING} ] then #RESULT="${RESULT} ${i}" CATEGORIE=$(< ${i}/.categorie) #echo ${CATEGORIE}/$(basename ${RESULT}) RESULT="${RESULT} ${CATEGORIE}/$(basename ${i})" fi fi done if [ -n "${RESULT}" ] then #check if ONE package was found and not more declare -i x=0 for i in ${RESULT} do x=${x}+1 done if [ ${x} -eq 1 ] then #echo "jo ich uninstall $(basename ${RESULT}) ..." #the mage file #echo "${RESULT}/$(basename ${RESULT}).mage" #gets categorie of the package #CATEGORIE=$(< ${i}/.categorie) #echo ${CATEGORIE}/$(basename ${RESULT}) echo "${RESULT}" return 0 else #echo "More than one package found ..." echo "${RESULT}" return 2 fi else #echo "No package found named '${SSTRING}'." echo "${SSTRING}" return 1 fi } # reads virtualdb file #$1 = virtualname; $2 commands: showpkgs, showline #return 0 == installed -> shows installed pkg as well #return 1 == not installed virtuals_read() { local VIRTUAL_NAME COMMAND VIRT_LINE line x i VIRTUAL_NAME=$1 COMMAND=$2 # parse file to get virtual_name line IFS=$'\n' for line in $(< ${VIRTUALDB_FILE}) do IFS=$' ' for x in ${line} do if [ "${x}" == "${VIRTUAL_NAME}" ] then VIRT_LINE="${line}" if [ "${COMMAND}" == "showline" ] then #give out the non stripped line echo "${line}" fi fi done IFS=$'\n' done unset IFS #now read the packages linked to VIRTUAL_NAME and output them if [ -n "${VIRT_LINE}" ] then if [ "${COMMAND}" == "showpkgs" ] then declare -i x=0 for i in ${VIRT_LINE} do if [ ${x} -ge 1 ] then echo "${i}" fi ((x++)) done fi return 0 fi return 1 } #add pkg to virtualdb # $1 == virtualname $2= pkgname # retvals: 0=ok,added; 1=error; 3=pkg already in virtual virtuals_add(){ local VIRTUAL_NAME PKG_NAME OLD_LINE line i VIRTUAL_NAME=$1 PKG_NAME=$2 if virtuals_read ${VIRTUAL_NAME} then #make shure ${PKG_NAME} is *not* in ${VIRTUAL_NAME} already for i in $(virtuals_read ${VIRTUAL_NAME} showpkgs) do if [ "${i}" == ${PKG_NAME} ] then echo "==== ${PKG_NAME} already linked with ${VIRTUAL_NAME} ..." return 3 fi done echo ">>>> Updating ${VIRTUAL_NAME} with ${PKG_NAME} in virtual database ..." OLD_LINE="$(virtuals_read ${VIRTUAL_NAME} showline)" #make a backup mv ${VIRTUALDB_FILE} ${VIRTUALDB_FILE}.old IFS=$'\n' for line in $(< ${VIRTUALDB_FILE}.old) do #if the right line, append ${PKG_NAME}, else do nothing if [ "${line}" == "${OLD_LINE}" ] then echo "${line} ${PKG_NAME}" >> ${VIRTUALDB_FILE} else echo "${line}" >> ${VIRTUALDB_FILE} fi done unset IFS else echo ">>>> Adding ${PKG_NAME} as ${VIRTUAL_NAME} to virtual database ..." echo "${VIRTUAL_NAME} ${PKG_NAME}" >> ${VIRTUALDB_FILE} \ && echo "ok" || echo "false" fi return 0 } #deletes pakages from virtual database #$1 virtualname; $2 pkgname virtuals_del() { local VIRTUAL_NAME PKG_NAME OLD_LINE METHOD line i x PKG_INSTALLED VIRTUAL_NAME=$1 PKG_NAME=$2 #first check if exists if virtuals_read ${VIRTUAL_NAME} then #get method -> delall or update and check if ${PKG_NAME} exists in ${VIRTUAL_NAME} declare -i x=0 for i in $(virtuals_read ${VIRTUAL_NAME} showpkgs) do if [ "${i}" == "${PKG_NAME}" ] then PKG_INSTALLED=true fi ((x++)) done #abort if not installed if [ "${PKG_INSTALLED}" != "true" ] then echo "!!!! ${PKG_NAME} does not exists in ${VIRTUAL_NAME}." return 0 fi if [ ${x} -ge 2 ] then METHOD=update else METHOD=delall fi #get the complete line OLD_LINE="$(virtuals_read ${VIRTUAL_NAME} showline)" #make a backup mv ${VIRTUALDB_FILE} ${VIRTUALDB_FILE}.old #parse virtualdb IFS=$'\n' for line in $(< ${VIRTUALDB_FILE}.old) do if [ "${line}" == "${OLD_LINE}" ] then #delall or update? case ${METHOD} in update) echo "<<<< Unlinking ${PKG_NAME} from ${VIRTUAL_NAME} in virtual database ..." #del PKG_NAME from line echo "${line/ ${PKG_NAME}/}" >> ${VIRTUALDB_FILE} ;; delall) echo "<<<< Deleting ${VIRTUAL_NAME} in virtual database ..." #continue; do not write anything continue ;; esac else echo "${line}" >> ${VIRTUALDB_FILE} fi done unset IFS else echo "!!!! ${VIRTUAL_NAME} does not exists in virtual database." fi } # gets real pkgname from virtuals.default #$1=VIRTUAL_NAME; returns PKG_NAME default_virtualname_to_pkgname(){ local VIRTUAL_NAME PKG_NAME db_virtualname db_pkgname VIRTUAL_NAME=$1 while read db_virtualname db_pkgname do if [ "${db_virtualname}" == "${VIRTUAL_NAME}" ] then PKG_NAME="${db_pkgname}" fi done << EOF $(< ${VIRTUALDB_DEFAULTS}) EOF if [ -n "${PKG_NAME}" ] then echo "${PKG_NAME}" fi }