#!/bin/bash me=$(echo $0 | sed -e 's=.*/\([^/][^/]*\)$=\1=') hmm=$(which $me) if [ -z "$hmm" ]; then echo "I'm so lost, I can't find myself. Sorry" exit 1 fi MYP4BIN=$(echo $hmm | sed -e "s=/$me$==") what=$(uname) if [ "$what" == 'Darwin' ]; then MYPLAT='bin.darwin90x86_64' BINTYPE='mac' MD5BIN='md5' else kind=$(uname -p) if [ "$kind" == 'x86_64' ]; then MYPLAT='bin.linux26x86_64' BINTYPE='linux' MD5BIN='md5sum -' else if [ "$kind" == 'armv6l' ]; then MYPLAT='bin.linux26armhf' BINTYPE='pi' MD5BIN='md5sum -' fi fi fi if [ -z $MYPLAT ]; then echo 'I have no idea what system you are playing' exit 1 fi P4BIN=$MYP4BIN/p4.$BINTYPE.bin P4DRAW=$MYP4BIN/p4d P4DBIN=$P4DRAW.$BINTYPE.bin P4WEBNAME=p4web.$BINTYPE.bin P4WEBBIN=$MYP4BIN/$P4WEBNAME if [ ! -f $P4BIN ] || [ ! -f $P4DBIN ]; then echo "Downloading Perforce binaries for $MYPLAT ..." which wget > /dev/null if [ $? -eq 0 ]; then wget -q -O $P4BIN ftp://ftp.perforce.com/perforce/r15.1/$MYPLAT/p4 wget -q -O $P4DBIN ftp://ftp.perforce.com/perforce/r15.1/$MYPLAT/p4d wget -q -O $P4WEBBIN ftp://ftp.perforce.com/perforce/r12.1/$MYPLAT/p4web else curl -q -o $P4BIN ftp://ftp.perforce.com/perforce/r15.1/$MYPLAT/p4 curl -q -o $P4DBIN ftp://ftp.perforce.com/perforce/r15.1/$MYPLAT/p4d curl -q -o $P4WEBBIN ftp://ftp.perforce.com/perforce/r12.1/$MYPLAT/p4web fi chmod a+x $P4BIN chmod a+x $P4DBIN chmod a+x $P4WEBBIN echo "Done." fi if [ ! -f $P4DRAW ]; then echo 'Missing link sighted' ln -s $P4DBIN $MYP4BIN/p4d echo "Linking $MYP4BIN/p4d.$BINTYPE.bin to p4d" fi # Check every so often like every 32 times or so if [ "$1" == "check" ] || [ $(($RANDOM / 911)) -eq 0 ]; then THIS_REV=29 NEWER_REV=$(($THIS_REV+1)) which wget > /dev/null if [ $? -eq 0 ]; then wget -S --spider \ https://swarm.workshop.perforce.com/files/guest/alan_h_teague/myb4/b4?v=$NEWER_REV \ > /dev/null 2> /dev/null if [ $? -eq 0 ]; then echo "A newer version of myb4 is available" echo "https://swarm.workshop.perforce.com/files/guest/alan_h_teague/myb4/b4" elif [ "$1" == "check" ]; then echo "No newer version is available" exit 0 fi else cur=$(curl -o /dev/null --silent --head --write-out "%{http_code}\n" \ https://swarm.workshop.perforce.com/files/guest/alan_h_teague/myb4/b4?v=$NEWER_REV) if [ "$cur" == "200" ]; then echo "A newer version of myb4 is available" echo "https://swarm.workshop.perforce.com/files/guest/alan_h_teague/myb4/b4" elif [ "$1" == "check" ]; then echo "No newer version is available" exit 0 fi fi fi rootDir=$($P4BIN -ztag set | \ egrep "^P4CONFIG=$P4CONFIG" | \ cut -d "'" -f 2 | \ sed "s/$P4CONFIG/.p4root/") RAWP4PORT="rsh:/bin/sh -c \"umask 077 && exec $P4DBIN -i -J off -r '$rootDir'\"" makeGroup () # (group owner subgroups protects) { exists=$($P4BIN groups | grep -e "$1") if [ -z "$exists" ]; then $P4BIN group -i > /dev/null < /dev/null $P4BIN user -f -o $userid | $P4BIN user -if > /dev/null } delFromGroup () # (group userid) { group=$1 userid=$2 $P4BIN group -o $group | egrep -v "^[ ][ ]*$userid\$" | $P4BIN group -i > /dev/null $P4BIN user -d -f $userid > /dev/null 2>&1 } reformatRemote () # oldRemoteName { remoteName=$1 echo Converting remote $remoteName to local configuration address=$($P4BIN -ztag remote -o $remoteName | egrep '^... Address' | \ sed -e 's/... Address //') options=$($P4BIN -ztag remote -o $remoteName | egrep '^... Options' | \ sed -e 's/... Options //') description=$($P4BIN -ztag remote -o $remoteName | egrep '^... Description' | \ sed -e 's/... Description //') lls=$($P4BIN -ztag remote -o $remoteName | egrep '^... DepotMap' | \ sed -e 's/... DepotMap[0-9]* //' -e 's=^\(//.*\) //.*=\1=') rrs=$($P4BIN -ztag remote -o $remoteName | egrep '^... DepotMap' | \ sed -e 's/... DepotMap[0-9]* //' -e 's=^.* \(//.*\)=\1=') IFS=$'\n' lefts=($lls) IFS=$'\n' rights=($rrs) for(( x=0; x < ${#lefts[*]}; x++)); do curStream=$(echo ${lefts[$x]} | sed -e 's=//[^/]*/==' -e 's=/[.][.][.]==') mapping=$(echo ${rights[$x]} | sed -e 's=/[.][.][.]==') setupRemote done } setupRemote () { owner=$(echo $address | sed -e 's/@.*//') if [ ! -z "$owner" ]; then address=$(echo $address | sed -e 's/^[^@]*@//') else owner=$P4USER # To catch @ with no userid specified address=$(echo $address | sed -e 's/^@//') fi if [ "$owner" == "$address" ]; then owner=$P4USER fi remote=$($P4BIN key push-$curStream) if [ "$remote" != '0' ]; then if [ "push-$owner:$address" != $remote ]; then echo "Changing remote server details for $curStream - NOT IMPLEMENTED" exit 1 fi fi existing=$($P4BIN remotes | grep "push-$owner:$address") if [ -z "$existing" ]; then echo 'Creating new mapping' $P4BIN -u $owner -p $address login -s > /dev/null if [ $? -ne 0 ]; then echo "Please run '$me -u $owner -p $address login'" exit 1 fi $P4BIN remote -i > /dev/null < /dev/null else if [ "$remote" != '0' ]; then echo 'Replacing previous mapping' $P4BIN remote -o push-$owner:$address | \ sed \ -e "s=^[ ][ ]*//$streamDepot/$curStream/... .*\$= //$streamDepot/$curStream/... $mapping/...=" \ | \ $P4BIN remote -i > /dev/null else echo 'Appending to existing mapping' $P4BIN remote -o push-$owner:$address | \ sed "\$,\$ s=\$= //$streamDepot/$curStream/... $mapping/...=" | \ $P4BIN remote -i > /dev/null $P4BIN key push-$curStream push-$owner:$address > /dev/null fi fi $P4BIN -ztag remote -o push-$owner:$address | \ egrep '^[.][.][.] *(Address|DepotMap|Owner)' | \ sed -e 's/Address/RemoteServer:/' \ -e 's/Owner/RemoteUserID:/' \ -e 's/DepotMap[0-9][0-9]*/ /' \ -e 's/[.][.][.]//' } pendingWork () { work=$($P4BIN -ztag opened //$P4CLIENT/... 2> /dev/null | wc -l) if [ $work -eq 0 ]; then work=$($P4BIN -ztag status //$P4CLIENT/... 2> /dev/null | wc -l) fi if [ $work -gt 0 ]; then echo 'There are pending changes, how do you want to proceed?' # One day add in (m) Move changes if [ $submitPending -eq 1 ]; then read -p '(s) Submit changes (r) Revert changes (q) Quit? ' \ answer else read -p '(s) Shelve changes for later (r) Revert changes (q) Quit? ' \ answer fi case $answer in s) $P4BIN reconcile -f //$P4CLIENT/... oops=$($P4BIN reconcile -n -f //$P4CLIENT/... 2>&1 | \ grep 'no file(s) to reconcile' | \ wc -l) if [ $oops -eq 0 ]; then echo 'There is a problem with reconcile. Fix it before continuing' exit 1 fi if [ $submitPending -eq 1 ]; then read -p 'Change description? ' \ answer if [ -z "$answer" ]; then $P4BIN submit -d 'Checkpoint work' else $P4BIN submit -d "$answer" fi fi ;; r) read -p 'New files will be deleted, edits discarded, etc. Are you sure? (y/n) ' \ answer if [ "$answer" == 'y' ]; then $P4BIN reconcile -f //$P4CLIENT/... > /dev/null 2> /dev/null $P4BIN revert -w //$streamDepot/$curStream/... else echo 'Aborting...' exit 1 fi ;; q) exit 0 ;; esac fi } fetchCurrent () { if [ ! -z "$1" ] && [ "$1" != '-a' ] && [ "$1" != '-u' ]; then echo 'Unknown argument' exit 1 fi map=$($P4BIN key push-$curStream) if [ -z "$map" ]; then echo Please define a map before fetching exit 1 fi submitPending=1 pendingWork address=$($P4BIN -ztag remote -o $map | egrep '^[.][.][.] *Address' | \ sed -e 's/Address//' -e 's/[.][.][.]//') if [ -z "$address" ]; then echo Map $map has a malformed address exit 1 fi mapping=$($P4BIN -ztag remote -o $map | \ egrep '^[.][.][.] *DepotMap' | \ egrep "//$streamDepot/$curStream" | \ awk '{print $4;}') if [ -z "$mapping" ]; then echo Map $map has a malformed DepotMap for "//$streamDepot/$curStream" exit 1 fi owner=$($P4BIN -ztag remote -o $map | egrep '^[.][.][.] *Owner' | \ awk '{print $3;}') if [ -z "$owner" ]; then owner=$P4USER fi $P4BIN -u $owner -p $address login -s > /dev/null if [ $? -ne 0 ]; then echo "Please run '$me -u $owner -p $address login'" exit 1 fi if [ "$owner" != "$P4USER" ]; then addToGroup 'alteregos' "$owner" fi if [ "$1" == '-u' ] || [ "$2" == '-u' ]; then unsub='-u' else unsub='' fi if [ "$1" != '-a' ] && [ "$2" != '-a' ]; then echo "Trying to fetch $mapping from remote $address" echo $P4BIN -u $owner fetch $unsub -v -r $map "//$streamDepot/$curStream/..." $P4BIN -u $owner fetch $unsub -v -r $map "//$streamDepot/$curStream/..." # | grep 'Usage' else echo "Trying to fetch all streams associated with $address" echo $P4BIN -u $owner fetch $unsub -v -r $map $P4BIN -u $owner fetch $unsub -v -r $map # | grep 'Usage' fi #cnt=$($P4BIN -u $owner fetch -v -r $map $mapping 2> /dev/null | wc -l) $P4BIN sync //... > /dev/null if [ "$owner" != "$P4USER" ]; then delFromGroup 'alteregos' "$owner" fi } helpMe () { if [ -z "$1" ]; then $P4BIN "$@" exit $? fi if [ -z "$2" ]; then $P4BIN "$@" exit $? fi if [ "$2" == '-r' ]; then $P4BIN help $3 exit $? fi case $2 in dvcs) cat <