Project

General

Profile

Paste
Download (2.61 KB) Statistics
| Branch: | Revision:

root / bin / scripts-utils.sh @ 6d91f38e

1
#!/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
ask_password_db() {
27
    # ARGS: server_name, user_name
28
    local db_password="pour_boucler"
29
    # empty db request to validate password
30
    while ! mysql -h $1 -u $2 -p$db_password -e "" 2>/dev/null ; do
31
        db_password=`ask_password "database password:"`
32
    done
33
    echo $db_password
34
}
35

    
36
generate_password() {
37
    # ARGS: [password_length]
38
    # The password contains special characters. '/' must be excluded to avoid sed malfunction.
39

    
40
    local site_password='/'
41

    
42
    if [ -z $1 ] ; then
43
        local password_length=20
44
    else
45
        local password_length=$1
46
    fi
47

    
48
    while echo "$site_password" | grep -Fq '/' ; do
49
        site_password=`dd if=/dev/urandom count=1 | uuencode -m - | head -n 2 | tail -n 1 | cut -c-$password_length`
50
    done
51

    
52
    echo $site_password
53
}
54

    
55
count_d7_sites() {
56
    find $d7_dir_sites -type d ! -name all -maxdepth 1 | wc -l
57
}
58

    
59
check_arguments() {
60
    # ARGS: number of arguments passed to script, number of arguments required, [help text]
61
    if [ $1 -lt $2  ] ; then
62
        echo "Number of arguments insuffisant."
63
	echo -e $3
64
        exit 1
65
    fi
66
}
67

    
68
generate_settings_local() {
69
    # ARGS: site_name, site_password, d7_settings_local_template, d7_site_settings_local
70
    sed "s/\%\%DBUSER\%\%/$1/ ; s/\%\%DBNAME\%\%/$1/ ; s/\%\%DBPASS\%\%/$2/ ; s/\%\%SITE_NAME\%\%/$1/" < $3 > $4
71
}
72

    
73
give_dir() {
74
    # ARG: file
75
    # Return the abosulte directory path of a file or a dir.
76
    settings_location=`realpath $1`
77
    echo `dirname $settings_location`
78
}
79

    
80
work_tree_clean() {
81
    git_status_output=`git status --porcelain`
82
    if [ -z "$git_status_output" ] ; then
83
	exit 0
84
    else
85
	exit 1
86
    fi
87
}
88

    
89
mail_unclean_work_tree() {
90
    cd $dir_multi_assos
91
    git_status_output=`git status`
92
    echo "$git_status_output" | mail -s "$1" $email_multi_assos
93
}
94

    
95
commit_if_unclean() {
96
    if ! `work_tree_clean` ; then
97
	commit_message="COMMIT OF UNCLEAN STUFF"
98
	commit -a -m "$commit_message"
99
	mail_unclean_work_tree "[git] $commit_message"
100
    fi
101
}
102

    
103
commit() {
104
    # ARG: commit message
105
    if [ -z "$1" ] ; then
106
	echo "Empty commit message. Nothing was commited."
107
	exit 2
108
    fi
109
    cd $dir_multi_assos
110
    git commit -a -m "$1"
111
}