Snippets

Travis Wellman bin/hat manages multiple wikis for multiple projects

Created by Travis Wellman
#!/bin/bash

nmap_svcs=/usr/share/nmap/nmap-services
known_tcp_ports_f () {
    grep -v ^# $nmap_svcs |cut -f2 |grep -vE '/udp$' |grep -oE '^[0-9]+'
}
if [[ ! -v known_tcp_ports ]] && [ -r $nmap_svcs ]
then known_tcp_ports=($(known_tcp_ports_f))
fi


cksum_port () {
    local inp_cksum=$(echo -n "$*" |cksum |cut -d' ' -f1)
    if [[ ! -v known_tcp_ports ]]
    then head -c4 <<<$inp_cksum
         return $?
    fi
    for port_gen_offset in `seq 6`
    do local potential_port=$(tail -c+$port_gen_offset <<<$inp_cksum |head -c4)
       local c=0
       until [ ! $c -lt ${#known_tcp_ports[*]} ] ||
                 [ $potential_port = ${known_tcp_ports[$c]} ]
       do ((c++))
       done
       if [ $c -eq ${#known_tcp_ports[*]} ]
       then echo $potential_port
            return $?
       fi
    done
    return 1
}

# options for all cases
if [[ ! -v hopts ]]
then hopts="-e UTF-8 --subdirectories"
fi


case "$1" in

[Dd]ocuments)
prefx=thelight        
port=$(cksum_port documents)
rm -v ~/tmp/hatta-thelight.?????.out
hat_out=$(mktemp -p ~/tmp hatta-thelight.XXXXX.out)

hatta_cache=$HOME/tmp/hatta-documents
mkdir -p $hatta_cache

set -x
nohup python -m hatta $hopts \
      -d ~/documents/wiki/pages \
      -R ~/documents/wiki \
      --port=$port \
      -t $hatta_cache \
      -n 'thelight wiki' &> $hat_out &
sleep 1
;;


career)
career_wiki_repo=$HOME/documents/career/wiki
port=$(cksum_port $career_wiki_repo)
rm -v ~/tmp/hatta-career.?????.out
hat_out=$(mktemp -p ~/tmp hatta-career.XXXXX.out)

hatta_cache=$HOME/tmp/hatta-career
mkdir -p $hatta_cache

set -x
nohup python -m hatta $hopts \
      -d $career_wiki_repo \
      -R $career_wiki_repo \
      --port=$port \
      -t $hatta_cache \
      -n 'career wiki' &> $hat_out &
sleep 1
;;


/*)
bn="$(basename "$1")"
port=$(cksum_port $bn)
rm -v ~/tmp/hatta-$bn.?????.out
hat_out=$(mktemp -p ~/tmp hatta-$bn.XXXXX.out)

hatta_cache=$HOME/tmp/hatta-$bn
mkdir -p $hatta_cache

set -x
nohup python -m hatta $hopts \
      -d "$1"/wiki \
      -R "$1" \
      --port=$port \
      -t $hatta_cache \
      -n "$bn wiki" &> $hat_out &
sleep 1
;;


*)
dirct="$(echo $HOME/*/$1/.hg |awk '{print $1}')"
dirct="$(dirname "$dirct")"
bn="$(basename "$dirct")"
port=$(cksum_port $bn)
rm -v ~/tmp/hatta-$bn.?????.out
hat_out=$(mktemp -p ~/tmp hatta-$bn.XXXXX.out)

hatta_cache=$HOME/tmp/hatta-$bn
mkdir -p $hatta_cache

set -x
nohup python -m hatta $hopts \
      -d "$dirct"/wiki \
      -R "$dirct" \
      --port=$port \
      -t $hatta_cache \
      -n "$bn wiki" &> $hat_out &
sleep 1
;;

esac

if [[ -v hat_out ]] && [[ -v port ]]
then until curl -sf http://localhost:$port/ >/dev/null
     do sleep 1
     done
     firefox http://localhost:$port/ &
     tail $hat_out
fi

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.