#!/bin/bash # sheepdog version .03 # This is ludicrously beta software. It will probably cause your hair to # turn green or light on fire. Seriously, it's barely been tested and has # the capacity to blow up your entire world. Use it carefully if at all. # Copyright (c) 2006, Faisal N. Jawdat # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. Redistributions in # binary form must reproduce the above copyright notice, this list of # conditions and the following disclaimer in the documentation and/or # other materials provided with the distribution. Neither the name of # Faisal N. Jawdat nor the names of its contributors may be used to # endorse or promote products derived from this software without specific # prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT # HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL # THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SHEEP_ARGUMENT=$1 SHEEP_HOST=`grep '^host: ' ~/.sheepdogrc|cut -d\ -f2` SHEEP_FILES=`grep '^files: ' ~/.sheepdogrc | cut -d: -f2` SHEEP_SH_MASTERS='.profile .bashrc .bash_login .bash_logout .bash_profile .zshenv .zprofile .zshrc .zlogin .zlogout' SHEEP_CSH_MASTERS='.cshrc .tcshrc .login .logout' function help { echo 'Valid commands are:' echo '' echo ' help: display this text.' echo ' initialize: create the sheepdog directory structure.' echo ' import: import your old . files into the (fresh) structure.' echo ' lninstall: set up the symbolic links.' echo ' scmupdate: get the latest files from the repository (cvs or svn).' echo ' scmcommit: commit the current environment to the repository (cvs or svn).' echo '' } function initialize { if [ -d ~/.sheepdog ]; then echo "~/.sheepdog exists. Cannot proceed." exit else mkdir ~/.sheepdog cd ~/.sheepdog mkdir scripts mkdir scripts/pre scripts/main scripts/post for file in $SHEEP_SH_MASTERS ; do create_sh $file; done for file in $SHEEP_CSH_MASTERS ; do create_csh $file; done mkdir configs create_rc echo 'Directories and templates initalized.' echo 'You should now create a ~/.sheepdogrc.' echo 'The easiest way is to copy ~/.sheepdog/configs/sample.sheeprc.' fi } function create_sh { echo 'SHEEPDOG_HOST=`grep "^host:" ~/.sheepdogrc|cut -d\ -f2`' > ~/.sheepdog/scripts/$1 cat >> ~/.sheepdog/scripts/$1 << EOF if [ -f ~/.sheepdog/scripts/pre/$1.\$SHEEPDOG_HOST ]; then . ~/.sheepdog/scripts/pre/$1.\$SHEEPDOG_HOST; fi if [ -f ~/.sheepdog/scripts/main/$1 ]; then . ~/.sheepdog/scripts/main/$1; fi if [ -f ~/.sheepdog/scripts/post/$1.\$SHEEPDOG_HOST ]; then . ~/.sheepdog/scripts/post/$1.\$SHEEPDOG_HOST; fi EOF } function create_csh { echo 'setenv SHEEPDOG_HOST `grep "^host:" ~/.sheepdogrc|cut -d\ -f2`' > ~/.sheepdog/scripts/$1 cat >> ~/.sheepdog/scripts/$1 << EOF if -f ~/.sheepdog/scripts/pre/$1.\$SHEEPDOG_HOST source ~/.sheepdog/scripts/pre/$1.\$SHEEPDOG_HOST if -f ~/.sheepdog/scripts/main/$1 source ~/.sheepdog/scripts/main/$1 if -f ~/.sheepdog/scripts/post/$1.\$SHEEPDOG_HOST source ~/.sheepdog/scripts/post/$1.\$SHEEPDOG_HOST EOF } function create_rc { cat > ~/.sheepdog/configs/sample.sheepdogrc << EOF host: hostname files: .bashrc .profile EOF } function import { if [ -f ~/.sheepdogrc ] && [ -d ~/.sheepdog ]; then echo "Importing $SHEEP_FILES" mkdir ~/.sheepdog/backup mkdir ~/.sheepdog/backup/$SHEEP_HOST for file in $SHEEP_FILES ; do cp $file ~/.sheepdog/backup/$SHEEP_HOST/$file done for file in $SHEEP_FILES ; do mv $file ~/.sheepdog/scripts/post/$file.$SHEEP_HOST done echo 'The files were moved to ~/.sheepdog/scripts/post/.' echo 'and will run as-is after any generic scripts.' echo "The files were also backed up to ~/.sheepdog/backup/$SHEEP_HOST." echo 'You should now run:' echo ' sheepdog lninstall' else echo 'Something is wrong with your config.' echo 'Have you initialized the .sheepdog directory?' echo 'Have you created a .sheepdogrc?' exit fi } function lninstall { if [ -f ~/.sheepdogrc ] && [ -d ~/.sheepdog ]; then echo Host ID: $SHEEP_HOST; for file in $SHEEP_FILES ; do echo "Linking $file." ln -s .sheepdog/scripts/$file; done else echo 'Something is wrong with your config.' echo 'Have you initialized the .sheepdog directory?' echo 'Have you created a .sheepdogrc?' exit fi } function scmupdate { cd ~/.sheepdog if [ -d ~/.sheepdog/CVS ]; then cvs up elif [ -d ~/.sheepdog/.svn ]; then svn up else echo "~/.sheepdog doesn't seem to be under source control." exit fi } function scmcommit { cd ~/.sheepdog if [ -d ~/.sheepdog/CVS ]; then cvs ci elif [ -d ~/.sheepdog/.svn ]; then svn ci else echo "~/.sheepdog doesn't seem to be under source control." exit fi } cd ~ if [ "$SHEEP_ARGUMENT" = 'help' ]; then help elif [ "$SHEEP_ARGUMENT" = 'initialize' ]; then initialize elif [ "$SHEEP_ARGUMENT" = 'import' ]; then import elif [ "$SHEEP_ARGUMENT" = 'lninstall' ]; then lninstall elif [ "$SHEEP_ARGUMENT" = 'scmupdate' ]; then scmupdate elif [ "$SHEEP_ARGUMENT" = 'scmcommit' ]; then scmcommit else echo "No action specified. Valid actions are:" echo "help, initialize, import, lninstall, scmupdate, scmcommit" fi