#!/bin/bash ### # Gentoox Magic # Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.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 2 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ### ### # NewVersion: 20090123003 # Format: yyyymmddXXX, this is the only sane format as any other variation # would result in the number getting smaller at some stage during # the year. XXX = Sub updates on the same day. # Note: This is now taken care of by editmagic. ### ### # When a forced update to the magic script is needed, set the "NewVersion" line # to the current date. It would be useful to update this line when a new # release is made so all people using it must update on the first run. ### ### # This cleans up all temporary files ### cleanup() { rm -rf ~/patches* rm -rf ~/magic.md5* rm -rf ~/*.b0x rm -rf /tmp/.shutupled* rm -rf ~/motd.gentoox* rm -rf /tmp/serverdown rm -rf /tmp/patches.md5* } ### # The following code traps when a user hits Ctrl-C and cleans everything up before # exiting ### trap escape 20 2 trap cleanup 0 escape() { clear cleanup echo -e "${redtxt}Magic caught 'Ctrl-C'" echo -e "" echo -e "${bluetxt}Magic will now clean up then exit.\nNote: this is counted as an error.${normaltxt}\n" $error ### # Backup the magic-database file after magic - abort ### cp -a /etc/conf.d/magic-database /etc/conf.d/magic.backup/magic-database-postmagic-abort_`date +%Y-%m-%d_%H%M.%S` 1> /dev/null 2> /dev/null exit 1 } export currentversion=`grep -m1 "NewVersion" /bin/magic | awk '{ print $3 }'` if [ `whoami` != "root" ]; then echo "You must be logged in as root to use this utility" exit 1 fi ### # For phpSysInfo ### date > /etc/conf.d/lastmagic ### # For idiots who cant configure their resolv.conf ### if [ ! -d /magic/b0x ]; then cat /etc/resolv.conf | grep -vi shallax > /etc/resolv.conf.tmp mv /etc/resolv.conf.tmp /etc/resolv.conf chmod 644 /etc/resolv.conf fi ### # Wget command ### if [ ! -z `echo $WGET | grep limit-rate` ]; then echo "Sorry, you cannot specify the --limit-rate option" exit 1 fi if [ -z $WGET ]; then export wget="wget -T 30 -t 5 " else export wget="wget $WGET " fi ### # Set the colours for text here ### export bluetxt="\033[01;34m" export redtxt="\033[01;31m" export normaltxt="\033[00m" export greentxt="\033[01;32m" export yellowtxt="\033[01;33m" ### # LED Definitions ### if [ -f /bin/led ]; then export downloading="/bin/led ogrr" export good="/bin/led gxgx" export important="/bin/led roro" export error="/bin/led rxxx" export busy="/bin/led rgog" export input="/bin/led oxox" export gggg="/bin/led gggg" export oooo="/bin/led oooo" export rrrr="/bin/led rrrr" export rxgx="/bin/led rxgx" else ### # If the binary doesnt exist, set these to "nothing" ### export downloading="" export good="" export important="" export error="" export busy="" export input="" export gggg="" export oooo="" export rrrr="" export rxgx="" fi clear if [ -f /tmp/.shutupled ]; then clear echo -e "\n\n\n${redtxt} Gentoox Magic (c) ShALLaX ${normaltxt}(${bluetxt}gentoox@shallax.com${normaltxt})" echo -e "${normaltxt} Be sure to check ${bluetxt}http://gentoox.shallax.com${normaltxt} regularly for updates!" echo -e " --------------------------------------------------------------------" echo -en "${redtxt}" echo -e " MAGIC IS ALREADY RUNNING " echo -e " Please wait for it to finish before relaunching. " echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -e echo -e " --------------------------------------------------------------------" echo -e "\n Gentoox is distributed under the GNU General Public License. \n\n\n" echo -e " If this is an error, hit Ctrl-C whilst this screen is being shown to" echo -e " clear the safety lock.\n\n" if [ -f /etc/functions.magic ]; then source /etc/functions.magic ding else sleep 5 fi clear exit 1 fi echo "running" > /tmp/.shutupled cd ~/ #$input # #echo "Please select a mirror by typing its number (eg: 1): " #echo -e "" #echo -e "1) http://gentoox.shallax.com - ${bluetxt}Master server${normaltxt} (${redtxt}160kbit${normaltxt})" #echo -e "" #echo -en "Mirror: " #read gentooxurlnumber ### # For now, no mirrors are being used, so default to 1 ### export gentooxurlnumber="1" ### # Fallback server ### export gentooxurl="http://shallax.com:2000" ### # Interpret number choices ### if [ $gentooxurlnumber == "1" ]; then export gentooxurl="http://shallax.com:2000" else $important echo "Sorry, this is an invalid choice, falling back to master server" sleep 5 fi if [ ! -z `echo "${overrideURL}"` ]; then export gentooxurl=${overrideURL} fi ### # Make sure the user always has the latest magic script, ### clear echo -e "\n\n\n${redtxt} Gentoox Magic (c) ShALLaX ${normaltxt}(${bluetxt}gentoox@shallax.com${normaltxt})" echo -e "${normaltxt} Be sure to check ${bluetxt}http://gentoox.shallax.com${normaltxt} regularly for updates!" echo -e " --------------------------------------------------------------------" echo -en "${redtxt}" echo -e " UPDATING MAGIC SCRIPT " echo -e echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -e echo -e " --------------------------------------------------------------------" echo -e "\n Gentoox is distributed under the GNU General Public License. " sleep 2 rm -rf /tmp/magic cd /tmp $downloading $wget --limit-rate=5120 $gentooxurl/magicscript/serverdown 1> /dev/null 2> /dev/null if [ $? != "0" ]; then $error clear echo -e "Downloading magic [${bluetxt}server test${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi $busy if [ `cat ./serverdown | grep AMIDOWN | awk '{ print $3 }'` == "yes" ]; then ### # Backdoor for development ### if [ -z $SUREASSURE ]; then sh ./serverdown exit 1 fi fi $downloading $wget --limit-rate=5120 $gentooxurl/magicscript/limitrate 1> /dev/null 2> /dev/null if [ $? != "0" ]; then clear $error echo -e "Downloading magic [${bluetxt}limitrate${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi $busy export limithuman=`grep kbps /tmp/limitrate | awk '{ print $2 }'` export sum="`grep kbps /tmp/limitrate | awk '{ print $2 }'`*1024" export limitrate=`echo $sum | bc` rm -rf /tmp/limitrate* $good rm -rf /tmp/magic $wget --limit-rate=${limitrate} $gentooxurl/magicscript/magic 1> /dev/null 2> /dev/null if [ $? != "0" ]; then clear $error echo -e "Downloading magic [${bluetxt}magic${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi rm -rf /bin/magic mv /tmp/magic /bin/magic chmod 755 /bin/magic ### # If there's a new version number, then let the user know! This also # ensures that functions.magic is only downloaded where necessary! ### if [ `grep -m1 "NewVersion" /bin/magic | awk '{ print $3 }'` -gt $currentversion ]; then ### # Delete these files so they are updated on the next run ### rm -rf /etc/functions.magic rm -rf /etc/patches.magic $important clear echo -e "\n\n\n${redtxt} Gentoox Magic (c) ShALLaX ${normaltxt}(${bluetxt}gentoox@shallax.com${normaltxt})" echo -e "${normaltxt} Be sure to check ${bluetxt}http://gentoox.shallax.com${normaltxt} regularly for updates!" echo -e " --------------------------------------------------------------------" echo -en "${redtxt}" echo -e " MAGIC HAS BEEN UPDATED " echo -e " MAGIC WILL NOW RESTART TO APPLY CHANGES. " echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -e "" echo -e " --------------------------------------------------------------------" echo -e "\n Gentoox is distributed under the GNU General Public License. \n\n\n" if [ -f /etc/functions.magic ]; then source /etc/functions.magic ding else sleep 5 fi $good cleanup /bin/magic $1 $2 exit fi ### # The next time magic runs after updating itself, it must download the new # versions of the *.magic files. ### if [ ! -f /etc/functions.magic ]; then rm -rf /tmp/functions.magic $wget --limit-rate=${limitrate} $gentooxurl/magicscript/functions.magic 1> /dev/null 2> /dev/null if [ $? != 0 ]; then clear echo -e "Downloading magic [${bluetxt}functions.magic${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi rm -rf /etc/functions.magic mv /tmp/functions.magic /etc/functions.magic chmod 644 /etc/functions.magic fi if [ ! -f /etc/patches.magic ]; then rm -rf /tmp/patches.magic $wget --limit-rate=${limitrate} $gentooxurl/magicscript/patches.magic 1> /dev/null 2> /dev/null if [ $? != 0 ]; then clear echo -e "Downloading magic [${bluetxt}patches.magic${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi rm -rf /etc/patches.magic mv /tmp/patches.magic /etc/patches.magic chmod 644 /etc/patches.magic fi clear ### # By now, these files MUST exist ### source /etc/functions.magic source /etc/patches.magic ### # We can now start splitting down the script into functions contained within # magic.functions. ### updatingMagicDone cd ~/ clear downloadingPatches $downloading $wget --limit-rate=${limitrate} $gentooxurl/magicscript/magic.md5 1> /dev/null 2> /dev/null if [ $? != "0" ]; then clear $error echo -e "Downloading magic [${bluetxt}magic.md5${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi $busy if [ `cat ~/magic.md5 | awk '{ print $1 }'` != `md5sum /bin/magic | awk '{ print $1 }'` ]; then sed "s/NewVersion\: [0-9]*/NewVersion\: 0/" /bin/magic > /bin/magic.tmp mv /bin/magic.tmp /bin/magic chown root:root /bin/magic chmod 755 /bin/magic clear echo "Magic is corrupt, please rerun it to correct this error." exit 1 fi $downloading $wget --limit-rate=${limitrate} $gentooxurl/magicscript/patches.md5 1> /dev/null 2> /dev/null if [ $? != "0" ]; then clear $error echo -e "Downloading magic [${bluetxt}patches.md5${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi $busy $wget --limit-rate=${limitrate} $gentooxurl/magicscript/patches 1> /dev/null 2> /dev/null if [ $? == "0" ]; then $busy if [ `cat ~/patches.md5 | awk '{ print $1 }'` != `md5sum ~/patches | awk '{ print $1 }'` ]; then clear echo "Patch file corrupt, please rerun magic to correct this error." exit 1 fi ### # Backup the magic-database file before doing anything ### cp -a /etc/conf.d/magic-database /etc/conf.d/magic.backup/magic-database-premagic_`date +%Y-%m-%d_%H%M.%S` 1> /dev/null 2> /dev/null sh ~/patches $1 $2 ### # If any error occurs at a script run at a higher level, this is called ### if [ $? != "0" ]; then magicError fi ### # Backup the magic-database file after magic - success ### cp -a /etc/conf.d/magic-database /etc/conf.d/magic.backup/magic-database-postmagic-success_`date +%Y-%m-%d_%H%M.%S` 1> /dev/null 2> /dev/null $good else clear $error echo -e "Downloading magic [${bluetxt}patches${normaltxt}] failed, please try again later." echo -ne "${redtxt}Press enter to quit...${normaltxt}" read exit 1 fi