#!/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: 20050210001 # 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. # # DONT change the position of the "NewVersion" line, it MUST always be on the # second line directly after "#!/bin/bash"!!! ### ### # This cleans up all temporary files ### cleanup() { rm -rf ~/patches* rm -rf ~/magic.md5 rm -rf ~/*.b0x rm -rf ~/.runningmagic rm -rf ~/motd.gentoox rm -rf /tmp/serverdown rm -rf /tmp/patches.md5 } if [ -f /etc/mce_ver ]; then rm -rf /mnt/fatx/e/linuxboot.cfg umount /dev/hda55 1> /dev/null 2> /dev/null grep -v hda55 /etc/fstab > /etc/fstab.tmp mv /etc/fstab.tmp /etc/fstab chmod 644 /etc/fstab fi ### # The following code traps when a user hits Ctrl-C and cleans everything up before # exiting ### trap escape 20 2 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 ### # 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 gggg" 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 ~/.runningmagic ]; 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 fi cleanup echo "running" > ~/.runningmagic 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 ### # 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 -n "Press enter to quit..." read cleanup exit 1 fi $busy if [ `cat ./serverdown | grep AMIDOWN | awk '{ print $3 }'` == "yes" ]; then ### # Backdoor for development ### if [ -z $SUREASSURE ]; then sh ./serverdown cleanup 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 -n "Press enter to quit..." read cleanup 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 -n "Press enter to quit..." read cleanup 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 -n "Press enter to quit..." read cleanup 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 -n "Press enter to quit..." read cleanup exit 1 fi rm -rf /etc/patches.magic mv /tmp/patches.magic /etc/patches.magic chmod 644 /etc/patches.magic fi clear $good 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 ${normaltxt}- ${bluetxt}DONE " echo -e "${redtxt}" echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -e echo -e " --------------------------------------------------------------------" echo -e "\n Gentoox is distributed under the GNU General Public License. " sleep 1 ### # By now, these files MUST exist ### source /etc/functions.magic source /etc/patches.magic $downloading cd ~/ 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 " DOWNLOADING PATCH INFORMATION" echo -e " PLEASE WAIT! " echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -en "${bluetxt}" echo -e " CURRENT LIMIT: ${limithuman}KBPS" echo -en "${normaltxt}" echo -e " --------------------------------------------------------------------" echo -e "\n Gentoox is distributed under the GNU General Public License. " $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 -n "Press enter to quit..." read cleanup 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." cleanup 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 -n "Press enter to quit..." read cleanup 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." cleanup 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 $error echo -e "${redtxt}" echo "Magic encountered the above error, please try again later." echo -e "${bluetxt}" echo "Emerge/ Unmerge errors are problems with portage, please try resyncing or" echo "waiting before running magic again. Emerge errors are also common with" echo "UNSET CLOCKS! To set your clock, as root run: date MMDDhhmmYYYY" echo "" echo "Download errors are most likely to be Gentoox central server/ DNS errors" echo "and should be fixed almost immediately. If you get a download error, " echo "simply try running magic again straight away." echo "" echo "An 'Invalid selection' error means that your input to a question was not" echo "correct - please note that CaSe matters. If you receive an invalid" echo "selection error, please re-run magic and answer all questions correctly" echo "questions have valid answer syntax printed in brackets." echo "" echo "'Decompression Failed.' means that data got corrupted whilst downloading" echo "a compressed archive. Try running magic again to see if the problem" echo "corrects itself." echo -e "${normaltxt}" echo -n "Press enter to quit..." read cleanup ### # Backup the magic-database file after running but failed ### cp -a /etc/conf.d/magic-database /etc/conf.d/magic.backup/magic-database-postmagic-fail_`date +%Y-%m-%d_%H%M.%S` 1> /dev/null 2> /dev/null exit 1 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 cleanup else clear $error echo -e "Downloading magic [${bluetxt}patches${normaltxt}] failed, please try again later." echo -n "Press enter to quit..." read cleanup exit 1 fi ### # Just to be sure.... ### cleanup