source: home-linker/trunk/fuentes/home-linker.install/usr/sbin/home-linker @ 2543

Last change on this file since 2543 was 2543, checked in by daduve, 3 years ago

refactoring

  • Property svn:executable set to *
File size: 9.0 KB
Line 
1#!/bin/sh
2set -x
3
4
5echo "Username -> $1"
6
7LOG="/var/log/home_linker.log"
8echo "" > "$LOG"
9
10trim() {
11    local var="$*"
12    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
13    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
14    echo -n "$var"
15}
16
17#SET VARIABLES FOR THE ENVIRONMENT
18
19#GET VARIABLES
20USER=$1
21HL_USER=$(trim "$USER")
22
23if [ ! -z "$2" ]; then
24                HL_HOME="$2"   
25        else
26                NHOME=$(python -c "
27tmp='$HOME'.split('/')
28last=tmp[-1]
29print '/'.join(tmp[:-1])+'/'+last.strip()
30")
31
32                HL_HOME="$NHOME"
33fi
34
35
36if [ ! -z "$LDM_LANGUAGE" ]; then
37                AUX_LANG="$LDM_LANGUAGE"       
38        else
39                AUX_LANG="$LANG"
40fi
41
42
43
44# From this file get $LANG
45{ [ -r "$HL_HOME/.pam_environment" ] && . "$HL_HOME/.pam_environment" ;} || { [ -r "/etc/default/locale" ] && . "/etc/default/locale" ;} || LANG="en"
46
47
48# Get Usernames
49GRP_USER="$(id -ng $HL_USER)"
50
51# Source for user-dirs if it exists
52USER_DIRS="$HL_HOME/.config/user-dirs.dirs"
53[ ! -r $USER_DIRS ] || . $USER_DIRS
54
55# Source the token file if it exists
56LAST_LOGIN_DIRECTORIES="$HL_HOME/.config/user-dirs.created"
57[ ! -r "$LAST_LOGIN_DIRECTORIES" ] || . "$LAST_LOGIN_DIRECTORIES"
58
59#Temp file with new user-dirs
60[ -d "$HL_HOME" ] || { mkdir -p "$HL_HOME" ; chown $HL_USER:$GRP_USER "$HL_HOME" ;}
61[ -d "$HL_HOME/.config" ] || { mkdir -p "$HL_HOME/.config" ; chown $HL_USER:$GRP_USER "$HL_HOME/.config" ;}
62TEMP_NEW_USER_DIRS="$HL_HOME/.config/aux-user-dirs.create"
63:> $TEMP_NEW_USER_DIRS
64
65#set Acl on Home to be more restricted
66get_acl_from_server "$HL_HOME" "$GRP_USER" $HL_USER
67
68# Download directory actions
69DOWNLOADS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG" "Downloads")"
70DESKTOP_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Desktop")"
71DOCUMENTS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Documents")"
72SHARE_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Share")"
73SHARETEACHERS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Teachers_Share")"
74GROUPSSHARE_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Groups_Share")"
75
76
77[ ! -z $DESKTOP_LOCALE ] || DESKTOP_LOCALE="Desktop"
78[ ! -z $DOWNLOADS_LOCALE ] || DOWNLOADS_LOCALE="Downloads"
79[ ! -z $DOCUMENTS_LOCALE ] || DOCUMENTS_LOCALE="Documents"
80[ ! -z $SHARE_LOCALE ] || SHARE_LOCALE="Share"
81[ ! -z $SHARETEACHERS_LOCALE ] || SHARETEACHERS_LOCALE="Teachers_Share"
82[ ! -z $GROUPSSHARE_LOCALE ] || GROUPSSHARE_LOCALE="Groups_Share"
83
84
85
86
87# Umount directories mounted previously
88LIST_BIND_MOUNTED="$(mount | grep -E '[,(]bind[,)]' | cut -d ' ' -f3 | tr '\n' ' ')"
89for MOUNT_POINT in $LIST_BIND_MOUNTED; do
90    rc=0
91    $(echo $MOUNT_POINT | grep -q $HL_HOME) || rc=1
92    if [ $rc -eq 0 ]; then
93        umount $MOUNT_POINT || true
94    fi
95done
96
97
98if [ "$TOKEN_DESKTOP" != "$DESKTOP_LOCALE" ]; then
99    if [ ! -z "$TOKEN_DESKTOP" -a -e $HL_HOME/${TOKEN_DESKTOP} ]; then
100        mv $HL_HOME/${TOKEN_DESKTOP} $HL_HOME/${DESKTOP_LOCALE}
101    else
102        mkdir $HL_HOME/${DESKTOP_LOCALE} || true
103    fi
104fi
105
106if [ -L "$HL_HOME/${DESKTOP_LOCALE}" ]; then
107    rm -rf "$HL_HOME/${DESKTOP_LOCALE}"
108    mkdir $HL_HOME/${DESKTOP_LOCALE}
109fi
110
111chown $HL_USER:$GRP_USER $HL_HOME/${DESKTOP_LOCALE}
112echo "TOKEN_DESKTOP=\"$DESKTOP_LOCALE\"" >> $TEMP_NEW_USER_DIRS
113
114
115if [ "$TOKEN_DOCUMENTS" != "$DOCUMENTS_LOCALE" ]; then
116    if [ ! -z "$TOKEN_DOCUMENTS" -a -e $HL_HOME/${TOKEN_DOCUMENTS} ]; then
117        mv $HL_HOME/${TOKEN_DOCUMENTS} $HL_HOME/${DOCUMENTS_LOCALE}
118    else
119        mkdir $HL_HOME/${DOCUMENTS_LOCALE} || true
120    fi
121fi
122
123if [ -L "$HL_HOME/${DOCUMENTS_LOCALE}" ]; then
124    rm -rf "$HL_HOME/${DOCUMENTS_LOCALE}"
125    mkdir $HL_HOME/${DOCUMENTS_LOCALE}
126fi
127
128
129chown $HL_USER:$GRP_USER $HL_HOME/${DOCUMENTS_LOCALE}
130echo "TOKEN_DOCUMENTS=\"$DOCUMENTS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
131
132if [ "$TOKEN_DOWNLOADS" != "$DOWNLOADS_LOCALE" ]; then
133    if [ ! -z "$TOKEN_DOWNLOADS" -a -e $HL_HOME/${TOKEN_DOWNLOADS} ]; then
134        mv $HL_HOME/${TOKEN_DOWNLOADS} $HL_HOME/${DOWNLOADS_LOCALE}
135    else
136        mkdir $HL_HOME/${DOWNLOADS_LOCALE} || true
137    fi
138fi
139
140if [ -L "$HL_HOME/${DOWNLOADS_LOCALE}" ]; then
141    rm -rf "$HL_HOME/${DOWNLOADS_LOCALE}"
142    mkdir $HL_HOME/${DOWNLOADS_LOCALE}
143fi
144
145chown $HL_USER:$GRP_USER $HL_HOME/${DOWNLOADS_LOCALE}
146echo "TOKEN_DOWNLOADS=\"$DOWNLOADS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
147
148if [ -d "/run/$HL_USER/home" -o -d "/net/server-sync/home/" ]; then
149    if [ "$TOKEN_SHARE" != "$SHARE_LOCALE" ]; then
150            if [ ! -z "$TOKEN_SHARE" -a -e $HL_HOME/${TOKEN_SHARE} ]; then
151                mv $HL_HOME/${TOKEN_SHARE} $HL_HOME/${SHARE_LOCALE}
152            else
153                mkdir $HL_HOME/${SHARE_LOCALE} || true
154            fi
155    fi
156
157    if [ -L "$HL_HOME/${SHARE_LOCALE}" ]; then
158            rm -rf "$HL_HOME/${SHARE_LOCALE}"
159            mkdir "$HL_HOME/${SHARE_LOCALE}"
160    fi
161    chown $HL_USER:$GRP_USER $HL_HOME/${SHARE_LOCALE}
162    echo "TOKEN_SHARE=\"$SHARE_LOCALE\"" >> $TEMP_NEW_USER_DIRS
163    if [ "$GRP_USER" = "teachers" ]; then
164        if [ "$TOKEN_SHARETEACHERS" != "$SHARETEACHERS_LOCALE" ]; then
165                if [ ! -z "$SHARETEACHERS_TOKEN" -a -e $HL_HOME/${SHARETEACHERS_TOKEN} ]; then
166                        mv $HL_HOME/${SHARETEACHERS_TOKEN} $HL_HOME/${SHARETEACHERS_LOCALE}
167                else
168                        mkdir $HL_HOME/${SHARETEACHERS_LOCALE} || true
169                fi
170        fi
171
172        if [ -L "$HL_HOME/${SHARETEACHERS_LOCALE}" ]; then
173            rm -rf "$HL_HOME/${SHARETEACHERS_LOCALE}"
174            mkdir "$HL_HOME/${SHARETEACHERS_LOCALE}"
175        fi
176
177        chown $HL_USER:$GRP_USER $HL_HOME/${SHARETEACHERS_LOCALE}
178        echo "TOKEN_SHARETEACHERS=\"$SHARETEACHERS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
179    fi
180fi
181
182if [ "$TOKEN_GROUPSSHARE" != "$GROUPSSHARE_LOCALE" ]; then
183    if [ ! -z "$TOKEN_GROUPSSHARE" -a -e $HL_HOME/${TOKEN_GROUPSSHARE} ]; then
184        mv $HL_HOME/${TOKEN_GROUPSSHARE} $HL_HOME/${GROUPSSHARE_LOCALE}
185    else
186        mkdir $HL_HOME/${GROUPSSHARE_LOCALE} || true
187    fi
188fi
189
190if [ -L "$HL_HOME/${GROUPSSHARE_LOCALE}" ]; then
191    rm -rf "$HL_HOME/${GROUPSSHARE_LOCALE}"
192    mkdir "$HL_HOME/${GROUPSSHARE_LOCALE}"
193fi
194
195chown $HL_USER:$GRP_USER $HL_HOME/${GROUPSSHARE_LOCALE}
196echo "TOKEN_GROUPSSHARE=\"$GROUPSSHARE_LOCALE\"" >> $TEMP_NEW_USER_DIRS
197
198fix_owner_home "$HL_HOME" "$GRP_USER" "$HL_USER"
199
200if [ -d "/net/server-sync/home/$GRP_USER/$HL_USER/" ]; then
201    # other machines in aula
202    echo "OTHER MACHINES IN AULA" >> "$LOG"
203    [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /net/server-sync/home/$GRP_USER/$HL_USER/Desktop $HL_HOME/${DESKTOP_LOCALE} || true
204    [ "${DOCUMENTS_LOCALE}" = "" ] || mount --bind /net/server-sync/home/$GRP_USER/$HL_USER/Documents $HL_HOME/${DOCUMENTS_LOCALE} || true
205    [ "${SHARE_LOCALE}" = "" ] || mount --bind /net/server-sync/share $HL_HOME/${SHARE_LOCALE} || true
206    if [ "$GRP_USER" = "teachers" ]; then
207        [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /net/server-sync/teachers_share $HL_HOME/${SHARETEACHERS_LOCALE} || true
208        [ -d $HL_HOME/alum ] || mkdir $HL_HOME/alum
209        mount --bind /net/server-sync/home/students/ $HL_HOME/alum || true
210    fi
211    [ "${GROUPSSHARE_LOCALE}" = "" ] || mount --bind /net/server-sync/groups_share $HL_HOME/${GROUPSSHARE_LOCALE} || true
212   
213elif [ -d "/var/run/$HL_USER/home" ]; then
214    #Fat clients
215    echo "FATCLIENTS IN AULA" >> "$LOG"
216    [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /run/$HL_USER/home/$GRP_USER/$HL_USER/Desktop $HL_HOME/${DESKTOP_LOCALE} || true
217    [ "${DOCUMENTS_LOCALE}" = "" ] || mount --bind /run/$HL_USER/home/$GRP_USER/$HL_USER/Documents $HLHOME/${DOCUMENTS_LOCALE} || true
218    [ "${SHARE_LOCALE}" = "" ] || mount --bind /run/$HL_USER/share $HL_HOME/${SHARE_LOCALE} || true
219    if [ "$GRP_USER" = "teachers" ]; then
220        [ "${SHARETEACHERS_LOCALE}" = "" ] || mount --bind /run/$HL_USER/teachers_share $HL_HOME/${SHARETEACHERS_LOCALE} || true
221        [ -d $HL_HOME/alum ] || mkdir $HL_HOME/alum
222        mount --bind /run/$HL_USER/home/students $HL_HOME/alum || true
223    fi
224    [ "${GROUPSSHARE_LOCALE}" = "" ] || mount --bind /run/$HL_USER/groups_share $HL_HOME/${GROUPSSHARE_LOCALE} || true
225fi
226mv ${TEMP_NEW_USER_DIRS} ${LAST_LOGIN_DIRECTORIES}
227
228
229cat > $USER_DIRS << EOF
230# This file is written by xdg-user-dirs-update
231# If you want to change or add directories, just edit the line you're
232# interested in. All local changes will be retained on the next run
233# Format is XDG_xxx_DIR="$HL_HOME/yyy", where yyy is a shell-escaped
234# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
235# absolute path. No other format is supported.
236#
237XDG_DESKTOP_DIR="\$HOME/$DESKTOP_LOCALE"
238XDG_DOWNLOAD_DIR="\$HOME/$DOWNLOADS_LOCALE"
239XDG_DOCUMENTS_DIR="\$HOME/$DOCUMENTS_LOCALE"
240
241EOF
242chmod 600 "$USER_DIRS"
243chown $HL_USER:$GRP_USER "$USER_DIRS"
244
245date >> "$LOG"
246echo "" >> "$LOG"
247echo "Las variables a tener en cuenta son:" >> "$LOG"
248echo "         \$HL_USER -> $HL_USER" >> "$LOG"
249echo "         \$HL_HOME -> $HL_HOME" >> "$LOG"
250echo "         \$HOME -> $HOME" >> "$LOG"
251echo "         \$DESKTOP_LOCALE -> $DESKTOP_LOCALE" >> "$LOG"
252echo "" >> "$LOG"
253echo "El idioma es $AUX_LANG" >> "$LOG"
254echo "" >> "$LOG"
255echo "AFTER HOME LINKER" >> "$LOG"
256tree $HL_HOME >> "$LOG"
257echo "" >> "$LOG"
258cat $HL_HOME/.config/user-dirs.created >> "$LOG"
259echo "" >> "$LOG"
260cat $HL_HOME/.config/user-dirs.dirs >> "$LOG"
261echo "" >> "$LOG"
262echo "END /usr/bin/home-linker" >> "$LOG"
263
264
265
266exit 0
Note: See TracBrowser for help on using the repository browser.