Updated kvmsetup to add kvm.sh to both bash and zsh profiles if found

This commit is contained in:
Graeme Christie 2014-05-28 01:12:14 +08:00
parent bac5f06a4c
commit 54171dd221
1 changed files with 25 additions and 12 deletions

View File

@ -7,6 +7,18 @@ _kvmsetup_has() {
return $? return $?
} }
_kvmsetup_update_profile() {
local profile="$1"
local sourceString="$2"
if ! grep -qc 'kvm.sh' $profile; then
echo "=> Appending source string to $profile"
echo "" >> "$profile"
echo $sourceString >> "$profile"
else
echo "=> Source string already in $profile"
fi
}
if [ -z "$KRE_USER_HOME" ]; then if [ -z "$KRE_USER_HOME" ]; then
eval KRE_USER_HOME=~/.kre eval KRE_USER_HOME=~/.kre
fi fi
@ -33,7 +45,6 @@ curl -s "$KVM_SOURCE" -o "$KRE_USER_HOME/kvm/kvm.sh" || {
return 1 return 1
} }
echo echo
# Detect profile file if not specified as environment variable (eg: PROFILE=~/.myprofile). # Detect profile file if not specified as environment variable (eg: PROFILE=~/.myprofile).
@ -42,22 +53,29 @@ if [ -z "$PROFILE" ]; then
PROFILE="$HOME/.bash_profile" PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then elif [ -f "$HOME/.bashrc" ]; then
PROFILE="$HOME/.bashrc" PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.profile" ]; then elif [ -f "$HOME/.profile" ]; then
PROFILE="$HOME/.profile" PROFILE="$HOME/.profile"
fi fi
fi fi
if [ -f "$HOME/.zshrc" ]; then
ZPROFILE="$HOME/.zshrc"
fi
echo ">>$ZPROFILE"
SOURCE_STR="[ -s \"$KRE_USER_HOME/kvm/kvm.sh\" ] && . \"$KRE_USER_HOME/kvm/kvm.sh\" # this loads kvm" SOURCE_STR="[ -s \"$KRE_USER_HOME/kvm/kvm.sh\" ] && . \"$KRE_USER_HOME/kvm/kvm.sh\" # this loads kvm"
if [ -z "$PROFILE" ] || [ ! -f "$PROFILE" ] ; then if [[ -z "$PROFILE" && -z $ZPROFILE ]] || [[ ! -f "$PROFILE" && ! -f "$ZPROFILE" ]] ; then
if [ -z $PROFILE ]; then if [ -z $PROFILE ]; then
echo "=> Profile not found. Tried ~/.bash_profile ~/.zshrc and ~/.profile." echo "=> Profile not found. Tried ~/.bash_profile ~/.zshrc and ~/.profile."
echo "=> Create one of them and run this script again" echo "=> Create one of them and run this script again"
else elif [[ ! -f $PROFILE ]]; then
echo "=> Profile $PROFILE not found" echo "=> Profile $PROFILE not found"
echo "=> Create it (touch $PROFILE) and run this script again" echo "=> Create it (touch $PROFILE) and run this script again"
else
echo "=> Profile $ZPROFILE not found"
echo "=> Create it (touch $ZPROFILE) and run this script again"
fi fi
echo " OR" echo " OR"
echo "=> Append the following line to the correct file yourself:" echo "=> Append the following line to the correct file yourself:"
@ -65,13 +83,8 @@ if [ -z "$PROFILE" ] || [ ! -f "$PROFILE" ] ; then
echo " $SOURCE_STR" echo " $SOURCE_STR"
echo echo
else else
if ! grep -qc 'kvm.sh' $PROFILE; then [[ -n $PROFILE ]] && _kvmsetup_update_profile "$PROFILE" "$SOURCE_STR"
echo "=> Appending source string to $PROFILE" [[ -n $ZPROFILE ]] && _kvmsetup_update_profile "$ZPROFILE" "$SOURCE_STR"
echo "" >> "$PROFILE"
echo $SOURCE_STR >> "$PROFILE"
else
echo "=> Source string already in $PROFILE"
fi
fi fi
echo "=> Type 'source $KRE_USER_HOME/kvm/kvm.sh' to start using kvm" echo "=> Type 'source $KRE_USER_HOME/kvm/kvm.sh' to start using kvm"