Projet

Général

Profil

Paste
Télécharger (2,32 ko) Statistiques
| Branche: | Révision:

root / bin / scripts-utils.sh @ 4127f213

1 d52a5b2f jenselme
#!/bin/sh
2
3
# This script contains useful functions for other scripts.
4
5
# Check if scripts-config.sh is imported.
6
if [ -z $scripts_config ] ; then
7
    echo "Import of scripts-config.sh required."
8
    . scripts-config.sh
9
fi
10
11
scripts_utils='imported'
12
13
ask_password() {
14
    # read -s doesn't work with sh.
15
    # usage: pass=`ask_password "password please:"`
16
    echo $1 >&2
17
    echo -n ">" >&2
18
    stty_avant=`stty -g`
19
    stty -echo
20
    read password
21
    stty $stty_avant
22
    echo "$password"
23
    unset password
24
}
25
26 4377ca71 Julien Enselme
generate_password() {
27 d52a5b2f jenselme
    # ARGS: [password_length]
28
    # The password contains special characters. '/' must be excluded to avoid sed malfunction.
29
30
    local site_password='/'
31
32
    if [ -z $1 ] ; then
33
        local password_length=20
34
    else
35
        local password_length=$1
36
    fi
37
38
    while echo "$site_password" | grep -Fq '/' ; do
39 5fc58a68 Julien Enselme
        site_password=`dd if=/dev/random count=1 | uuencode -m - | head -n 2 | tail -n 1 | cut -c-$password_length`
40 d52a5b2f jenselme
    done
41
42
    echo $site_password
43
}
44
45 4377ca71 Julien Enselme
count_d7_sites() {
46 d52a5b2f jenselme
    find $d7_dir_sites -type d ! -name all -maxdepth 1 | wc -l
47
}
48
49
check_arguments() {
50
    # ARGS: number of arguments passed to script, number of arguments required, [help text]
51
    if [ $1 -lt $2  ] ; then
52
        echo "Number of arguments insuffisant."
53
	echo -e $3
54
        exit 1
55
    fi
56
}
57 469b9d6d Julien Enselme
58
generate_settings_local() {
59
    # ARGS: site_name, site_password, d7_settings_local_template, d7_site_settings_local
60
    sed "s/\%\%DBUSER\%\%/$1/ ; s/\%\%DBNAME\%\%/$1/ ; s/\%\%DBPASS\%\%/$2/ ; s/\%\%SITE_NAME\%\%/$1/" < $3 > $4
61
}
62
63 4377ca71 Julien Enselme
give_dir() {
64 469b9d6d Julien Enselme
    # ARG: file
65 65ce03da Julien Enselme
    # Return the abosulte directory path of a file or a dir.
66 469b9d6d Julien Enselme
    settings_location=`realpath $1`
67
    echo `dirname $settings_location`
68
}
69 1c1c5a42 Julien Enselme
70
work_tree_clean() {
71
    git_status_output=`git status --porcelain`
72 6d91f38e Julien Enselme
    if [ -z "$git_status_output" ] ; then
73 1c1c5a42 Julien Enselme
	exit 0
74
    else
75
	exit 1
76
    fi
77
}
78
79
mail_unclean_work_tree() {
80
    cd $dir_multi_assos
81
    git_status_output=`git status`
82 6d91f38e Julien Enselme
    echo "$git_status_output" | mail -s "$1" $email_multi_assos
83 1c1c5a42 Julien Enselme
}
84
85
commit_if_unclean() {
86 6d91f38e Julien Enselme
    if ! `work_tree_clean` ; then
87 1c1c5a42 Julien Enselme
	commit_message="COMMIT OF UNCLEAN STUFF"
88 6d91f38e Julien Enselme
	commit -a -m "$commit_message"
89 1c1c5a42 Julien Enselme
	mail_unclean_work_tree "[git] $commit_message"
90
    fi
91
}
92
93
commit() {
94
    # ARG: commit message
95 6d91f38e Julien Enselme
    if [ -z "$1" ] ; then
96 1c1c5a42 Julien Enselme
	echo "Empty commit message. Nothing was commited."
97
	exit 2
98
    fi
99
    cd $dir_multi_assos
100 6d91f38e Julien Enselme
    git commit -a -m "$1"
101 1c1c5a42 Julien Enselme
}