Merge pull request #115 from hfmanson/master
[tlspool] / QUICKSTART.sh
1 #! /usr/bin/env sh
2
3 # Copyright: the ARPA2 project. See LICENSE-USERSPACE.MD
4 #
5 # Quickstart: how to get a working instance of tlspool.
6
7 # The assumption is that you are using a shell environment.
8 # First we check for a few necessary programs. 
9
10 PROGRAMS="nix-env git"
11
12 for requiredProg in $PROGRAMS
13 do
14   command -v $requiredProg >/dev/null && continue || { printf "$requiredProg is not available. Please install."; exit 1; }
15 done
16
17 # If you start with nix you can install git (if you don't have it) from there 
18 # with "nix-env -i git".
19
20 # See: http://nixos.org/nix/manual/#chap-installation).
21
22 # Alternatively you can install git through your package manager.
23 # If you use a computer platform which doesn't have one, see:
24 # https://git-scm.com/downloads). 
25
26 printf "QUICKSTART.sh from ARPA2 project's TLS Pool here.\n\n"
27 printf "Usage: QUICKSTART.sh /path/you/want subdirectory_name.\n\n"
28
29 # User can indicate where to install everything by providing the 
30 # desired path and subfolder name as an argument to the script. By default 
31 # everything is placed in a time-stamped folder located directly under the 
32 # current folder or directory. 
33
34 # First we check to see if there are any arguments, then we clean them up.
35
36 if [ -z "$1" ]; then
37   BASEDIR=$PWD
38 else
39   if [ -d "$1" ]; then
40     # Clean up trailing slashes etc
41     x="$1"
42     case $x in
43       *[!/]*/) BASEDIR=${x%"${x##*[!/]}"};;
44     esac
45   fi
46 fi
47
48 if [ -z "$2" ]; then
49   WORKINGDIR="$(date --iso-8601)-tlspool-environment"; 
50 else
51   y="$2"
52   WORKINGDIR=${y##*/};
53 fi
54
55 # Ask the user if the settings are okay, and if she or he agrees with proceeding. 
56
57 printf "I'm going to create a subdirectory called $WORKINGDIR in $BASEDIR and install tlspool using nix.\n"
58 printf "You'll get a local copy of ARPA2's nixpkgs for free.\nAre you okay with that (y/n)? "
59
60 old_stty_cfg=$(stty -g)
61 stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg 
62 if printf "$answer" | grep -iq "^y" ;then
63   printf "\nGreat. I'll get to work in $BASEDIR/$WORKINGDIR. Big stuff, might take a while to download.\n"
64 else
65   printf "\nGood that you told me. \nIf you change your mind, let me know. \n"
66   
67   printf "Bye.\n"
68   exit 1;
69 fi
70
71 # Let's make sure the working directory exists, and go there
72
73 if [ ! -d "$BASEDIR/$WORKINGDIR" ]; then
74   mkdir "$BASEDIR/$WORKINGDIR"
75 fi
76
77 cd "$BASEDIR/$WORKINGDIR"
78
79 # By keeping the tlspool and nixpkgs repositories inside the same folder we can update
80 # tlspool with a simple "git pull" from the main repository and nix-build command.
81
82 if [ ! -d "tlspool" ]; then
83   git clone https://github.com/arpa2/tlspool
84   git checkout 49bf1157e3471ee15bc279d41c9492646a2bf44c
85 else
86   cd tlspool
87   git pull https://github.com/arpa2/tlspool
88   git checkout 49bf1157e3471ee15bc279d41c9492646a2bf44c
89   cd ..
90 fi
91
92 if [ ! -d "nixpkgs" ]; then
93   git clone https://github.com/arpa2/nixpkgs
94 else
95   cd nixpkgs
96   git pull https://github.com/arpa2/nixpkgs
97   cd ..
98 fi
99
100 if [ ! -d "steamworks" ]; then
101   git clone https://github.com/arpa2/steamworks
102 else
103   cd steamworks 
104   git pull https://github.com/arpa2/steamworks
105   cd ..
106 fi
107
108 # Go into the nixpkgs folder and switch to the tlspool branch:
109
110 cd nixpkgs
111 export NIXPKGS="$BASEDIR/$WORKINGDIR/nixpkgs"
112 git checkout tlspool
113
114 # Install tlspool and all the dependencies through nix:
115
116 nix-env -f "$NIXPKGS" -iA tlspool
117
118 cd ..
119
120 # NB: for SoftHSM you will need to create a config file
121 CONFIGFILE="$HOME/.config/softhsm2/softhsm2.conf";
122
123 # This will have the following minimal contents
124 LINE1a="directories.tokendir = ";
125 LINE1b="/path/to/tokendir/";
126 LINE2="objectstore.backend = file";
127 LINE3="log.level = DEBUG";
128
129 # Does the user already have a SoftHSM2 config file?
130
131 if [ ! -e "$CONFIGFILE" ];
132 then
133
134   printf "Don't forget to create the config file for SoftHSM2\n"
135   printf "You can create a file named $CONFIGFILE"
136   printf "with the following suggested content:\n"
137   printf  "%s\n--------\n";
138   printf "$LINE1a$LINE1b\n$LINE2\n$LINE3\n"
139   printf  "%s\n--------\n";
140   printf "After that you can initiate a token with:\n\n"
141   printf "softhsm2-util --init-token --free --label 'TLS_Pool_dev_data'\n\n"
142
143   printf "Do you want me to create the config file for you, with tokendir pointing to ./$WORKINGDIR/token (y/n)? "
144   stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg 
145
146   if echo "$answer" | grep -iq "^y" ; then
147     # Make sure config directory and token directory exist
148     if [ ! -d "$HOME/.config/softhsm2" ]; then 
149       mkdir -p "$HOME/.config/softhsm2"
150     fi
151     if [ ! -d "$BASEDIR/$WORKINGDIR/token" ]; then 
152       mkdir -p "$BASEDIR/$WORKINGDIR/token"
153     fi
154     # Create the config file and check wether it was created.
155     printf "$LINE1a$BASEDIR/$WORKINGDIR/token\n$LINE2\n$LINE3\n" >> "$CONFIGFILE"
156     if [ -a "$CONFIGFILE" ]; then 
157       printf "\nSoftHSMv2 configuration file $CONFIGFILE created.\n"
158     fi
159     # Now, given that there was no config, surely there will not be a token. 
160     # So should we generate it? Let's ask.
161     printf "\nDo you want to generate a token (y/n)?"
162     stty raw -echo ; answer=$(head -c 1) ; stty $old_stty_cfg 
163     if echo "$answer" | grep -iq "^y" ; then
164       printf "\nDon't forget to write down your PIN numbers.\n"
165       softhsm2-util --init-token --free --label 'TLS_Pool_dev_data'
166     fi
167   else
168       printf "\nSoftHSM doesn't work without a config file. But you probably already got that.\n"
169       exit 1;
170   fi
171 else
172   printf "You've already got a config file for SoftHSM2. Great."
173 fi
174
175 printf "\n\nYou can now go into the ../tlspool directory and edit files "
176 printf "you want to edit. If you want to rebuild tlspool, just reexecute\n\n"
177 printf "nix-env -f $NIXPKGS -iA tlspool\n\n"
178
179 UPDATESCRIPT="$BASEDIR/$WORKINGDIR/update-tlspool.sh"
180
181 if [ ! -e $UPDATESCRIPT ]; then
182   printf "#! /usr/bin/env sh\n\n# Created by QUICKSTART.sh.\n$SHELL $BASEDIR/$WORKINGDIR/tlspool/QUICKSTART.sh '$BASEDIR' '$WORKINGDIR'\n" > $UPDATESCRIPT
183   chmod +x $UPDATESCRIPT;
184 fi
185
186 printf "A simple git pull will update either.\n\nOr just copy $UPDATESCRIPT to wherever you want it to be.\n\n"
187
188 printf "You can run 'tlspool -c configfile'. There is an example config file at "
189 printf "~i/.nix-profile/etc/tlspool/tlspool.conf which you can modify for usage."