#!/bin/sh # Become self aware. SELF_NAME="${0##*/}" SELF_VERSION="0.1.4" # Check if being run by the root user. if [[ "${EUID}" -ne 0 ]] ; then echo "This program must be run as the 'root' user!" exit 1 fi # Get program options. FLAG_HELP="no" FLAG_QUIET="no" FLAG_PRETEND="no" FLAG_VERSION="no" if ! OPTIONS=$(getopt -q -o h,q,p,v -l help,quiet,local,pretend,version -- "$@") ; then echo "Unrecognized option:" echo echo "Type '${SELF_NAME} --help' for help" exit 1 fi set -- ${OPTIONS} while [[ $# -gt 0 ]] ; do case $1 in (-h|--help) FLAG_HELP="yes" ;; (-q|--quiet) FLAG_QUIET="yes" ;; (-p|--pretend) FLAG_PRETEND="yes" ;; (-v|--version) FLAG_VERSION="yes" ;; (--) shift break ;; (-*) echo "You used an accepted but not yet implemented option." echo "If you are not the developer please report this issue upstream." exit 1 ;; (*) break ;; esac shift done # Parse program options. if [[ "${FLAG_HELP}" = "yes" ]] ; then echo "Usage:" echo echo " ${SELF_NAME} " echo echo "Options:" echo echo " -h, --help Display this guide then exit." echo echo " -q, --quiet Suppress normal output." echo echo " -p, --pretend Only explain what would happen if the process" echo " was actually performed." echo echo " -v, --version Display version information then exit." exit 1 elif [[ "${FLAG_VERSION}" = "yes" ]] ; then echo "${SELF_NAME} ${SELF_VERSION}" exit 1 fi # Define locations relevant to this script. PIDFILE="/var/run/${SELF_NAME}.pid" TMPFILE="/var/tmp/${SELF_NAME}.dat" # Define variables relevant to this script. if grep --quiet "^PORTDIR=" /etc/make.conf ; then PORTDIR=$(grep "^PORTDIR=" /etc/make.conf | cut -d \" -f 2) else PORTDIR="/usr/portage" fi # Setup helper functions. function validate_emerge_sync() { emerge --sync > "${TMPFILE}" 2>&1 if grep --quiet "successful" "${TMPFILE}" ; then return 0 fi return 1 } # Check if already running. if [[ ! -f "${PIDFILE}" ]] ; then # Indicate the start of the process. if [[ "${FLAG_PRETEND}" = "no" ]] ; then touch "${PIDFILE}" fi # Restore missing blob object(s) if [ -d "${PORTDIR}" ] ; then cd "${PORTDIR}" until validate_emerge_sync ; do MISSING_BLOB_OBJECT=$(grep "missing blob object" ${TMPFILE} | cut -d \' -f 2) GITHUB_MESSAGE=$(python2.7 -c "import json,urllib; print json.load(urllib.urlopen('https://api.github.com/repos/funtoo/ports-2012/git/blobs/${MISSING_BLOB_OBJECT}'))['message']" 2> /dev/null) if [[ "${GITHUB_MESSAGE}" = "Not Found" ]] ; then echo "Not Found!" echo " (wait a hour then try again)" break else if [[ ${FLAG_PRETEND} = "no" ]] ; then if [[ ${FLAG_QUIET} = "no" ]] ; then echo "Restoring ${MISSING_BLOB_OBJECT}..." python2.7 -c "import json,urllib; print json.load(urllib.urlopen('https://api.github.com/repos/funtoo/ports-2012/git/blobs/${MISSING_BLOB_OBJECT}'))['content']" | base64 --decode | git hash-object -w --stdin > /dev/null echo " ...done!" else python2.7 -c "import json,urllib; print json.load(urllib.urlopen('https://api.github.com/repos/funtoo/ports-2012/git/blobs/${MISSING_BLOB_OBJECT}'))['content']" | base64 --decode | git hash-object -w --stdin > /dev/null fi rm ${TMPFILE} else echo "Restoring ${MISSING_BLOB_OBJECT}" break fi fi sleep 2 done else echo "${PORTDIR} does not exist!" echo " (this shouldn't happen)" fi # Indicate the end of the process. if [[ ${FLAG_PRETEND} = "no" ]] ; then rm ${PIDFILE} fi else if [[ ${FLAG_QUIET} = "no" ]] ; then echo "Already running!" echo " (remove ${PIDFILE} if this is not true)" fi exit 1 fi exit 0