(def HOME "/home/") (def PORCH "/porch/") (def BOWL "/porch/bowl/") (def USER (first ($ "whoami"))) (def NAME "simon") (def PRONOUN "he") (def CAT " |\ _,,,---,,_ /,`.-'`' -. ;-;;,_ |,4- ) )-,_..;\ ( `'-' '---''(_/--' `-'\_) ") ; get a user from their /home/*/* path (func user-from-path (inp) (first (rest (rest (split inp "/"))))) ; get all users who have a "porch" (func get-porches () (map user-from-path (glob (concat HOME "*" PORCH)))) ; add " * " to the beginning of the string (func concat-star (inp) (concat " * " inp)) ; pretty-print all porches (func print-porches () (print "Neighborhood porches") (map print (map concat-star (get-porches)))) ; get all eats available on a given porch (func get-eats (porch) (glob (concat HOME porch BOWL "*"))) ; get all eats on all porches (func get-porches-with-eats () (map get-eats (get-porches))) ; find the cat (func locate-cat () (glob (concat HOME "*" PORCH NAME))) (func locate () (def porch (locate-cat)) (if (empty? porch) (print (concat "hmm, " PRONOUN "must have wandered off. maybe you should leave some food?")) (print (concat "last I saw " NAME ", " PRONOUN " was on " (user-from-path (first porch)) "'s porch")))) (func get-random-eats () ; expected format ((porch1/eat1 porch1/eat2) (porch2/eat1)...) (safe-first (shuf (safe-first (shuf (get-porches-with-eats)))))) ; prevent a panic on (first ()) (func safe-first (inp) (if (empty? inp) () (first inp))) (func wander () (print (concat "off " PRONOUN " goes!")) (def current-location (locate-cat)) (if (not (empty? current-location)) (print (concat "need to unlink: " (first current-location)))) ; if there's no eats, exit (def target-eats (get-random-eats)) (if (and (list? target-eats) (empty? target-eat)) (exit)) ; eat the food ;(unlink target-eats) ; go to the porch (def target-eats-stem (last (split target-eats "/"))) ;(def target-porch (user-from-path target-eats)) (def target-porch "mryouse") (with-write (concat HOME target-porch PORCH NAME) (write CAT _file_) (write (concat "thank you for the " target-eats-stem "!" (newline)) _file_))) (func init () ; create the new porch and bowl ;(def new_porch (concat HOME USER PORCH)) (def new_porch "newporch") (if (not (exists? new_porch)) ($ (concat "mkdir -m 777 " new_porch))) ;(def new_bowl (concat HOME USER BOWL)) (def new_bowl "newporch/newbowl") (if (not (exists? new_bowl)) ($ (concat "mkdir -m 777 " new_bowl))) ; get rid of the old stuff, if applicable ; TODO (print "You're all set up!") ) (func help () (print "neighborcat -- a neighborhood cat") (print " -> 'init' : initialize your porch") (print " -> 'locate' : locate the neighborcat") (print " -> 'feed ' : locate the neighborcat") (print " -> 'wander' : send the cat a-wandering")) (func main (args) (branch ((empty? args) (print-porches)) ((eq? "init" (first args)) (init)) ((eq? "help" (first args)) (help)) ((eq? "wander" (first args)) (wander)) ((eq? "locate" (first args)) (locate)) ((eq? "feed" (first args)) (feed (rest args))) (#true (print (concat "unknown argument: " (first args)))))) (main (rest (argv)))