#!/bin/bash # go command function go() { ARG1=$1; ARG2=$2; ARG3=$3; if [[ -z "$ARG1" ]]; then __func_head "[MODE] SITE TARGET_DIR [..SLICE]" __func_help "up [MODE] TARGET_DIR" __func_help "down [MODE] TARGET_DIR [..SLICE]" echo return 1 fi NAV=false case "$ARG1" in p) shift CC='pushd' ;; c) shift CC='cd' ;; e) shift CC='echo' ;; up|down) shift NAV=true ;; *) CC='cd' ARG3=$ARG2 ARG2=$ARG1 ;; esac if [ $NAV = true ]; then __go_nav $ARG1 $@ return $? fi if [[ -f ~/.go_conf ]]; then source ~/.go_conf else echo "Cannot source config file" return 1 fi SITE=$( ls "$UDEV" | grep -m 1 "$ARG2" ) if [[ -z "$SITE" ]]; then echo "No such site: $ARG2" find "$UDEV/" -maxdepth 1 -type d | sed "s/^.\+\/\([^\/]\+\)\/\?$/ \1/g" return 1 fi if [[ -z "$ARG3" ]]; then $CC "$UDEV/$SITE" return $? fi for ((i = 0; i < ${#LOCS[@]}; i++)) do LOC=${LOCS[i]} LOCATION=$(ls -d "$UDEV/$SITE/$LOC/"*"$ARG3"*/ 2> /dev/null ) if [[ -n "$LOCATION" ]]; then break fi done # shift ARG2 ARG3 shift; shift if [[ -n "$LOCATION" ]]; then __dive $CC "$LOCATION" $@ return $? else __dive $CC "$UDEV/$SITE" "$ARG3" $@ return $? fi } __dive() { CC=$1 WDIR=$2 shift; shift if [ -z "$*" ]; then $CC "$WDIR" return $? fi OIFS=$IFS IFS="*" PATT="*$**" IFS=$OIFS pushd "$WDIR" > /dev/null DIR=$(find . -type d -path "$PATT" -print -quit | cut -c 3- ) popd > /dev/null if [[ -z $DIR ]]; then echo "Path: $WDIR" echo " - Pattern not found: $PATT" return 1 fi $CC "$WDIR/$DIR" } __go_nav() { ARG1=$1; ARG2=$2; ARG3=$3; case "$ARG2" in p) shift CC='pushd' ;; c|"") shift CC='cd' ;; e) shift CC='echo' ;; *) CC='cd' ARG3=$ARG2 ;; esac if [[ "$ARG1" == "up" ]]; then OIFS=$IFS IFS="/" read -a PWDA <<< "$(pwd)" IFS=$OIFS MARK=0 S_LEN=$(( ${#PWDA[@]} - 2 )) for (( i=1; i<=$S_LEN; i++ )) do STACK=${PWDA[$i]} if [ -z "$STACK" ]; then continue fi if [ -n "$ARG3" ] && [[ "$STACK" =~ "$ARG3" ]]; then MARK=$i fi done if [ -z "$ARG3" ]; then # Go up 1 level MARK=$(( ${#PWDA[@]} - 2 )) fi if [ $MARK -eq 0 ]; then echo "No such token in up stack: $ARG3" for (( i=1; i<=$S_LEN; i++ )); do echo " ${PWDA[$i]}"; done return 1 fi DIR='/' for (( i=1; i<=$MARK; i++ )) do DIR="$DIR${PWDA[$i]}/" done $CC "$DIR" else shift __dive $CC . $@ return $? fi }