Cosmetic change - better word wrapping
[jigit.git] / jigit
1 #!/bin/bash
2 #
3 # jigit
4 #
5 # (c) 2004 Steve McIntyre
6 #
7 # Wrapper for jigdo to make updating CDs easier
8 #
9
10 # Some definitions
11 APT_CACHE=/var/cache/apt/archives
12 WGET_OPTS="-q --passive-ftp --no-directories --non-verbose"
13 HOST="http://people.debian.org/~93sam/"
14
15 if [ -e /etc/jigit.conf ] ; then
16     . /etc/jigit.conf
17 fi
18 if [ -e ~/.jigit.conf ] ; then
19     . ~/.jigit.conf
20 fi
21
22 URL="$HOST/jigit"
23
24 if [ "$TMPDIR"x = ""x ] ; then
25     TMPDIR=/tmp
26 fi
27
28 CDNAME=$1
29 case $CDNAME in
30     --help|-h)
31         echo "$0:"
32         echo "Automatic downloader for Jigdo images"
33         echo "Usage:"
34         echo "   jigit [distribution]"
35         exit 1
36     ;;
37 esac
38
39 for DIR in files jigdo
40 do
41     if [ ! -d $TMPDIR/jigit/$DIR ] ; then
42         mkdir -p -m 700 $TMPDIR/jigit/$DIR
43     fi
44 done
45
46 cd $TMPDIR/jigit/jigdo
47 # Grab the latest config file
48 echo "Downloading config:   $URL/$CDNAME.conf"
49 wget $WGET_OPTS --mirror $URL/$CDNAME.conf
50
51 . $CDNAME.conf
52 echo "Downloading jigdo:    $URL/$JIGDO"
53 wget $WGET_OPTS --mirror $URL/$JIGDO
54 echo "Downloading template: $URL/$TEMPLATE"
55 wget $WGET_OPTS --mirror $URL/$TEMPLATE
56
57 echo "If you have a mirror, or any previous CD or CD image(s) available,"
58 echo "where are they mounted?"
59 echo "Say \"none\" if you have none; separate multiple entries with spaces"
60 read -p "> [none] " CD
61 if [ "$CD"x = ""x ] ; then
62     CD="none"
63 fi
64
65 cd $TMPDIR/jigit/files
66 # If we have a CD or iso, add it to the find list
67 if [ "$CD" != "none" ] ; then
68     FIND="$CD"
69 fi
70
71 # Now the local apt cache and our own temp directory
72 FIND="$FIND $APT_CACHE $TMPDIR/jigit/files"
73
74 > $TMPDIR/jigit/files/md5-list
75
76 for LOC in $FIND
77 do
78     echo
79     echo "Checking MD5 sums of files in $LOC:"
80     find $LOC -type f | \
81         xargs jigsum >> $TMPDIR/jigit/files/md5-list
82 done
83 echo
84 echo
85
86 rm -f $TMPDIR/jigit/jigdo/missing-list
87
88 # Check if we have all the pieces we need
89 mkimage -f $TMPDIR/jigit/files/md5-list \
90     -t $TMPDIR/jigit/jigdo/$TEMPLATE \
91     -j $TMPDIR/jigit/jigdo/$JIGDO \
92     -M $TMPDIR/jigit/jigdo/missing-list
93
94 # If we have a missing list, we're missing some files. Go and get them
95 if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
96     NUM=`wc -l $TMPDIR/jigit/jigdo/missing-list | awk '{print $1}'`
97     echo "Need to download $NUM files to complete the image"
98     while [ $NUM -gt 0 ]
99     do
100         cd $TMPDIR/jigit/files
101         for file in `cat $TMPDIR/jigit/jigdo/missing-list`
102         do
103             printf "\r%5d files missing; retrieving %-45.45s" $NUM `basename $file`
104             mkdir -p `dirname $file`
105             wget $WGET_OPTS --mirror $URL/$SNAPSHOT/$file -O $file
106             SIZE=`stat -c %s $file`
107             if [ $SIZE -gt 0 ] ; then
108                 NUM=$(($NUM - 1))
109             else
110                 echo
111                 echo "Unable to download file $URL/$SNAPSHOT/$file to $file. Abort"
112                 exit 1
113             fi
114             jigsum $file >> $TMPDIR/jigit/files/md5-list 2>/dev/null
115         done
116     done
117
118     rm -f $TMPDIR/jigit/jigdo/missing-list
119         # Check (again) if we have all the pieces we need
120     mkimage -f $TMPDIR/jigit/files/md5-list \
121         -t $TMPDIR/jigit/jigdo/$TEMPLATE \
122         -j $TMPDIR/jigit/jigdo/$JIGDO \
123         -M $TMPDIR/jigit/jigdo/missing-list
124
125         # If we still have a missing list, something is wrong. Give up
126     if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
127         echo "Failed to build image; could not find/download all the files. ABORT"
128         exit 1
129     fi
130 fi
131
132 printf "\r%5d files missing; all needed files available                       " 0
133 echo
134 echo
135 # We should have all the bits; build the image
136 mkimage -v -f $TMPDIR/jigit/files/md5-list \
137     -t $TMPDIR/jigit/jigdo/$TEMPLATE \
138     -j $TMPDIR/jigit/jigdo/$JIGDO \
139     -o $TMPDIR/jigit/jigdo/$CDNAME.iso \
140
141 if [ $? -eq 0 ] ; then
142     echo "Image created successfully in $TMPDIR/jigit/jigdo/$CDNAME.iso"
143 else
144     echo "Error creating image: $?"
145 fi