initial attempts at user add scripts for FreeBSD packaging.
[monkeysphere.git] / packaging / freebsd / pkg-install
1 #!/bin/sh
2
3 # an installation script for monkeysphere (borrowing liberally from
4 # Wnn6's port and from monkeysphere's debian/monkeysphere.postinst)
5
6 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 # Copyright 2008
8
9 # FIXME: is /var/lib/monkeysphere the right place for this stuff on
10 # FreeBSD?
11 VARLIB="/var/lib/monkeysphere"
12
13 check_pw()
14 {
15     if which -s pw; then
16         :
17     else
18         cat <<EOF
19
20 This system looks like a pre-2.2 version of FreeBSD.  We see that it
21 is missing the "pw" utility.  We need this utility.  Please get and
22 install it, and try again.  You can get the source from:
23
24   ftp://ftp.freebsd.org/pub/FreeBSD/FreeBSD-current/src/usr.sbin/pw.tar.gz
25
26 EOF
27         exit 1
28     fi
29 }
30
31 ask() {
32     local question default answer
33
34     question=$1
35     default=$2
36     if [ -z "${PACKAGE_BUILDING}" ]; then
37         read -p "${question} (y/n) [${default}]? " answer
38     fi
39     if [ x${answer} = x ]; then
40         answer=${default}
41     fi
42     echo ${answer}
43 }
44
45 yesno() {
46     local dflt question answer
47
48     question=$1
49     dflt=$2
50     while :; do
51         answer=$(ask "${question}" "${dflt}")
52         case "${answer}" in
53         [Yy]*)          return 0;;
54         [Nn]*)          return 1;;
55         esac
56         echo "Please answer yes or no."
57     done
58 }
59
60 failure() {
61     local retval badgroups badusers
62     retval=$1
63     badgroups=`getent group monkeysphere 641`
64     badusers=`getent passwd monkeysphere 641`
65
66     if [ X"$badgroups" != X ]; then
67         badgroups="
68 Conflicting group(s):
69
70 $badgroups"
71     fi
72
73     if [ X"$badusers" != X ]; then
74         badusers="Conflicting user(s):
75
76 $badusers"
77     fi
78     
79     cat <<EOF
80
81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
82 This port or package assumes that the ID number of 'monkeysphere' will
83 be 641.  But this system has:
84 $badgroups
85 $badusers
86
87 Please correct these conflict(s) and try again.
88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
89 EOF
90     exit $retval
91 }
92
93 case $2 in
94 POST-INSTALL)
95
96     # make sure that the correct user and group are present:
97     id_monkeysphere=`id -u monkeysphere 2> /dev/null`
98     gid_monkeysphere=`getent group monkeysphere | cut -f3 -d: 2> /dev/null`
99     if [ X"$id_monkeysphere" = X641 ] && [ X"$gid_monkeysphere" = X641 ];then
100         exit 0
101     else
102     # add an account 'monkeysphere' to this system
103         echo ""
104         echo "You need an account 'monkeysphere' whose ID number is 641, with group 'monkeysphere' (GID 641)"
105         if yesno "Would you like to create it automatically?" y; then
106         # We need a command 'pw(8)'
107             check_pw
108             pw groupadd monkeysphere -g 641 || failure $?
109             pw useradd monkeysphere -u 641 -g 641 -h - -d "$VARLIB" \
110                 -s /bin/sh -c 'monkeysphere authentication user,,,' || failure $?
111         # FIXME: should we really be using a real shell?  Convention
112         # (/usr/ports/UIDs) seems to indicate /nonexistent is
113         # preferred
114         else
115             echo "Please create it, and try again."
116             exit 1
117         fi
118     fi
119     # FIXME: we should create $VARLIB and chown the relevant subdirs
120     # (see debian/monkeysphere.postinst)
121     
122     ;;
123 esac