Skip to content

Instantly share code, notes, and snippets.

@chipsenkbeil
Created October 20, 2016 18:51
Show Gist options
  • Select an option

  • Save chipsenkbeil/d037f493623cc997999294e4ca8101da to your computer and use it in GitHub Desktop.

Select an option

Save chipsenkbeil/d037f493623cc997999294e4ca8101da to your computer and use it in GitHub Desktop.

Revisions

  1. chipsenkbeil created this gist Oct 20, 2016.
    132 changes: 132 additions & 0 deletions music.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    #!/bin/sh

    # Local dir: Place to mount remote dir
    # Remote dir: Place to connect to when mounting fs
    LOCAL_DIR=/mnt/music
    REMOTE_DIR=senkwich@senkbeil.org:/datadrive/music

    # Internal program settings
    MUSIC_PLAYER=cmus
    SSHFS=sshfs
    UMOUNT=umount

    # Internal CLI options
    DO_MOUNT=false
    DO_UNMOUNT=false
    DO_PLAYER=false
    VERBOSE=false
    HELP=false

    # Parse CLI arguments
    for i in "$@"; do
    case $i in
    --mount)
    DO_MOUNT=true
    shift # past argument=value
    ;;
    --unmount)
    DO_UNMOUNT=true
    shift # past argument=value
    ;;
    --remount)
    DO_MOUNT=true
    DO_UNMOUNT=true
    shift # past argument=value
    ;;
    --start-player)
    DO_PLAYER=true
    shift # past argument=value
    ;;
    -p=*|--player=*)
    MUSIC_PLAYER="${i#*=}"
    shift # past argument=value
    ;;
    -l=*|--local-dir=*)
    LOCAL_DIR="${i#*=}"
    shift # past argument=value
    ;;
    -r=*|--remote-dir=*)
    REMOTE_DIR="${i#*=}"
    shift # past argument=value
    ;;
    -h|--help)
    HELP=true
    shift # past argument with no value
    ;;
    -v|--verbose)
    VERBOSE=true
    shift # past argument with no value
    ;;
    *)
    # unknown option
    ;;
    esac
    done

    # Update paths to be absolute
    MUSIC_PLAYER_ABS=`which $MUSIC_PLAYER`
    SSHFS_ABS=`which $SSHFS`
    UMOUNT_ABS=`which $UMOUNT`

    # Check if needed programs are defined
    if [ -z "${MUSIC_PLAYER_ABS:+1}" ]; then
    printf "Cannot find music player: $MUSIC_PLAYER\n"
    exit 1
    fi
    if [ -z "${SSHFS_ABS:+1}" ]; then
    printf "Cannot find SSH FS: $SSHFS\n"
    exit 1
    fi
    if [ -z "${UMOUNT_ABS:+1}" ]; then
    printf "Cannot find umount: $UMOUNT\n"
    exit 1
    fi

    # If verbose flag toggled, display internal programs
    if [ $VERBOSE == true ]; then
    printf "=== PROGRAMS ===\n"
    printf "Music player: $MUSIC_PLAYER\n"
    printf "SSH FS: $SSHFS\n"
    printf "Umount: $UMOUNT\n"
    printf "\n"
    printf "=== DIRECTORIES ===\n"
    printf "Local directory: $LOCAL_DIR\n"
    printf "Remote directory: $REMOTE_DIR\n"
    printf "\n"
    fi

    # If help requested (or nothing provided)
    if [ $HELP == true ] || { [ $DO_MOUNT == false ] && [ $DO_UNMOUNT == false ] && [ $DO_PLAYER == false ]; }; then
    printf "Usage: music.sh [options]\n"
    printf "\n"
    printf "Options:\n"
    printf "\t--mount\t\t\tMounts the music dir\n"
    printf "\t--unmount\t\tUnmounts the music dir\n"
    printf "\t--remount\t\tUnmounts and then mounts the music dir\n"
    printf "\t--start-player\t\tOpens up music player to mounted dir\n"
    printf "\t-p, --player\t\tSets the music player\n"
    printf "\t-l, --local-dir\t\tSets local mounting directory\n"
    printf "\t-r, --remote-dir\tSets remote mounting directory\n"
    printf "\t-h, --help\t\tPrints help message\n"
    printf "\t-v, --verbose\t\tIncludes additional log output\n"
    printf "\n"
    exit 0
    fi

    # Unmount music
    if [ $DO_UNMOUNT == true ]; then
    printf "Unmounting $LOCAL_DIR\n"
    $UMOUNT $LOCAL_DIR
    fi

    # Mount music
    if [ $DO_MOUNT == true ]; then
    printf "Mounting $LOCAL_DIR from $REMOTE_DIR\n"
    $SSHFS $REMOTE_DIR $LOCAL_DIR
    fi

    # Open music player
    if [ $DO_PLAYER == true ]; then
    printf "Opening $MUSIC_PLAYER in $LOCAL_DIR\n"
    (cd $LOCAL_DIR && $MUSIC_PLAYER)
    fi