File indexing completed on 2024-05-12 05:27:59

0001 #!/bin/bash
0002 
0003 # Copyright 2016 Mycroft AI, Inc.
0004 #
0005 # This file is part of Mycroft Core.
0006 #
0007 # Mycroft Core is free software: you can redistribute it and/or modify
0008 # it under the terms of the GNU General Public License as published by
0009 # the Free Software Foundation, either version 3 of the License, or
0010 # (at your option) any later version.
0011 #
0012 # Mycroft Core is distributed in the hope that it will be useful,
0013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 # GNU General Public License for more details.
0016 #
0017 # You should have received a copy of the GNU General Public License
0018 # along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.
0019 
0020 
0021 # @author Augusto Monteiro
0022 #
0023 # This script assists in the installation and management of
0024 # skills loaded from Github.  
0025 
0026 mycroft_skill_folder="/opt/mycroft/skills"
0027 mycroft_virtualenv=~/.virtualenvs/mycroft/bin/activate
0028 
0029 echo "#######  Mycroft Skill Manager #######"
0030 
0031 function help() {
0032         echo "msm: Mycroft Skill Manager"
0033         echo -e "     Copyright (c) 2017 Mycroft AI, Inc.  All rights reserved.\n"
0034         echo "usage: msm install <repository> or <name>"
0035         echo "     Installs the given Skill into the /opt/mycroft/skills directory"
0036         echo "     where <repository> is the address of the skill in Github."
0037         echo -e "example: msm install https://github.com/ethanaward/demo_skill.git\n"
0038 }
0039 
0040 function install() {
0041         cd $mycroft_skill_folder
0042         if [ -z "$2" ]; then
0043                 echo "You must pass the git url or skill name"
0044                 exit 1
0045         fi
0046         if [[ "$2" == "git@"* || "$2" == "https://"* || "$2" == "http://"* ]]; then
0047                 repo=$2
0048         else
0049                 skill_list="`curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules"`"
0050                 skills=`echo "$skill_list" | grep -n 'submodule' | sed 's/[[:space:]]//g' | sed 's/\[submodule"//g' | sed 's/"\]//g'`
0051                 exact_match=`echo "$skills" | grep -i ".*:$2$"`
0052                 skill=`echo "$skills" | grep -i ".*:.*$2.*"`
0053                 if [ ! -z $exact_match ]; then
0054                         skill=$exact_match
0055                 fi
0056                 git_line=`echo "$skill" | sed 's/\:.*//'`
0057 
0058                 if [[ $skill ==  *$'\n'* ]]; then
0059                         echo -e "Your search has multiple choices\n\n$skill" | sed 's/.*://g'
0060                         exit 2
0061                 else
0062                         if [ -z $git_line ]; then
0063                                 echo "Skill not found"
0064                                 exit 3
0065                         fi
0066                         repo_line=$(($git_line + 2))
0067                         repo=`echo "$skill_list" | sed -n $repo_line'{p;q;}' | sed 's/[[:space:]]//g' | sed 's/url=//g'`
0068                 fi 
0069         fi
0070         git_name=`echo "$repo" | sed 's/.*\///'`
0071         name=`echo "$git_name" | sed 's/.git//'`
0072         echo "Cloning repository"
0073         git clone $repo >> /dev/null
0074         cd $name
0075          if [ -f "requirements.txt" ]; then
0076                 echo "Installing libraries requirements"
0077                 pip install -r requirements.txt
0078         fi 
0079         echo "Skill installed!"
0080 }
0081 
0082 function update() {
0083         cd $mycroft_skill_folder
0084         for d in *; do
0085                 if git -C "$d" rev-parse --git-dir > /dev/null 2>&1; then
0086                         cd $d
0087                         git fetch
0088                         git reset --hard origin/master 
0089                         cd ..
0090                 fi
0091         done
0092 }
0093 
0094 function install_defaults() {
0095         skills=( "alarm" "audio-record" "configuration" "date-time" "desktop-launcher" "ip" "joke" "hello-world" "media" "npr-news" "naptime" "pairing" "personal" "reminder" "installer" "speak" "spelling" "stop" "stock" "volume" "weather" "wiki" "wolfram-alpha" )
0096         for i in "${skills[@]}"
0097         do
0098                 if [ ! -d "$mycroft_skill_folder/skill-$i" ]; then
0099                         install "" "https://github.com/MycroftAI/skill-$i.git"  
0100                 fi
0101         done
0102         update
0103         echo "Installed!"
0104 }
0105 
0106 function list() {
0107         curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules" | grep 'submodule "' | sed 's/\[submodule "//g'| sed 's/"\]//g'
0108 }
0109 
0110 if [ "$1" = "install" ]; then
0111         install $*
0112 elif [ "$1" = "list" ]; then
0113         echo -e "Searching...\n"
0114         list 
0115 elif [ "$1" = "update" ]; then
0116         update 
0117 elif [ "$1" = "default" ]; then
0118         install_defaults 
0119 else
0120         help
0121 fi
0122