chiark / gitweb /
Fix error message; rewind after labelling.
[chiark-utils.git] / sync-accounts / grab-account
1 #!/bin/sh
2
3 set -e
4
5 if [ $# -lt 2 -o $# -gt 3 ]
6 then
7         echo >&2 \
8 'usage: grab-account <localuser> <shorthostname> [<remoteuser>]
9 creates an entry in /etc/sync-accounts, and runs sync-accounts
10 $Id: grab-account,v 1.2 1999-01-03 02:56:07 ian Exp $'
11         exit 1
12 fi
13
14 lu="$1"
15 sh="$2"
16
17 if [ $# -gt 2 ]
18 then
19         ru="$3"
20 else
21         ru="$1"
22 fi
23
24 cf=/etc/sync-accounts
25
26 if perl -ne 'exit 1 if m/^\s*user\s+'$lu'\s/;' <$cf
27 then
28         perl -pe '
29                 next unless m/^\s*host\s+'$sh'\s*$/...m/^host|^end/;
30                 next unless m/^\s*addhere\s*$/;
31                 next if $done++;
32                 print "user '$lu'".("'$lu'" eq "'$ru'" ? "" : " remote='$ru'")."\n"
33                         or die $!;
34          END {
35                 print(STDERR "\`addhere'\'' line not found\n"), $?=1 if !$? && !$done;
36          }
37                 ' $cf >$cf.new
38         mv -f $cf.new $cf
39 else
40         echo "entry already exists in $cf, leaving alone"
41 fi
42
43 sync-accounts $sh