|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Setup appimage shell script |
| 4 | +# Author: Kang Lin <kl222@126.com> |
| 5 | + |
| 6 | +#set -v |
| 7 | +set -e |
| 8 | +#set -x |
| 9 | + |
| 10 | +INSTALL_DIR=~/AppImage/io.github.KangLin.SerialPortAssistant |
| 11 | +usage_long() { |
| 12 | + echo "$0 [-h|--help] [--install=<install directory>]" |
| 13 | + echo " -h|--help: show help" |
| 14 | + echo "Directory:" |
| 15 | + echo " --install: Set install directory" |
| 16 | + exit |
| 17 | +} |
| 18 | +# [如何使用getopt和getopts命令解析命令行选项和参数](https://zhuanlan.zhihu.com/p/673908518) |
| 19 | +# [【Linux】Shell命令 getopts/getopt用法详解](https://blog.csdn.net/arpospf/article/details/103381621) |
| 20 | +if command -V getopt >/dev/null; then |
| 21 | + #echo "getopt is exits" |
| 22 | + #echo "original parameters=[$@]" |
| 23 | + # -o 或 --options 选项后面是可接受的短选项,如 ab:c:: ,表示可接受的短选项为 -a -b -c , |
| 24 | + # 其中 -a 选项不接参数,-b 选项后必须接参数,-c 选项的参数为可选的 |
| 25 | + # 后面没有冒号表示没有参数。后跟有一个冒号表示有参数。跟两个冒号表示有可选参数。 |
| 26 | + # -l 或 --long 选项后面是可接受的长选项,用逗号分开,冒号的意义同短选项。 |
| 27 | + # -n 选项后接选项解析错误时提示的脚本名字 |
| 28 | + OPTS=help,install: |
| 29 | + ARGS=`getopt -o h,v:: -l $OPTS -n $(basename $0) -- "$@"` |
| 30 | + if [ $? != 0 ]; then |
| 31 | + echo "exec getopt fail: $?" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + #echo "ARGS=[$ARGS]" |
| 35 | + #将规范化后的命令行参数分配至位置参数($1,$2,......) |
| 36 | + eval set -- "${ARGS}" |
| 37 | + #echo "formatted parameters=[$@]" |
| 38 | + |
| 39 | + while [ $1 ] |
| 40 | + do |
| 41 | + #echo "\$1: $1" |
| 42 | + #echo "\$2: $2" |
| 43 | + case $1 in |
| 44 | + --install) |
| 45 | + INSTALL_DIR=$2 |
| 46 | + shift 2 |
| 47 | + ;; |
| 48 | + --) # 当解析到“选项和参数“与“non-option parameters“的分隔符时终止 |
| 49 | + shift |
| 50 | + break |
| 51 | + ;; |
| 52 | + -h | -help) |
| 53 | + usage_long |
| 54 | + shift |
| 55 | + ;; |
| 56 | + *) |
| 57 | + usage_long |
| 58 | + break |
| 59 | + ;; |
| 60 | + esac |
| 61 | + done |
| 62 | +fi |
| 63 | + |
| 64 | +if [ -f ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop ]; then |
| 65 | + OLD_UNINSTALL=$(dirname $(readlink -f ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop)) |
| 66 | + if [ -f $OLD_UNINSTALL/uninstall.sh ]; then |
| 67 | + echo "" |
| 68 | + $OLD_UNINSTALL/uninstall.sh |
| 69 | + fi |
| 70 | +fi |
| 71 | + |
| 72 | +INSTALL_DIR=$(readlink -f $INSTALL_DIR) |
| 73 | +if [ ! -d $INSTALL_DIR ]; then |
| 74 | + mkdir -p $INSTALL_DIR |
| 75 | +fi |
| 76 | + |
| 77 | +ROOT_DIR=$(dirname $(readlink -f $0)) |
| 78 | +if [ ! -d /usr/share/icons/hicolor/scalable/apps ]; then |
| 79 | + mkdir -p /usr/share/icons/hicolor/scalable/apps |
| 80 | +fi |
| 81 | + |
| 82 | +pushd $ROOT_DIR > /dev/null |
| 83 | + |
| 84 | +APPIMAGE_FILE=`ls SerialPortAssistant_*.AppImage` |
| 85 | +if [ $INSTALL_DIR != $ROOT_DIR ]; then |
| 86 | + cp $APPIMAGE_FILE $INSTALL_DIR/$APPIMAGE_FILE |
| 87 | + cp io.github.KangLin.SerialPortAssistant.svg $INSTALL_DIR/io.github.KangLin.SerialPortAssistant.svg |
| 88 | + cp io.github.KangLin.SerialPortAssistant.desktop $INSTALL_DIR/io.github.KangLin.SerialPortAssistant.desktop |
| 89 | +fi |
| 90 | + |
| 91 | +sed -i "s#Exec=.*#Exec=${INSTALL_DIR}/${APPIMAGE_FILE}#g" $INSTALL_DIR/io.github.KangLin.SerialPortAssistant.desktop |
| 92 | +if [ ! -f ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop ]; then |
| 93 | + ln -s $INSTALL_DIR/io.github.KangLin.SerialPortAssistant.desktop ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop |
| 94 | +fi |
| 95 | +if [ ! -d ~/.icons/hicolor/scalable/apps ]; then |
| 96 | + mkdir -p ~/.icons/hicolor/scalable/apps |
| 97 | +fi |
| 98 | +if [ ! -f ~/.icons/hicolor/scalable/apps/io.github.KangLin.SerialPortAssistant.svg ]; then |
| 99 | + ln -s $INSTALL_DIR/io.github.KangLin.SerialPortAssistant.svg ~/.icons/hicolor/scalable/apps/io.github.KangLin.SerialPortAssistant.svg |
| 100 | +fi |
| 101 | + |
| 102 | +update-desktop-database ~/.local/share/applications |
| 103 | + |
| 104 | +echo "echo \"Uninstall rabbit remote control in $(dirname $(readlink -f ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop))\"" > $INSTALL_DIR/uninstall.sh |
| 105 | +echo "rm ~/.local/share/applications/io.github.KangLin.SerialPortAssistant.AppImage.desktop" >> $INSTALL_DIR/uninstall.sh |
| 106 | +echo "rm ~/.icons/hicolor/scalable/apps/io.github.KangLin.SerialPortAssistant.svg" >> $INSTALL_DIR/uninstall.sh |
| 107 | +echo "rm -fr $INSTALL_DIR" >> $INSTALL_DIR/uninstall.sh |
| 108 | +chmod u+x $INSTALL_DIR/uninstall.sh |
| 109 | +chmod u+x $INSTALL_DIR/$APPIMAGE_FILE |
| 110 | + |
| 111 | +echo "" |
| 112 | +echo "Install rabbit remote control AppImage to \"$INSTALL_DIR\"." |
| 113 | +echo "" |
| 114 | +echo "If you want to uninstall it. Please execute:" |
| 115 | +echo " $INSTALL_DIR/unistasll.sh" |
| 116 | +echo "" |
| 117 | + |
| 118 | +popd > /dev/null |
0 commit comments