#!/bin/bash # # scratchpkg # # Copyright (c) 2018 by Emmett1 (emmett1.2miligrams@gmail.com) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # RED='\e[0;31m' GREEN='\e[0;32m' YELLOW='\e[0;33m' CYAN='\e[0;36m' PURPLE='\e[0;35m' CRESET='\e[0m' msg() { echo -e "${GREEN}==>${CRESET} $1" } msgerr() { echo -e "${RED}==> ERROR:${CRESET} $1" } msginst() { echo -e "[${GREEN}i${CRESET}] $1" } msgmiss() { echo -e "[${YELLOW}m${CRESET}] $1" } msgnoinst() { echo -e "[ ] $1" } msgwarn() { echo -e "${YELLOW}==> WARNING:${CRESET} $1" } needroot() { if [ $UID != 0 ]; then if [ "$#" -eq 0 ]; then needroot "This operation" else msgerr "$@ need root access!" fi exit 1 fi } getportpath() { for repo in ${PORT_REPO[@]}; do if [[ -f $repo/$1/$BUILD_SCRIPT ]]; then echo "$(dirname $repo/$1/$BUILD_SCRIPT)" return 0 fi done return 1 } pushd() { command pushd $1 &>/dev/null } popd() { command popd &>/dev/null } vercomp() { if [ "$1" = "$2" ]; then return 0 # same version elif [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]; then return 1 # $1 lower than $2 else return 2 # $1 higher than $2 fi } installed_pkg_info() { if isinstalled $2; then echo $(cat $INDEX_DIR/$2/.pkginfo | grep ^$1 | cut -d " " -f3-) fi } allinstalled() { for i in $INDEX_DIR/*; do if isinstalled ${i##*/}; then echo ${i##*/} fi done } confirm() { read -r -p "$1 (Y/n) " response case "$response" in [Nn][Oo]|[Nn]) echo "$2"; exit 2 ;; *) : ;; esac } checktool() { if ! type -p $1 &>/dev/null; then msgerr "'$1' not exist in your system!" exit 1 fi } needarg() { if [[ -z "$@" ]]; then msgerr "This operation required an arguments!" exit 1 fi } catport() { if PPATH=$(getportpath "$1"); then cat "$PPATH/$BUILD_SCRIPT" else msgerr "Port '$1' not exist." exit 1 fi } isinstalled() { if [ -s $INDEX_DIR/$1/.pkginfo ] && [[ $(grep $1 $INDEX_DIR/$1/.pkginfo) ]]; then return 0 else return 1 fi } settermtitle() { echo -en "\033]0;$@\a" } missingdep() { local pkg d for pkg in $(allinstalled); do if [ $(getportpath "$pkg") ]; then depends=$(grep "^# depends[[:blank:]]*:" $(getportpath "$pkg")/$BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++') fi if [ "$depends" ]; then for d in ${depends[@]}; do if ! isinstalled $d; then msd+=($d) fi done fi if [ ${#msd[@]} -gt 0 ]; then echo -e "${GREEN}$pkg${CRESET} missing ${RED}${msd[@]}${CRESET}" fi unset depends msd done } checkintegrity() { if [ "$1" ]; then pushd / if [ -f $INDEX_DIR/$1/.files ]; then while IFS=' ' read -r line; do if [ ! -e "$line" ]; then MISSING_FILE=yes if [ -L "$line" ]; then echo -e "${YELLOW}broken symlink${CRESET} $1: /$line" else echo -e "${RED}file missing${CRESET} $1: /$line" fi fi done < <(cat $INDEX_DIR/$1/.files) else echo "Package '$1' not installed." exit 1 fi popd else pushd / for pkg in $(allinstalled); do while IFS=' ' read -r line; do if [ ! -e "$line" ]; then MISSING_FILE=yes if [ -L "$line" ]; then echo -e "${YELLOW}broken symlink${CRESET} $pkg: /$line" else echo -e "${RED}file missing${CRESET} $pkg: /$line" fi fi done < <(cat $INDEX_DIR/$pkg/.files) done popd fi [ "$UID" != "0" ] && msg "${YELLOW}(check integrity is recommended run as root or using sudo)${CRESET}" if [ "$1" ]; then p="Package '$1'" else p="Your system" fi [ ! "$MISSING_FILE" ] && msg "$p files is consistent with package tree." } listinstalled() { local pkg for pkg in $(allinstalled); do iname=$(installed_pkg_info name $pkg) iversion=$(installed_pkg_info version $pkg) irelease=$(installed_pkg_info release $pkg) echo -e "$iname ${GREEN}$iversion${CRESET}-${BLUE}$irelease${CRESET}" unset iname iversion irelease done } listorphan() { local pkg all depends deps for pkg in $(allinstalled); do if [ $(getportpath $pkg) ]; then depends=$(grep "^# depends[[:blank:]]*:" $(getportpath $pkg)/$BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++') fi for deps in ${depends[@]}; do ALL_DEP+=($deps) done done # find orphan package for all in $(allinstalled); do ORPHAN=yes for depended in ${ALL_DEP[@]}; do if [ $depended = $all ]; then ORPHAN=no break fi done if [ "$ORPHAN" = yes ]; then iname=$(installed_pkg_info name $all) iversion=$(installed_pkg_info version $all) irelease=$(installed_pkg_info release $all) echo -e "$iname ${GREEN}$iversion${CRESET}-${BLUE}$irelease${CRESET}" fi done } lockpkg() { local pkg needroot "Locking package" for pkg in "$@"; do if ! isinstalled $pkg; then msgerr "Package '$pkg' is not installed." elif [ -f $INDEX_DIR/$pkg/.lock ]; then msgerr "Package '$pkg' already locked." else touch $INDEX_DIR/$pkg/.lock && msg "Successfully locked package '$pkg'." fi done } unlockpkg() { local pkg needroot "Unlocking package" for pkg in "$@"; do if ! isinstalled $pkg; then msgerr "Package '$pkg' is not installed." elif [ ! -f $INDEX_DIR/$pkg/.lock ]; then msgerr "Package '$pkg' is not locked." else rm -f $INDEX_DIR/$pkg/.lock && msg "Successfully unlocked package '$pkg'." fi done } listlocked() { local pkg for pkg in $(allinstalled); do if [ -f "$INDEX_DIR"/$pkg/.lock ]; then echo -e "$pkg" fi done } showdepends() { local dep if [ $(getportpath "$1") ]; then depends=$(grep "^# depends[[:blank:]]*:" $(getportpath "$1")/$BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++') else msgerr "Port '$1' not exist." exit 1 fi #isinstalled ${depends[0]} for dep in ${depends[@]}; do if isinstalled $dep; then msginst "$dep" elif getportpath $dep >/dev/null; then msgnoinst "$dep" else msgmiss "$dep" fi done } showdependent() { local port all dep pname for port in ${PORT_REPO[@]}; do if [ -d $port ]; then for all in $port/*/$BUILD_SCRIPT; do depend=$(grep "^# depends[[:blank:]]*:" $all | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++') for dep in ${depend[@]}; do if [ $dep = $1 ]; then GDP=yes pname=$(dirname $all) pname=$(echo ${pname##*/}) if isinstalled $pname; then msginst "$pname" else msgnoinst "$pname" fi fi done done fi done [ "$GDP" ] || msg "No package depends on '$1'." } checkowner() { local arg arg=$(echo $1 | sed 's:^/::') for pkg in $(allinstalled); do grep $arg $INDEX_DIR/$pkg/.files | while read line; do echo -e "${CYAN}$pkg${CRESET} => ${PURPLE}$line${CRESET}" done done } showtree() { if ! isinstalled $1; then msg "Package'$1' not installed." else while IFS=' ' read -r line; do echo "$line" done < <(cat $INDEX_DIR/$1/.files) fi } updports() { checktool httpup needroot "Updating ports" if [ ! -e "$REPO_FILE" ]; then msgerr "Repo file not found! ($REPO_FILE)" exit 1 fi while read repodir repourl junk; do case $repodir in ""|"#"*) continue ;; esac if [ -n "$repodir" ] && [ -n "$repourl" ]; then httpup sync $repourl $repodir fi done < "$REPO_FILE" } printreadme() { needarg $@ if PPATH=$(getportpath "$1"); then if [ -f "$PPATH/readme" ]; then cat "$PPATH/readme" else msgerr "Port '$1' does not have readme." fi else msgerr "Port '$1' not exist." exit 1 fi } searchpkg() { needarg $@ case $1 in -*) msgerr "Invalid pattern '$1'" return 1 ;; esac local port found OUTPUT for port in ${PORT_REPO[@]}; do if [ -d $port ]; then pushd $port OUTPUT=$(grep -R description | grep "$BUILD_SCRIPT:# description[[:blank:]]*:" | sed "s/$BUILD_SCRIPT:# description[[:blank:]]*://" | grep -i "$1" | cut -d '/' -f1) popd if [ -n "$OUTPUT" ]; then found=yes for out in ${OUTPUT[@]}; do if [ -f $port/$out/$BUILD_SCRIPT ]; then pushd $port/$out description=$(grep "^# description[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# description[[:blank:]]*:[[:blank:]]*//') . $BUILD_SCRIPT popd if [ ! -z "$name" ] && [ ! -z "$version" ] && [ ! -z "$release" ]; then portname=$(basename $port) search_result="${PURPLE}($portname)${CRESET} $name ${CYAN}$version-$release${CRESET} $description" if isinstalled $name; then echo -e "[${GREEN}*${CRESET}] $search_result" else echo -e "[ ] $search_result" fi unset description name version release fi fi done fi fi done if [ ! "$found" ]; then msg "No matching package found." fi } foreignpkg() { for pkg in $(allinstalled); do if ! getportpath $pkg >/dev/null; then iname=$(installed_pkg_info name $pkg) iversion=$(installed_pkg_info version $pkg) irelease=$(installed_pkg_info release $pkg) echo -e "$iname ${GREEN}$iversion${CRESET}-${CYAN}$irelease${CRESET}" fi unset iname iversion irelease done } srunhooks() { local hook if [ "$mode" = "upgrade" ] || [ "$mode" = "sysup" ]; then opr=upgrade else opr=install fi for hook in $HOOK_DIR/*.hook; do [ -f $hook ] || continue description=$(grep "^# description[[:blank:]]*:" $hook | sed 's/^# description[[:blank:]]*:[[:blank:]]*//') operation=$(grep "^# operation[[:blank:]]*:" $hook | sed 's/^# operation[[:blank:]]*:[[:blank:]]*//') target=$(grep "^# target[[:blank:]]*:" $hook | sed 's/^# target[[:blank:]]*:[[:blank:]]*//') if [ -n "$description" ] && [ -n "$operation" ] && [ -n "$target" ]; then if [ "$(echo $operation | grep -w "$opr" )" ]; then for pn in $@; do if [ -s $INDEX_DIR/$pn/.files ] && [ "$(grep $target $INDEX_DIR/$pn/.files)" ]; then [ "$ee" != 1 ] && { echo; ee=1; } echo "$description" . $hook if [ "`type -t exechook`" = "function" ]; then exechook fi break fi done fi fi unset description operation target unset -f exechook done } srunremovehooks() { local hook if [ "${#runthishook[@]}" -gt 0 ]; then echo for hook in ${runthishook[@]}; do description=$(cat "$hook" | grep ^"# description" | sed 's/\://' | cut -d ' ' -f 3-) operation=$(cat "$hook" | grep ^"# operation" | sed 's/\://' | cut -d ' ' -f 3-) target=$(cat "$hook" | grep ^"# target" | sed 's/\://' | cut -d ' ' -f 3-) if [ -n "$description" ] && [ -n "$operation" ] && [ -n "$target" ]; then echo "$description" . $hook if [ "`type -t exechook`" = "function" ]; then exechook fi fi unset description operation target unset -f exechook done fi } srunpreremovehooks() { local hook for hook in $HOOK_DIR/*.hook; do [ -f $hook ] || continue operation=$(cat "$hook" | grep ^"# operation" | sed 's/\://' | cut -d ' ' -f 3-) target=$(cat "$hook" | grep ^"# target" | sed 's/\://' | cut -d ' ' -f 3-) if [ "$(echo $operation | grep -w "remove" )" ]; then if [ "$(grep $target $INDEX_DIR/$1/.files)" ]; then if [ ! $(echo ${runthishook[@]} | tr ' ' '\n' | grep -x $hook) ]; then runthishook+=($hook) fi fi fi unset operation target done } buildpkg() { local OPTS if [ -z "$1" ]; then echo "Please specify package to build." return 1 fi while [ "$1" ]; do if [[ "$1" =~ ^-(f|v|w|m|o|x)$ ]]; then OPTS+=($1) elif [[ "$1" =~ ^--(no-prebuild|force-rebuild|skip-mdsum|download|extract|keep-work)$ ]]; then OPTS+=($1) elif [[ "$1" =~ ^--(pkgdir|srcdir)= ]]; then OPTS+=($1) elif [[ "$1" =~ ^-. ]]; then echo "Unrecognize option. ($1)" return 1 else PKGNAME=$1 fi shift done pushd $(getportpath $PKGNAME) if [ $? = 0 ]; then pkgbuild ${OPTS[@]} || return 1 else echo "Package '$PKGNAME' not found." return 1 fi } installpkg() { local pkg i int pkgcount count IPKG OPTS REINSTALL needroot "Installing package" if [ -z "$1" ]; then echo "Please specify package(s) to install." return 1 fi while [ "$1" ]; do if [[ "$1" =~ ^-(c|v|-verbose|-ignore-conflict)$ ]]; then OPTS+=($1) elif [[ "$1" =~ ^-(r|-reinstall)$ ]]; then OPTS+=($1) REINSTALL=1 elif [[ "$1" =~ ^-(d|-no-dep)$ ]]; then NO_DEP=1 elif [[ "$1" = "--no-confirm" ]]; then NOCONFIRM=1 elif [[ "$1" =~ ^-. ]]; then echo "Unrecognize option. ($1)" return 1 else PKGNAME+=($1) fi shift done # if reinstall, dont calculate dep, just reinstall it then exit if [ "$REINSTALL" = 1 ]; then for ii in ${PKGNAME[@]}; do if [ ! $(getportpath $ii) ]; then echo "Package '$ii' not found." elif ! isinstalled $ii; then echo "Package '$ii' not installed." else pushd $(getportpath $ii) pkgbuild -s --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi popd fi done srunhooks ${PKGNAME[@]} [ "$error" = 1 ] && return 1 || return 0 fi if [ "$NO_DEP" = 1 ]; then for ii in ${PKGNAME[@]}; do if [ ! $(getportpath $ii) ]; then echo "Package '$ii' not found." elif ! isinstalled $ii; then echo "Package '$ii' not installed." return 1 else pushd $(getportpath $ii) pkgbuild -i --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi popd fi done srunhooks ${PKGNAME[@]} [ "$error" = 1 ] && return 1 || return 0 fi for i in ${PKGNAME[@]}; do if [ ! $(getportpath $i) ]; then echo "Package '$i' not found." elif isinstalled $ii; then echo "Package '$i' already installed." return 1 else IPKG+=($i) fi done if [ "${#IPKG[@]}" = 0 ]; then echo "Nothing to do. Exiting..." return 0 fi echo "Resolving dependencies..." INST="$(pkgdeplist -l -q ${IPKG[@]} | awk '{print $2}')" if [ "$INST" ]; then echo pkgcount=0 for pkg in $INST; do pkgcount=$(( $pkgcount + 1 )) echo -n "$pkgcount) $pkg " done echo; echo if [ ! "$NOCONFIRM" ]; then confirm "Continue install package(s)?" "Package installation cancelled." echo fi count=0 total=$(echo $INST | wc -w) for int in ${INST[@]}; do count=$(( $count + 1 )) portpathh=$(getportpath $int) if [ "$portpathh" ]; then pushd $portpathh . $BUILD_SCRIPT settermtitle "[ $count/$total ] installing $name-$version-$release" pkgbuild -is --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi popd else msgwarn "Skipping missing package: $int" fi unset portpathh done settermtitle "Triggering install hook" srunhooks ${INST[@]} if [ "$error" = 1 ]; then settermtitle "Install process done with failure" return 1 else settermtitle "Install process done" return 0 fi fi } outdatepkg() { local pkg for pkg in $(allinstalled); do if [ ! -e "$INDEX_DIR/$pkg/.lock" ] && getportpath $pkg >/dev/null; then . $(getportpath $pkg)/$BUILD_SCRIPT iversion=$(installed_pkg_info version $pkg) irelease=$(installed_pkg_info release $pkg) if [ "$release" != "$irelease" ] || [ "$version" != "$iversion" ]; then echo $name fi unset iversion irelease fi done } removepkg() { local pkg i IPKG OPTS needroot "Removing package" if [ -z "$1" ]; then echo "Please specify package(s) to remove." return 1 fi while [ "$1" ]; do if [[ "$1" =~ ^--(no-postremove|no-preremove)$ ]]; then OPTS+=($1) elif [[ "$1" = "--no-confirm" ]]; then NOCONFIRM=1 elif [[ "$1" =~ ^-. ]]; then echo "Unrecognize option. ($1)" return 1 else PKGNAME+=($1) fi shift done for i in ${PKGNAME[@]}; do if ! isinstalled $i; then echo "Package '$i' not installed." else IPKG+=($i) fi done if [ "${#IPKG[@]}" = 0 ]; then echo "Nothing to do. Exiting..." return 0 fi if [ "$IPKG" ]; then pkgcount=0 count=0 for pkg in ${IPKG[@]}; do pkgcount=$(( $pkgcount + 1 )) echo -n "$pkgcount) $pkg " done echo; echo if [ ! "$NOCONFIRM" ]; then confirm "Continue remove package(s)?" "Package removing cancelled." echo fi for pkg in ${IPKG[@]}; do count=$(( $count + 1 )) srunpreremovehooks $pkg settermtitle "[ $count/$pkgcount ] Removing $pkg" pkgdel -s --no-hook $pkg ${OPTS[@]} || return 1 done settermtitle "Triggering remove hook" srunremovehooks settermtitle "Remove process done" fi } sysup() { local d UPGPKG NEWPKG PKGOUTDATE OPTS needroot "Upgrading package" while [ "$1" ]; do if [ "$1" = "--no-confirm" ]; then NOCONFIRM=1 elif [ "$1" = "--no-backup" ]; then OPTS+=($1) fi shift done PKGOUTDATE=$(outdatepkg) if [ ! "$PKGOUTDATE" ]; then msg "All package is up to date." return 0 fi echo "Resolving dependencies..." DEP=$(pkgdeplist ${PKGOUTDATE[@]} -l | awk '{print $2}') echo UPGPKG=0 NEWPKG=0 for d in $DEP; do if [ "$(echo $PKGOUTDATE | tr ' ' '\n' | grep -x $d)" = "$d" ]; then echo -ne "[${GREEN}u${CRESET}] $d " WILLINSTALL+=($d) UPGPKG=$(( $UPGPKG + 1 )) elif ! isinstalled $d && [ $(getportpath "$d") ]; then echo -ne "[${CYAN}n${CRESET}] $d " WILLINSTALL+=($d) NEWPKG=$(( $NEWPKG + 1 )) fi done echo echo echo "( $UPGPKG upgrade, $NEWPKG new install )" echo if [ ! "$NOCONFIRM" ]; then confirm "Continue upgrade/install these package(s)?" "Package upgrade cancelled." echo fi count=0 total=$(echo ${WILLINSTALL[@]} | wc -w) for inst in ${WILLINSTALL[@]}; do # install all required dependencies and target packages itself count=$(( $count + 1 )) pushd $(getportpath $inst) . $BUILD_SCRIPT if ! isinstalled $inst; then settermtitle "[ $count/$total ] Installing $name-$version-$release" pkgbuild -is --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi else settermtitle "[ $count/$total ] Upgrading $name-$version-$release" pkgbuild -us --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi fi done settermtitle "Triggering install hook" srunhooks ${WILLINSTALL[@]} if [ "$error" = 1 ]; then settermtitle "Install process done with failure" return 1 else settermtitle "Install process done" return 0 fi } upgradepkg() { local pkg needroot "Upgrading package" needarg "$@" while [ "$1" ]; do if [[ "$1" =~ ^--(no-postupgrade|no-preupgrade)$ ]]; then OPTS+=($1) elif [[ "$1" =~ ^-(-no-dep|d)$ ]]; then NO_DEP=1 elif [[ "$1" = "--no-confirm" ]]; then NOCONFIRM=1 elif [[ "$1" =~ ^-. ]]; then echo "Unrecognize option. ($1)" exit 1 else PKGNAME+=($1) fi shift done for pkg in ${PKGNAME[@]}; do if ! isinstalled $pkg; then msgerr "Package '$pkg' not installed." return 1 fi if [ ! $(getportpath $pkg) ]; then msgerr "Package '$pkg' not exist." return 1 fi done if [ -z "$NO_DEP" ]; then echo "Resolving dependencies..." DEP=$(pkgdeplist -l -n ${PKGNAME[@]} | awk '{print $2}') for dep in $DEP; do if ! isinstalled $dep; then if [ $(getportpath $dep) ]; then NEWPKG+=($dep) fi fi done fi echo count=0 for i in ${NEWPKG[@]} ${PKGNAME[@]}; do count=$(( $count + 1 )) echo -n "$count) $i " done echo echo echo "( ${#PKGNAME[@]} upgrade, ${#NEWPKG[@]} new install )" echo if [ ! "$NOCONFIRM" ]; then confirm "Continue upgrade/install these package(s)?" "Package upgrade cancelled." echo fi if [ ${#NEWPKG[@]} -gt 0 ]; then for newpkg in ${NEWPKG[@]}; do pushd $(getportpath $newpkg) . $BUILD_SCRIPT settermtitle "Installing $name-$version-$release" pkgbuild -is --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi popd done fi for pkg in ${PKGNAME[@]}; do # upgrade all target packages pushd $(getportpath $pkg) . $BUILD_SCRIPT settermtitle "Upgrading $name-$version-$release" pkgbuild -us --no-hook ${OPTS[@]} if [ $? != 0 ]; then error=1 break fi popd done settermtitle "triggering upgrade hook" srunhooks ${NEWPKG[@]} ${PKGNAME[@]} if [ "$error" = 1 ]; then settermtitle "Upgrade process done with failure" return 1 else settermtitle "Upgrade process done" return 0 fi } outdate() { local pkg for pkg in $(allinstalled); do if [ $(getportpath $pkg) ]; then . $(getportpath $pkg)/$BUILD_SCRIPT iversion=$(installed_pkg_info version $pkg) irelease=$(installed_pkg_info release $pkg) [ -f "$INDEX_DIR/$pkg/.lock" ] && ITSLOCK="[locked]" outdatemsg="$name ${RED}$iversion-$irelease${CRESET} => ${GREEN}$version-$release${CRESET} ${CYAN}$ITSLOCK${CRESET}" newerinstmsg="$name ${RED}$iversion-$irelease${CRESET} => ${GREEN}$version-$release${CRESET} ${YELLOW}[newer installed]${CRESET} ${CYAN}$ITSLOCK${CRESET}" if [ "$version" != "$iversion" ]; then vercomp $version $iversion if [ $? = 2 ]; then echo -e "$outdatemsg" OUTDATE=yes elif [ $? = 1 ]; then echo -e "$newerinstmsg" OUTDATE=yes fi elif [ "$release" != "$irelease" ]; then vercomp $release $irelease if [ $? = 2 ]; then echo -e "$outdatemsg" OUTDATE=yes elif [ $? = 1 ]; then echo -e "$newerinstmsg" OUTDATE=yes fi fi unset ITSLOCK fi done [ ! "$OUTDATE" ] && msg "All package is up to date." } clearpkgcache() { needroot "Removing package & source cache" getpkgcache if [ ${#ALL_PACKAGES[@]} -gt 0 ]; then for pkg in ${ALL_PACKAGES[@]}; do rm -v $PACKAGE_DIR/$pkg done fi if [ ${#ALL_SOURCES[@]} -gt 0 ]; then for src in ${ALL_SOURCES[@]}; do rm -v $SOURCE_DIR/$src done fi } getpkgcache() { [ -f /etc/scratchpkg.conf ] && . /etc/scratchpkg.conf for list in $(ls "$PACKAGE_DIR"); do [ -f "$PACKAGE_DIR"/$list ] && ALL_PACKAGES+=($list) done for list in $(ls "$SOURCE_DIR"); do [ -f "$SOURCE_DIR"/$list ] && ALL_SOURCES+=($list) done for repo in ${PORT_REPO[@]}; do for port in $(ls $repo); do if [ -f $repo/$port/$BUILD_SCRIPT ]; then . $repo/$port/$BUILD_SCRIPT PORT_PACKAGES+=($name-$version-$release.spkg.txz) if [ ! -z $source ]; then for src in ${source[@]}; do if [ $(echo $src | grep -E "(ftp|http|https)://") ]; then if [ $(echo $src | grep -E "::(ftp|http|https)://") ]; then sourcename="$(echo $src | awk -F '::' '{print $1}')" else sourcename="$(echo $src | rev | cut -d / -f 1 | rev)" fi SOURCE_NAMES+=($sourcename) fi done fi fi done done for i in ${PORT_PACKAGES[@]}; do for pkg in ${!ALL_PACKAGES[@]}; do if [ "${ALL_PACKAGES[pkg]}" = "$i" ]; then unset 'ALL_PACKAGES[pkg]' break fi done done for a in ${SOURCE_NAMES[@]}; do for src in ${!ALL_SOURCES[@]}; do if [ "${ALL_SOURCES[src]}" = "$a" ]; then unset 'ALL_SOURCES[src]' break fi done done } pkgcache() { getpkgcache if [ ${#ALL_PACKAGES[@]} -gt 0 ]; then ALL_PACKAGES_SIZE=$(pushd "$PACKAGE_DIR" && du -ch ${ALL_PACKAGES[@]} | grep total | awk '{print $1}' && popd) else ALL_PACKAGES_SIZE=0M fi if [ ${#ALL_SOURCES[@]} -gt 0 ]; then ALL_SOURCES_SIZE=$(pushd "$SOURCE_DIR" && du -ch ${ALL_SOURCES[@]} | grep total | awk '{print $1}' && popd) else ALL_SOURCES_SIZE=0M fi msg "${CYAN}Package cache ($ALL_PACKAGES_SIZE):${CRESET}" [ ${#ALL_PACKAGES[@]} -gt 0 ] && (echo ${ALL_PACKAGES[@]} | tr ' ' '\n') || echo "(none)" echo "" msg "${CYAN}Source cache ($ALL_SOURCES_SIZE):${CRESET}" [ ${#ALL_SOURCES[@]} -gt 0 ] && (echo ${ALL_SOURCES[@]} | tr ' ' '\n') || echo "(none)" } showportpath() { needarg $@ if PPATH=$(getportpath "$1"); then echo "$PPATH" else msgerr "Port '$1' not exist." exit 1 fi } duplicateports() { dup=$(find ${PORT_REPO[@]} -type d -print | egrep -xv "($(echo ${PORT_REPO[@]} | tr ' ' '|'))" | \ rev | cut -d '/' -f1 | rev | sort | uniq -d) if [ "$dup" ]; then for dp in $dup; do for repo in ${PORT_REPO[@]}; do reponame=$(basename $repo) [ -d $repo/$dp ] && echo "$repo/$dp" done done else msg "No duplicate ports found." fi } usage_extra() { cat << EOF Usage: $(basename $0) [ ] Operation: depends show depends of a package search search packages in port's repos lock lock packages from upgrade unlock unlock packages from upgrade cat view a package build scripts dependent show package's dependent own show package's owner of file pkgtree show list files of installed package path show package's buildscripts path sync update port's repo sysup full system update dup print duplicate ports in repo readme print readme file if exist listinst list installed package in system listorphan list orphan package integrity check integrity of package's files outdate check for outdate packages cache print leftover cache rmcache remove leftover cache missingdep check for mising dependency of installed package foreignpkg print package installed without port in repo listlocked print locked packages EOF } usage_build() { cat << EOF Usage: $(basename $0) build [ ] Options: -f, --force-rebuild force rebuild -m, --skip-mdsum skip md5sum check for sources -d, --no-dep skip dependency check -e, --extract extract only -w, --keep-work keep woring directory -o, --download download source files only -v, --verbose be verbose --redownload re-download source files --srcdir= override default SOURCE_DIR --pkgdir= override default PACKAGE_DIR --no-preinstall skip pre-install script EOF } usage_upgrade() { cat << EOF Usage: $(basename $0) upgrade [ ] Options: -d, --no-dep skip installing dependencies (new dependencies) -c, --ignore-conflict skip file conflict check -v, --verbose print install process --no-backup skip backup configuration file --no-preupgrade skip pre-upgrade script --no-postupgrade skip post-upgrade script --no-confirm dont ask confirmation EOF } usage_remove() { cat << EOF Usage: $(basename $0) remove [ ] Options: -d, --no-dep skip dependency check -v, --verbose print removed files --no-preremove skip pre-remove script --no-postremove skip post-remove script --no-confirm dont ask confirmation EOF } usage_install() { cat << EOF Usage: $(basename $0) install [ ] Options: -d, --no-dep skip installing dependencies -c, --ignore-conflict skip file conflict check -r, --reinstall reinstall installed package --no-preinstall skip pre-install script --no-postinstall skip post-install script --no-confirm dont ask confirmation EOF } usage_help() { cat << EOF Usage: $(basename $0) help Operation: build build only packages install install packages remove remove packages in system upgrade upgrade packages and install new dependencies (if any) extra various extra options EOF } usage() { if [ -z "$1" ];then usage_help else case "$1" in install) usage_install ;; remove) usage_remove ;; upgrade) usage_upgrade ;; build) usage_build ;; extra) usage_extra ;; esac fi } extract_opt() { local opt OPTS for opt in $@; do case $opt in --*) OPTS+=($opt) ;; -*) for (( i=1; i<${#opt}; i++ )); do OPTS+=(-${opt:$i:1}); done ;; *) OPTS+=($opt) ;; esac done echo ${OPTS[@]} } main() { if [ "$mode" = "build" ]; then buildpkg $@ exit $? fi if [ "$mode" = "install" ]; then installpkg $@ exit $? fi if [ "$mode" = "upgrade" ]; then upgradepkg $@ exit $? fi if [ "$mode" = "remove" ]; then removepkg $@ exit $? fi if [ "$mode" = "sysup" ]; then sysup $@ exit $? fi if [ "$mode" = "outdate" ]; then outdate exit $? fi if [ "$mode" = "listorphan" ]; then listorphan exit $? fi if [ "$mode" = "search" ]; then searchpkg $1 exit $? fi # search for foreign port (installed package with no port in repos) if [ "$mode" = "foreignpkg" ]; then foreignpkg exit $? fi if [ "$mode" = "sync" ]; then updports exit $? fi if [ "$mode" = "listinstalled" ]; then listinstalled exit $? fi if [ "$mode" = "readme" ]; then printreadme $1 exit $? fi if [ "$mode" = "pkgtree" ]; then showtree $1 exit $? fi if [ "$mode" = "own" ]; then checkowner $1 exit $? fi if [ "$mode" = "dependent" ]; then showdependent $1 exit $? fi if [ "$mode" = "cat" ]; then catport $1 exit $? fi if [ "$mode" = "depends" ]; then showdepends $1 exit $? fi if [ "$mode" = "lock" ]; then lockpkg $@ exit $? fi if [ "$mode" = "unlock" ]; then unlockpkg $@ exit $? fi if [ "$mode" = "listlocked" ]; then listlocked exit $? fi if [ "$mode" = "listinst" ]; then listinstalled exit $? fi if [ "$mode" = "rmcache" ]; then clearpkgcache exit $? fi if [ "$mode" = "cache" ]; then pkgcache exit $? fi if [ "$mode" = "help" ]; then usage $1 exit $? fi if [ "$mode" = "integrity" ]; then checkintegrity $1 exit $? fi if [ "$mode" = "path" ]; then showportpath $1 exit $? fi if [ "$mode" = "dup" ]; then duplicateports exit $? fi if [ "$mode" = "missingdep" ]; then missingdep exit $? fi echo "Run 'scratch help' to see available mode and options" exit 5 } BUILD_SCRIPT="spkgbuild" INDEX_DIR="/var/lib/scratchpkg/index" REPO_FILE="/etc/scratchpkg.repo" HOOK_DIR="/etc/hooks" SOURCE_DIR="/var/cache/scratchpkg/sources" PACKAGE_DIR="/var/cache/scratchpkg/packages" if [ ! -f "$REPO_FILE" ]; then msgerr "repo file not exist. ($REPO_FILE)" exit 1 else while read repodir repourl junk; do case $repodir in ""|"#"*) continue ;; esac PORT_REPO+=($repodir) done < "$REPO_FILE" fi mode=$1 shift main $(extract_opt $@)