typo
[build.git] / mirroring / mirror.sh
1 #!/bin/bash
2 # $Id$
3
4 COMMAND=$(basename $0)
5 DIRNAME=$(dirname $0)
6
7 default_url="http://localhost/mirror/"
8 default_distro="f8"
9 all_distros="fc4 fc6 f7 f8 centos5"
10
11 function check_distro () {
12     local distro=$1; shift
13     if [ ! -d $DIRNAME/$distro/yum.repos.d ] ; then
14         echo "Distro $distro not supported - skipped"
15         return 1
16     fi
17     return 0
18 }
19
20 function do_repo () {
21     local distro=$1; shift
22     sedargs="-e s,@MIRRORURL@,$URL,"
23     [ -n "$GPGOFF" ] && sedargs="$sedargs -e "'s,gpgcheck\W*=\W*1,gpgcheck=0,'
24     sed $sedargs $DIRNAME/$distro/yum.repos.d/building.repo.in
25 }
26
27 function do_init () { 
28     local distro=$1; shift
29     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
30     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
31     if [ ! -d $dir ] ; then
32         [ -n "$VERBOSE" ] && echo Creating dir $dir
33         mkdir -p -d $dir
34     fi
35     [ -n "$VERBOSE" ] && echo "Creating $repo"
36     do_repo $distro > $repo
37 }
38
39 function do_diff () {
40     local distro=$1; shift
41     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
42     if [ ! -f $repo ] ; then
43         echo "Cannot find $repo"
44     else
45         would=/tmp/$COMMAND.$$
46         do_repo $distro > $would
47         echo "==================== DIFF for $distro" '(current <-> would be)'
48         diff $repo $would 
49         rm $would
50     fi
51 }
52
53 function do_display () {
54     local distro=$1; shift
55     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
56     if [ -d $dir ] ; then
57         echo "====================" Contents of $dir
58         find $dir -name '*.repo' | xargs head --verbose --lines=1000
59     else
60         echo "====================" $dir does not exist
61     fi
62 }
63
64 function do_clean () {
65     local distro=$1; shift
66     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
67     [ -n "$VERBOSE" ] && echo Removing $repo
68     rm $repo
69 }
70
71 function do_superclean () {
72     local distro=$1; shift
73     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
74     [ -n "$VERBOSE" ] && echo Removing all repo files in $dir
75     rm $dir/*.repo
76 }
77
78 function usage () {
79     echo "Usage $COMMAND [options] <command>"
80     echo "  a help to manage the yum.repos.d template in /etc/vservers/.distributions/<distro>"
81     echo "Available commands"
82     echo "  display: shows content (default if missing)"
83     echo "  init: creates /etc/vservers/.distributions/<distro>/yum.repos.d/building.repo"
84     echo "       default is to use mirror root at $default_url"
85     echo "       use -u URL to specify another location"
86     echo "  diff: shows diff between current and what init would do"
87     echo "  clean: removes building.repo"
88     echo "  superclean: removes yum.repos.d altogether"
89     echo "Options"
90     echo "  -f <distro> : defaults to $default_distro"
91     echo "  -a : runs on all distros $all_distros"
92     echo "  -0 : turns off gpgcheck"
93     echo "  -v : verbose"
94     echo "Examples"
95     echo "  $COMMAND -a display "
96     echo "  $COMMAND -a superclean"
97     echo "  $COMMAND -a -u http://mirror.one-lab.org/ init"
98     echo "  $COMMAND -a display"
99     exit 1
100 }
101     
102 DISTROS=""
103 URL=""
104 VERBOSE=""
105 GPGOFF=""
106
107 function main () {
108
109     while getopts "u:f:a0v" opt; do
110         case $opt in
111             u) URL=$OPTARG ;;
112             f) DISTROS="$DISTROS $OPTARG" ;;
113             a) DISTROS="$DISTROS $all_distros" ;;
114             0) GPGOFF=true ;;
115             v) VERBOSE=true ;;
116             *) usage ;;
117         esac
118     done
119
120     shift $(($OPTIND - 1))
121     
122     # no action = display
123     case "$#" in 
124         0)
125             action=display ;;
126         1) 
127             action=$1; shift
128             case $action in
129                 disp*) action=display ;;
130                 init*) action=init ;;
131                 diff*) action=diff ;;
132                 clea*) action=clean ;;
133                 super*) action=superclean ;;
134                 *) usage ;;
135             esac ;;
136         *)
137             usage ;;
138     esac
139
140     [ -z "$URL" ] && URL=$default_url
141     [ -z "$DISTROS" ] && DISTROS="$default_distro"
142
143     # remove trailing slash
144     URL=$(echo $URL | sed -e 's,/$,,')
145
146     for distro in $DISTROS; do
147         [ -n "$VERBOSE" ] && echo ==================== Running $action for $distro
148         check_distro $distro && do_$action $distro
149     done
150
151     exit 0
152 }
153
154 main "$@"